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