TagRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 26 4
A insertMultiple() 0 18 3
A deleteUpToId() 0 7 2
A getTagFetchLimit() 0 3 1
A __construct() 0 9 1
A getLastUsedId() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\AsyncVarnish\Model;
5
6
use Magento\Framework\App\ResourceConnection;
7
use IntegerNet\AsyncVarnish\Model\ResourceModel\Tag as TagResource;
8
use Magento\Framework\App\Config\ScopeConfigInterface;
9
10
class TagRepository
11
{
12
    /**
13
     * DB Storage table name
14
     */
15
    const TABLE_NAME = 'integernet_async_varnish_tags';
16
17
    /**
18
     * Limits the amount of tags being fetched from database
19
     */
20
    const FETCH_TAG_LIMIT_CONFIG_PATH = 'system/full_page_cache/async_varnish/varnish_fetch_tag_limit';
21
22
    private $lastUsedId;
23
24
    /**
25
     * @var \Magento\Framework\DB\Adapter\AdapterInterface
26
     */
27
    private $connection;
28
29
    /**
30
     * @var ResourceConnection
31
     */
32
    private $resource;
33
34
    /**
35
     * @var TagResource
36
     */
37
    private $tagResource;
38
39
    private $scopeConfig;
40
41
    /**
42
     * @param \Magento\Framework\App\ResourceConnection $resource
43
     */
44
    public function __construct(
45
        ResourceConnection $resource,
46
        TagResource $tagResource,
47
        ScopeConfigInterface $scopeConfig
48
    ) {
49
        $this->connection = $resource->getConnection();
50
        $this->resource = $resource;
51
        $this->tagResource = $tagResource;
52
        $this->scopeConfig = $scopeConfig;
53
    }
54
55
    private function getTagFetchLimit()
56
    {
57
        return $this->scopeConfig->getValue(self::FETCH_TAG_LIMIT_CONFIG_PATH);
58
    }
59
60
    /**
61
     * Insert multiple Tags
62
     *
63
     * @param array $tags
64
     * @return int
65
     * @throws \Exception
66
     */
67
    public function insertMultiple($tags = [])
68
    {
69
        if (empty($tags)) {
70
            return 0;
71
        }
72
73
        $data = array_map(
74
            function ($tag) {
75
                return ['entity_id' => null, 'tag' => $tag];
76
            },
77
            $tags
78
        );
79
80
        try {
81
            $tableName = $this->resource->getTableName(self::TABLE_NAME);
82
            return $this->connection->insertMultiple($tableName, $data);
83
        } catch (\Exception $e) {
84
            throw $e;
85
        }
86
    }
87
88
    /**
89
     * Delete multiple Tags by max entity_id
90
     *
91
     * @param int $maxId
92
     * @return int
93
     * @throws \Exception
94
     */
95
    public function deleteUpToId($maxId = 0)
96
    {
97
        try {
98
            $tableName = $this->resource->getTableName(self::TABLE_NAME);
99
            return $this->connection->delete($tableName, 'entity_id <= '.$maxId);
100
        } catch (\Exception $e) {
101
            throw $e;
102
        }
103
    }
104
105
    /**
106
     * @return array
107
     * @throws \Zend_Db_Statement_Exception
108
     */
109
    public function getAll()
110
    {
111
112
        $tags = [];
113
114
        $tagResource = $this->tagResource;
115
        $tagFetchLimit = $this->getTagFetchLimit();
116
117
        $maxIdResult = $tagResource->getMaxTagId($tagFetchLimit);
118
119
        if (empty($maxIdResult)) {
120
            return $tags;
121
        }
122
123
        $maxId = $maxIdResult['max_id'];
124
125
        $uniqueTagsResult = $tagResource->getUniqueTagsByMaxId((int)$maxId);
126
127
        if (!empty($uniqueTagsResult)) {
128
            $this->lastUsedId = $maxId;
129
130
            foreach ($uniqueTagsResult as $tag) {
131
                $tags[] = ($tag['tag']);
132
            }
133
        }
134
        return $tags;
135
    }
136
137
    /**
138
     * @return int
139
     */
140
    public function getLastUsedId()
141
    {
142
        return $this->lastUsedId ?: 0;
143
    }
144
}
145