Passed
Pull Request — master (#2)
by
unknown
03:15
created

PurgeAsyncCache::getMaxHeaderLengthFromConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\AsyncVarnish\Model;
5
6
use Magento\CacheInvalidate\Model\PurgeCache as PurgeCache;
0 ignored issues
show
Bug introduced by
The type Magento\CacheInvalidate\Model\PurgeCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use IntegerNet\AsyncVarnish\Model\TagRepository as TagRepository;
8
use Magento\Framework\App\Config\ScopeConfigInterface;
9
10
class PurgeAsyncCache
11
{
12
    /**
13
     * Size of the purge headers we are allowed to send to Varnish
14
     */
15
    const VARNISH_PURGE_TAG_GLUE = "|";
16
    const MAX_HEADER_LENGTH_CONFIG_PATH = 'system/full_page_cache/async_varnish/varnish_max_header_length';
17
18
    /**
19
     * @var PurgeCache
20
     */
21
    private $purgeCache;
22
23
    /**
24
     * @var TagRepository
25
     */
26
    private $tagRepository;
27
28
    /**
29
     * @var ScopeConfigInterface
30
     */
31
    private $scopeConfig;
32
33
    public function __construct(
34
        PurgeCache $purgeCache,
35
        ScopeConfigInterface $scopeConfig,
36
        TagRepository $tagRepository
37
    ) {
38
        $this->purgeCache = $purgeCache;
39
        $this->scopeConfig = $scopeConfig;
40
        $this->tagRepository = $tagRepository;
41
    }
42
43
    private function getMaxHeaderLengthFromConfig()
44
    {
45
        return $this->scopeConfig->getValue(self::MAX_HEADER_LENGTH_CONFIG_PATH);
46
    }
47
48
    /**
49
     * @throws \Zend_Db_Statement_Exception
50
     * @throws \Exception
51
     */
52
    public function run():int
53
    {
54
        $tags = $this->tagRepository->getAll();
55
        $maxHeaderLength = $this->getMaxHeaderLengthFromConfig();
56
        if (!empty($tags)) {
57
            $tagChunks = [];
58
            $index = 0;
59
            foreach ($tags as $tag) {
60
                $nextChunkString = (isset($tagChunks[$index])
61
                    ? $tagChunks[$index] . self::VARNISH_PURGE_TAG_GLUE
62
                    : '') . $tag;
63
                if (strlen($nextChunkString) <= $maxHeaderLength) {
64
                    $tagChunks[$index] = $nextChunkString;
65
                } else {
66
                    $index ++;
67
                    $tagChunks[$index] = $tag;
68
                }
69
            }
70
            foreach ($tagChunks as $tagChunk) {
71
                $this->purgeCache->sendPurgeRequest($tagChunk);
72
            }
73
        }
74
75
        if ($lastUsedId = $this->tagRepository->getLastUsedId()) {
76
            $this->tagRepository->deleteUpToId($lastUsedId);
77
        }
78
        return count($tags);
79
    }
80
}
81