Skip to content

Commit ee0ca5e

Browse files
committed
feat: type hints & docs
1 parent daa1487 commit ee0ca5e

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

Console/Command/Flush.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace SamJUK\CacheDebounce\Console\Command;
46

Cron/Flush.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace SamJUK\CacheDebounce\Cron;
46

@@ -15,8 +17,8 @@ public function __construct(
1517
$this->cacheDebouncedEntries = $cacheDebouncedEntries;
1618
}
1719

18-
public function execute()
20+
public function execute() : void
1921
{
20-
return $this->cacheDebouncedEntries->flush();
22+
$this->cacheDebouncedEntries->flush();
2123
}
2224
}

Model/Config.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace SamJUK\CacheDebounce\Model;
46

@@ -7,39 +9,55 @@
79
class Config
810
{
911
private const XML_PATH_GENERAL_ENABLED = 'samjuk_cache_debounce/general/enabled';
10-
private const XML_PATH_CRON_FLUSH = 'samjuk_cache_debounce/cron/flush';
1112

13+
/** @var bool $shouldDebouncePurgeRequest */
1214
private $shouldDebouncePurgeRequest = true;
1315

16+
/** @var ScopeConfigInterface $scopeConfig */
1417
private $scopeConfig;
15-
18+
1619
public function __construct(
1720
ScopeConfigInterface $scopeConfig
1821
) {
1922
$this->scopeConfig = $scopeConfig;
2023
}
2124

22-
public function isModuleEnabled()
25+
/**
26+
* Check if the module feature flag is enabled
27+
*/
28+
public function isModuleEnabled() : bool
2329
{
2430
return $this->getFlag(self::XML_PATH_GENERAL_ENABLED);
2531
}
2632

27-
public function shouldDebouncePurgeRequest()
33+
/**
34+
* Check if we should debounce this purge request
35+
*/
36+
public function shouldDebouncePurgeRequest() : bool
2837
{
2938
return $this->isModuleEnabled() && $this->shouldDebouncePurgeRequest;
3039
}
3140

32-
public function setShouldDebouncePurgeRequest($state)
41+
/**
42+
* Set flag to allow skipping the debounce (for flush workflows)
43+
*/
44+
public function setShouldDebouncePurgeRequest(bool $state) : void
3345
{
3446
$this->shouldDebouncePurgeRequest = $state;
3547
}
3648

37-
private function getFlag($path, $scope = 'default', $scopeCode = null)
49+
/**
50+
* Fetch a system config flag
51+
*/
52+
private function getFlag(string $path, ?string $scope = 'default', ?string $scopeCode = null) : bool
3853
{
3954
return (bool)$this->scopeConfig->isSetFlag($path, $scope, $scopeCode);
4055
}
4156

42-
private function getValue($path, $scope = 'default', $scopeCode = null)
57+
/**
58+
* Fetch a system config value
59+
*/
60+
private function getValue(string $path, ?string $scope = 'default', ?string $scopeCode = null)
4361
{
4462
return $this->scopeConfig->getValue($path, $scope, $scopeCode);
4563
}

Model/Entries.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace SamJUK\CacheDebounce\Model;
46

@@ -9,10 +11,19 @@
911

1012
class Entries
1113
{
14+
/** @var string $tableName */
1215
private $tableName;
16+
17+
/** @var ResourceConnection $resourceConnection */
1318
private $resourceConnection;
19+
20+
/** @var PurgeCache $purgeCache */
1421
private $purgeCache;
22+
23+
/** @var Config $config */
1524
private $config;
25+
26+
/** @var LoggerInterface $logger */
1627
private $logger;
1728

1829
public function __construct(
@@ -28,20 +39,29 @@ public function __construct(
2839
$this->tableName = $resourceConnection->getTableName('samjuk_cache_debounce');
2940
}
3041

31-
public function add($tags)
42+
/**
43+
* Add new tags to the purge queue
44+
*/
45+
public function add(array $tags) : void
3246
{
3347
$this->resourceConnection->getConnection()
3448
->insertArray($this->tableName, ['tag'], $tags);
3549
}
3650

37-
public function get()
51+
/**
52+
* Get all tags from the purge queue
53+
*/
54+
public function get() : array
3855
{
3956
$connection = $this->resourceConnection->getConnection();
4057
$query = $connection->select()->from($this->tableName, 'tag')->distinct();
4158
return $connection->fetchCol($query);
4259
}
4360

44-
public function flush()
61+
/**
62+
* Purge all queued tags, and clear the queue
63+
*/
64+
public function flush() : void
4565
{
4666
$tags = $this->get();
4767
if (count($tags) > 0) {

Plugin/PurgeCache.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace SamJUK\CacheDebounce\Plugin;
46

@@ -8,8 +10,10 @@
810

911
class PurgeCache
1012
{
13+
/** @var Config $config **/
1114
private $config;
1215

16+
/** @var CacheDebounceEntries $cacheDebouncedEntries */
1317
private $cacheDebouncedEntries;
1418

1519
public function __construct(
@@ -21,12 +25,14 @@ public function __construct(
2125
}
2226

2327
/**
28+
* Prevent flushing varnish now, and push the tag into a queue to flush later, if required.
29+
*
2430
* @param \Magento\CacheInvalidate\Model\PurgeCache $subject
2531
* @param callable $proceed
2632
* @param array|string $tags
2733
* @return bool
2834
*/
29-
public function aroundSendPurgeRequest(Subject $subject, callable $proceed, $tags)
35+
public function aroundSendPurgeRequest(Subject $subject, callable $proceed, $tags) : bool
3036
{
3137
if (!$this->config->shouldDebouncePurgeRequest()) {
3238
return $proceed($tags);

Test/Unit/Plugin/PurgeCacheTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function testPluginSkipsDebouncePurgeRequest()
4141
->willReturn(false);
4242

4343
$purgeCacheModel->expects($this->once())
44-
->method('sendPurgeRequest');
44+
->method('sendPurgeRequest')
45+
->willReturn(true);
4546

4647
$purgeCachePlugin->aroundSendPurgeRequest(
4748
$purgeCacheModel,

0 commit comments

Comments
 (0)