-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGridSearch.php
More file actions
213 lines (187 loc) · 7.82 KB
/
GridSearch.php
File metadata and controls
213 lines (187 loc) · 7.82 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
<?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\DataIndex\Grid;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\AssetFolder;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DocumentSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\DataObjectQueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\DocumentQueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchIndexFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\DocumentSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\DataObject;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\Type\DataObjectFolder;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Factory\QueryFactoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Filter\MappedParameter\FilterParameter;
use Pimcore\Bundle\StudioBackendBundle\Filter\Service\FilterServiceProviderInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;
use Pimcore\Bundle\StudioBackendBundle\Response\StudioElementInterface;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\UserInterface;
/**
* @internal
*/
final readonly class GridSearch implements GridSearchInterface
{
private SearchIndexFilterInterface $filterService;
public function __construct(
private AssetSearchServiceInterface $assetSearchService,
private DataObjectSearchServiceInterface $dataObjectSearchService,
private DocumentSearchServiceInterface $documentSearchService,
private FilterServiceProviderInterface $filterServiceProvider,
private QueryFactoryInterface $queryFactory,
private SecurityServiceInterface $securityService
) {
$this->filterService = $this->filterServiceProvider->create(SearchIndexFilterInterface::SERVICE_TYPE);
}
/**
* {@inheritdoc}
*/
public function searchAssets(GridParameter $gridParameter): AssetSearchResult
{
return $this->searchElementsForUser(
ElementTypes::TYPE_ASSET,
$gridParameter,
$this->securityService->getCurrentUser()
);
}
/**
* {@inheritdoc}
*/
public function searchAssetsForUser(GridParameter $gridParameter, UserInterface $user): AssetSearchResult
{
return $this->searchElementsForUser(
ElementTypes::TYPE_ASSET,
$gridParameter,
$user
);
}
/**
* {@inheritdoc}
*/
public function searchDataObjects(GridParameter $gridParameter): DataObjectSearchResult
{
return $this->searchElementsForUser(
ElementTypes::TYPE_DATA_OBJECT,
$gridParameter,
$this->securityService->getCurrentUser()
);
}
/**
* {@inheritdoc}
*/
public function searchDocuments(GridParameter $gridParameter): DocumentSearchResult
{
return $this->searchElementsForUser(
ElementTypes::TYPE_DOCUMENT,
$gridParameter,
$this->securityService->getCurrentUser()
);
}
/**
* {@inheritdoc}
*/
public function searchElementsForUser(
string $type,
GridParameter $gridParameter,
UserInterface $user
): AssetSearchResult|DataObjectSearchResult|DocumentSearchResult {
$type = $this->getStudioElementType($type);
/** @var AssetQueryInterface|DataObjectQueryInterface|DocumentQueryInterface $query */
$query = $this->getSearchQuery($type, $gridParameter, $user);
return match($type) {
ElementTypes::TYPE_ASSET => $this->assetSearchService->searchAssets($query),
ElementTypes::TYPE_DATA_OBJECT => $this->dataObjectSearchService->searchDataObjects($query),
ElementTypes::TYPE_DOCUMENT => $this->documentSearchService->searchDocuments($query),
default => throw new InvalidElementTypeException($type)
};
}
/**
* {@inheritdoc}
*/
public function searchElementIdsForUser(
string $type,
GridParameter $gridParameter,
UserInterface $user
): array {
$type = $this->getStudioElementType($type);
/** @var AssetQueryInterface|DataObjectQueryInterface $query */
$query = $this->getSearchQuery($type, $gridParameter, $user);
return match($type) {
ElementTypes::TYPE_ASSET => $this->assetSearchService->fetchAssetIds($query),
ElementTypes::TYPE_DATA_OBJECT => $this->dataObjectSearchService->fetchDataObjectIds($query),
default => throw new InvalidElementTypeException($type)
};
}
private function getSearchQuery(
string $type,
GridParameter $gridParameter,
UserInterface $user
): QueryInterface {
$filter = $gridParameter->getFilters();
$filter = $this->setFilterPath($filter, $type, $gridParameter->getFolderId(), $user);
$query = $this->queryFactory->create($type);
$query = $this->filterService->applyFilters($query, $filter, $type);
$query->setUser($user);
return $query;
}
private function setFilterPath(
FilterParameter $filter,
string $type,
int $folderId,
?UserInterface $user
): FilterParameter {
$folder = match($type) {
ElementTypes::TYPE_ASSET => $this->assetSearchService->getAssetById($folderId, $user),
ElementTypes::TYPE_DATA_OBJECT => $this->dataObjectSearchService->getDataObjectById($folderId, $user),
ElementTypes::TYPE_DOCUMENT => $this->documentSearchService->getDocumentById($folderId, $user),
default => throw new InvalidElementTypeException($type)
};
if (!$this->isFolderOfType($type, $folder)) {
throw new NotFoundException($type . ' Folder', $folderId);
}
$filter->setPath($folder->getFullPath());
return $filter;
}
private function isFolderOfType(string $type, StudioElementInterface $element): bool
{
if ($type === ElementTypes::TYPE_ASSET && $element instanceof AssetFolder) {
return true;
}
if ($type === ElementTypes::TYPE_DATA_OBJECT && $element instanceof DataObjectFolder) {
return true;
}
// We handle Object as folders since they can have child items.
if ($type === ElementTypes::TYPE_DATA_OBJECT && $element instanceof DataObject) {
return true;
}
// Allow all documents as folder since they can all have parent items.
if ($type === ElementTypes::TYPE_DOCUMENT) {
return true;
}
return false;
}
private function getStudioElementType(string $type): string
{
return match (true) {
$type === ElementTypes::TYPE_OBJECT => ElementTypes::TYPE_DATA_OBJECT,
default => $type
};
}
}