-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLogRepository.php
More file actions
228 lines (193 loc) · 7.09 KB
/
LogRepository.php
File metadata and controls
228 lines (193 loc) · 7.09 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Bundle\ApplicationLogger\Repository;
use Carbon\Carbon;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\Types;
use Pimcore\Bundle\ApplicationLoggerBundle\Handler\ApplicationLoggerDb;
use Pimcore\Bundle\StaticResolverBundle\Db\DbResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Bundle\ApplicationLogger\Util\Constant\FilterableFields;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Filter\FilterType;
use Pimcore\Bundle\StudioBackendBundle\Filter\MappedParameter\FilterParameter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\CollectionFilterParameter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\SortFilter;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use function count;
use function in_array;
use function is_int;
/**
* @internal
*/
final class LogRepository implements LogRepositoryInterface
{
use ElementProviderTrait;
private array $allowedKeys = [];
public function __construct(
private readonly DbResolverInterface $dbResolver,
) {
}
/**
* {@inheritdoc}
*/
public function list(CollectionFilterParameter $parameters): array
{
$qb = $this->dbResolver->get()->createQueryBuilder();
$qb
->select('*, priority + 0 AS priority_value')
->from(ApplicationLoggerDb::TABLE_NAME);
$filters = $parameters->getFilters();
if ($filters instanceof FilterParameter) {
$this->addFilters($qb, $filters);
$this->addSorting($qb, $filters->getSortFilter());
$this->addPaging($qb, $filters);
return $this->performSearch($qb);
}
$qb->orderBy('id', 'DESC');
return $this->performSearch($qb);
}
/**
* {@inheritdoc}
*/
public function getTotalCount(CollectionFilterParameter $parameters): int
{
$qb = $this->dbResolver->get()->createQueryBuilder();
$qb
->select('COUNT(*) AS total_count')
->from(ApplicationLoggerDb::TABLE_NAME);
$filters = $parameters->getFilters();
if ($filters instanceof FilterParameter) {
$this->addFilters($qb, $filters);
}
try {
$result = $qb->executeQuery()->fetchOne();
return (int) $result;
} catch (Exception $e) {
throw new DatabaseException($e->getMessage(), $e);
}
}
/**
* {@inheritdoc}
*/
public function getComponents(): array
{
$qb = $this->dbResolver->get()->createQueryBuilder();
$qb
->select('component')
->from(ApplicationLoggerDb::TABLE_NAME)
->where($qb->expr()->isNotNull('component'))
->groupBy('component');
try {
return $qb->executeQuery()->fetchFirstColumn();
} catch (Exception $e) {
throw new DatabaseException($e->getMessage(), $e);
}
}
private function addFilters(QueryBuilder $queryBuilder, FilterParameter $filters): void
{
$this->addKeyFilters($queryBuilder, $filters, FilterType::LIKE->value);
$this->addKeyFilters($queryBuilder, $filters, FilterType::EQUALS->value);
$this->addDateFilters($queryBuilder, $filters);
}
private function addKeyFilters(QueryBuilder $queryBuilder, FilterParameter $parameters, string $operation): void
{
$filters = iterator_to_array($parameters->getColumnFilterByType($operation));
if (count($filters) === 0) {
return;
}
$operator = 'LIKE';
if ($operation === FilterType::EQUALS->value) {
$operator = '=';
}
/** @var ColumnFilter $filter */
foreach ($filters as $filter) {
$key = $filter->getKey();
if (!$this->isKeyAllowed($key)) {
continue;
}
$paramName = $key . '_' . $operation;
$paramValue = $filter->getFilterValue();
$paramType = is_int($filter->getFilterValue()) ? Types::INTEGER : Types::STRING;
if ($operation === FilterType::LIKE->value) {
$paramType = Types::STRING;
$paramValue = '%' . $paramValue . '%';
}
$queryBuilder
->andWhere($key . ' ' . $operator . ' :' . $paramName)
->setParameter($paramName, $paramValue, $paramType);
}
}
private function addDateFilters(QueryBuilder $queryBuilder, FilterParameter $parameters): void
{
$filters = iterator_to_array($parameters->getColumnFilterByType(FilterType::DATE->value));
if (count($filters) === 0) {
return;
}
/** @var ColumnFilter $filter */
foreach ($filters as $filter) {
$operation = $filter->getFilterValue()['operator'] ?? '';
$operator = $this->getDateOperation($operation);
if ($operator === null) {
continue;
}
$queryBuilder->andWhere('timestamp ' . $operator . ' :' . $operation . '_date');
$queryBuilder->setParameter(
$operation . '_date',
Carbon::parse($filter->getFilterValue()['value'])->setTimezone('UTC'),
Types::DATETIME_MUTABLE
);
}
}
private function getDateOperation(string $operation): ?string
{
return match ($operation) {
'to' => '<=',
'from' => '>',
default => null
};
}
private function addPaging(QueryBuilder $queryBuilder, FilterParameter $parameters): void
{
$page = $parameters->getPage();
$queryBuilder
->setFirstResult(($page - 1) * $parameters->getPageSize())
->setMaxResults($parameters->getPageSize());
}
private function addSorting(QueryBuilder $queryBuilder, SortFilter $sortFilter): void
{
$queryBuilder
->orderBy(
$this->dbResolver->get()->quoteIdentifier($sortFilter->getKey()),
$sortFilter->getDirection()
);
}
/**
* @throws DatabaseException
*/
private function performSearch(QueryBuilder $queryBuilder): array
{
try {
return $queryBuilder->executeQuery()->fetchAllAssociative();
} catch (Exception $e) {
throw new DatabaseException($e->getMessage(), $e);
}
}
private function isKeyAllowed(string $key): bool
{
if (empty($this->allowedKeys)) {
$this->allowedKeys = FilterableFields::values();
}
return in_array($key, $this->allowedKeys, true);
}
}