Total Complexity | 50 |
Total Lines | 388 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FileAttachmentQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileAttachmentQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class FileAttachmentQueryBuilder |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public const BUSINESS_UNIT_IDS_COLUMN = 'businessUnitIds'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public const COMPANY_IDS_COLUMN = 'companyIds'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | public const COMPANY_USER_IDS_COLUMN = 'companyUserIds'; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public const SSP_ASSET_IDS_COLUMN = 'sspAssetIds'; |
||
45 | |||
46 | public function applyCriteria(SpyFileQuery $fileQuery, FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer): SpyFileQuery |
||
47 | { |
||
48 | $fileQuery = $this->applyFileIdFilter($fileQuery, $fileAttachmentCriteriaTransfer); |
||
49 | $fileQuery = $this->applyFileAttachmentUuidFilter($fileQuery, $fileAttachmentCriteriaTransfer); |
||
50 | $fileQuery = $this->addRelationsToAQuery($fileQuery, $fileAttachmentCriteriaTransfer); |
||
51 | $fileQuery = $this->applyFileAttachmentSearch($fileQuery, $fileAttachmentCriteriaTransfer); |
||
52 | $fileQuery = $this->applyFileAttachmentTypeFilter($fileQuery, $fileAttachmentCriteriaTransfer); |
||
53 | $fileQuery = $this->applyFileAttachmentDateRangeFilter($fileQuery, $fileAttachmentCriteriaTransfer); |
||
54 | |||
55 | /** @var \ArrayObject<array-key, \Generated\Shared\Transfer\SortTransfer> $sortTransfers */ |
||
56 | $sortTransfers = $fileAttachmentCriteriaTransfer->getSortCollection(); |
||
57 | $this->applyFileAttachmentSorting($fileQuery, $sortTransfers); |
||
58 | |||
59 | return $fileQuery; |
||
60 | } |
||
61 | |||
62 | protected function addRelationsToAQuery(SpyFileQuery $fileQuery, FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer): SpyFileQuery |
||
63 | { |
||
64 | if ( |
||
65 | !$fileAttachmentCriteriaTransfer->getWithBusinessUnitRelation() && |
||
66 | !$fileAttachmentCriteriaTransfer->getWithCompanyUserRelation() && |
||
67 | !$fileAttachmentCriteriaTransfer->getWithSspAssetRelation() |
||
68 | ) { |
||
69 | return $fileQuery; |
||
70 | } |
||
71 | |||
72 | $fileAttachmentConditions = $fileAttachmentCriteriaTransfer->getFileAttachmentConditions() ?? new FileAttachmentConditionsTransfer(); |
||
73 | |||
74 | $fileAttachmentFilterCriteria = null; |
||
75 | if ($fileAttachmentCriteriaTransfer->getWithBusinessUnitRelation()) { |
||
76 | $fileQuery |
||
77 | ->withColumn('GROUP_CONCAT(' . SpyCompanyBusinessUnitFileTableMap::COL_FK_COMPANY_BUSINESS_UNIT . ')', static::BUSINESS_UNIT_IDS_COLUMN) |
||
78 | ->joinSpyCompanyBusinessUnitFile(null, Criteria::LEFT_JOIN); |
||
79 | |||
80 | $businessUnitFileCriteria = $this->getBusinessUnitFileCriteria($fileQuery, $fileAttachmentConditions); |
||
81 | |||
82 | $fileAttachmentFilterCriteria = $this->addSubCriteria($businessUnitFileCriteria, $fileAttachmentFilterCriteria); |
||
83 | } |
||
84 | |||
85 | if ($fileAttachmentCriteriaTransfer->getWithCompanyUserRelation()) { |
||
86 | $fileQuery |
||
87 | ->withColumn('GROUP_CONCAT(' . SpyCompanyUserFileTableMap::COL_FK_COMPANY_USER . ')', static::COMPANY_USER_IDS_COLUMN) |
||
88 | ->joinSpyCompanyUserFile(null, Criteria::LEFT_JOIN); |
||
89 | |||
90 | $companyUserFilterCriteria = $this->getCompanyUserFileCriteria($fileQuery, $fileAttachmentConditions); |
||
91 | |||
92 | if ($companyUserFilterCriteria) { |
||
93 | $fileAttachmentFilterCriteria = $this->addSubCriteria($companyUserFilterCriteria, $fileAttachmentFilterCriteria); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if ($fileAttachmentCriteriaTransfer->getWithSspAssetRelation()) { |
||
98 | $fileQuery |
||
99 | ->withColumn('GROUP_CONCAT(' . SpySspAssetFileTableMap::COL_FK_SSP_ASSET . ')', static::SSP_ASSET_IDS_COLUMN) |
||
100 | ->useSpySspAssetFileQuery(null, Criteria::LEFT_JOIN); |
||
101 | |||
102 | $sspAssetFilterCriteria = $this->applySspAssetFileCriteria($fileQuery, $fileAttachmentCriteriaTransfer); |
||
103 | |||
104 | if ($sspAssetFilterCriteria) { |
||
105 | $fileAttachmentFilterCriteria = $this->addSubCriteria($sspAssetFilterCriteria, $fileAttachmentFilterCriteria); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (!$fileAttachmentFilterCriteria) { |
||
110 | return $fileQuery; |
||
111 | } |
||
112 | |||
113 | $fileQuery->addAnd($fileAttachmentFilterCriteria); |
||
114 | |||
115 | return $fileQuery; |
||
116 | } |
||
117 | |||
118 | protected function getBusinessUnitFileCriteria( |
||
119 | SpyFileQuery $fileQuery, |
||
120 | FileAttachmentConditionsTransfer $fileAttachmentConditionsTransfer |
||
121 | ): ?AbstractCriterion { |
||
122 | $businessUnitQuery = $fileQuery |
||
123 | ->useSpyCompanyBusinessUnitFileQuery(null, Criteria::LEFT_JOIN) |
||
124 | ->useCompanyBusinessUnitQuery(null, Criteria::LEFT_JOIN); |
||
125 | |||
126 | $businessUnitQuery->endUse()->endUse(); |
||
127 | |||
128 | $criteria = null; |
||
129 | if ($fileAttachmentConditionsTransfer->getCompanyIds()) { |
||
130 | $criteria = $businessUnitQuery->getNewCriterion( |
||
131 | SpyCompanyBusinessUnitTableMap::COL_FK_COMPANY, |
||
132 | $fileAttachmentConditionsTransfer->getCompanyIds(), |
||
133 | Criteria::IN, |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | if ($fileAttachmentConditionsTransfer->getBusinessUnitIds()) { |
||
138 | $businessUnitIdFileCriteria = $businessUnitQuery->getNewCriterion( |
||
139 | SpyCompanyBusinessUnitTableMap::COL_ID_COMPANY_BUSINESS_UNIT, |
||
140 | $fileAttachmentConditionsTransfer->getBusinessUnitIds(), |
||
141 | Criteria::IN, |
||
142 | ); |
||
143 | |||
144 | if ($criteria) { |
||
145 | $criteria->addAnd($businessUnitIdFileCriteria); |
||
146 | |||
147 | return $criteria; |
||
148 | } |
||
149 | |||
150 | return $businessUnitIdFileCriteria; |
||
151 | } |
||
152 | |||
153 | if ($fileAttachmentConditionsTransfer->getBusinessUnitUuids()) { |
||
154 | $businessUnitUuidCriteria = $businessUnitQuery->getNewCriterion( |
||
155 | SpyCompanyBusinessUnitTableMap::COL_UUID, |
||
156 | $fileAttachmentConditionsTransfer->getBusinessUnitUuids(), |
||
157 | Criteria::IN, |
||
158 | ); |
||
159 | |||
160 | if ($criteria) { |
||
161 | $criteria->addAnd($businessUnitUuidCriteria); |
||
162 | |||
163 | return $criteria; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return $criteria; |
||
168 | } |
||
169 | |||
170 | protected function getCompanyUserFileCriteria( |
||
171 | SpyFileQuery $fileQuery, |
||
172 | FileAttachmentConditionsTransfer $fileAttachmentConditionsTransfer |
||
173 | ): ?AbstractCriterion { |
||
174 | $companyUserFileQuery = $fileQuery |
||
175 | ->useSpyCompanyUserFileQuery(null, Criteria::LEFT_JOIN); |
||
176 | |||
177 | $companyUserQuery = $companyUserFileQuery->useCompanyUserQuery('company_user', Criteria::LEFT_JOIN); |
||
178 | |||
179 | $companyUserFileQuery->endUse(); |
||
180 | |||
181 | $criteria = null; |
||
182 | if ($fileAttachmentConditionsTransfer->getCompanyUserIds()) { |
||
183 | $criteria = $companyUserFileQuery->getNewCriterion( |
||
184 | SpyCompanyUserFileTableMap::COL_FK_COMPANY_USER, |
||
185 | $fileAttachmentConditionsTransfer->getCompanyUserIds(), |
||
186 | Criteria::IN, |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | if ($fileAttachmentConditionsTransfer->getCompanyIds()) { |
||
191 | $companyIdCriteria = $companyUserQuery->getNewCriterion( |
||
192 | 'company_user.fk_company', |
||
193 | $fileAttachmentConditionsTransfer->getCompanyIds(), |
||
194 | Criteria::IN, |
||
195 | ); |
||
196 | |||
197 | if ($criteria) { |
||
198 | $companyIdCriteria->addAnd($criteria); |
||
199 | } |
||
200 | |||
201 | if (!$criteria) { |
||
202 | $criteria = $companyIdCriteria; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if ($fileAttachmentConditionsTransfer->getBusinessUnitIds()) { |
||
207 | $businessUnitIdCriteria = $companyUserQuery->getNewCriterion( |
||
208 | 'company_user.fk_company_business_unit', |
||
209 | $fileAttachmentConditionsTransfer->getBusinessUnitIds(), |
||
210 | Criteria::IN, |
||
211 | ); |
||
212 | |||
213 | if ($criteria) { |
||
214 | $criteria->addAnd($businessUnitIdCriteria); |
||
215 | |||
216 | return $criteria; |
||
217 | } |
||
218 | |||
219 | return $businessUnitIdCriteria; |
||
220 | } |
||
221 | |||
222 | return $criteria; |
||
223 | } |
||
224 | |||
225 | protected function applySspAssetFileCriteria(SpyFileQuery $fileQuery, FileAttachmentCriteriaTransfer $criteriaTransfer): ?AbstractCriterion |
||
226 | { |
||
227 | $fileAttachmentCriteriaTransfer = $criteriaTransfer->getFileAttachmentConditionsOrFail(); |
||
228 | |||
229 | $sspAssetQuery = $fileQuery |
||
230 | ->useSpySspAssetFileQuery(null, Criteria::LEFT_JOIN) |
||
231 | ->useSspAssetQuery(null, Criteria::LEFT_JOIN); |
||
232 | |||
233 | $companyBusinessUnitQuery = $sspAssetQuery |
||
234 | ->useSpySspAssetToCompanyBusinessUnitQuery(null, Criteria::LEFT_JOIN) |
||
235 | ->joinSpyCompanyBusinessUnit('ssp_asset_business_unit', Criteria::LEFT_JOIN); |
||
236 | |||
237 | $companyBusinessUnitQuery |
||
238 | ->endUse() |
||
239 | ->endUse() |
||
240 | ->endUse(); |
||
241 | |||
242 | $criteria = null; |
||
243 | if ($fileAttachmentCriteriaTransfer->getAssetReferences()) { |
||
244 | $criteria = $sspAssetQuery->getNewCriterion( |
||
245 | SpySspAssetTableMap::COL_REFERENCE, |
||
246 | $fileAttachmentCriteriaTransfer->getAssetReferences(), |
||
247 | Criteria::IN, |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | if ($fileAttachmentCriteriaTransfer->getSspAssetCompanyIds()) { |
||
252 | $assetCompanyIdCriterion = $fileQuery->getNewCriterion( |
||
253 | 'ssp_asset_business_unit.fk_company', |
||
254 | $fileAttachmentCriteriaTransfer->getSspAssetCompanyIds(), |
||
255 | Criteria::IN, |
||
256 | ); |
||
257 | |||
258 | if ($criteria) { |
||
259 | $criteria->addAnd($assetCompanyIdCriterion); |
||
260 | } else { |
||
261 | $criteria = $assetCompanyIdCriterion; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | if ($fileAttachmentCriteriaTransfer->getSspAssetBusinessUnitIds()) { |
||
266 | $assetBusinessUnitIdCriterion = $fileQuery->getNewCriterion( |
||
267 | 'ssp_asset_business_unit.id_company_business_unit', |
||
268 | $fileAttachmentCriteriaTransfer->getSspAssetBusinessUnitIds(), |
||
269 | Criteria::IN, |
||
270 | ); |
||
271 | |||
272 | if ($criteria) { |
||
273 | $criteria->addAnd($assetBusinessUnitIdCriterion); |
||
274 | } else { |
||
275 | $criteria = $assetBusinessUnitIdCriterion; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return $criteria; |
||
280 | } |
||
281 | |||
282 | protected function applyFileAttachmentSearch( |
||
283 | SpyFileQuery $query, |
||
284 | FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer |
||
285 | ): SpyFileQuery { |
||
286 | if (!$fileAttachmentCriteriaTransfer->getFileAttachmentSearchConditions()) { |
||
287 | return $query; |
||
288 | } |
||
289 | |||
290 | $searchString = $fileAttachmentCriteriaTransfer->getFileAttachmentSearchConditions()->getSearchString(); |
||
291 | if ($searchString) { |
||
292 | $query->filterByFileName_Like(sprintf('%%%s%%', $searchString)) |
||
293 | ->_or() |
||
294 | ->filterByFileReference_Like(sprintf('%%%s%%', $searchString)); |
||
295 | } |
||
296 | |||
297 | return $query; |
||
298 | } |
||
299 | |||
300 | protected function applyFileAttachmentTypeFilter( |
||
301 | SpyFileQuery $query, |
||
302 | FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer |
||
303 | ): SpyFileQuery { |
||
304 | $fileTypes = $fileAttachmentCriteriaTransfer->getFileAttachmentConditionsOrFail()->getFileTypes(); |
||
305 | |||
306 | if (!$fileTypes) { |
||
307 | return $query; |
||
308 | } |
||
309 | |||
310 | return $query |
||
311 | ->useSpyFileInfoQuery() |
||
312 | ->filterByExtension_In($fileTypes) |
||
313 | ->endUse(); |
||
314 | } |
||
315 | |||
316 | protected function applyFileAttachmentDateRangeFilter( |
||
317 | SpyFileQuery $query, |
||
318 | FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer |
||
319 | ): SpyFileQuery { |
||
320 | $rangeCreatedAt = $fileAttachmentCriteriaTransfer->getFileAttachmentConditionsOrFail()->getRangeCreatedAt(); |
||
321 | if (!$rangeCreatedAt) { |
||
322 | return $query; |
||
323 | } |
||
324 | |||
325 | if ($rangeCreatedAt->getFrom()) { |
||
326 | $query->useSpyFileInfoQuery() |
||
327 | ->filterByCreatedAt($rangeCreatedAt->getFrom(), Criteria::GREATER_EQUAL) |
||
328 | ->endUse(); |
||
329 | } |
||
330 | |||
331 | if ($rangeCreatedAt->getTo()) { |
||
332 | $query->useSpyFileInfoQuery() |
||
333 | ->filterByCreatedAt($rangeCreatedAt->getTo(), Criteria::LESS_EQUAL) |
||
334 | ->endUse(); |
||
335 | } |
||
336 | |||
337 | return $query; |
||
338 | } |
||
339 | |||
340 | protected function applyFileAttachmentUuidFilter( |
||
341 | SpyFileQuery $query, |
||
342 | FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer |
||
343 | ): SpyFileQuery { |
||
344 | $uuids = $fileAttachmentCriteriaTransfer->getFileAttachmentConditionsOrFail()->getUuids(); |
||
345 | if ($uuids !== []) { |
||
346 | $query |
||
347 | ->filterByUuid_In($uuids); |
||
348 | } |
||
349 | |||
350 | return $query; |
||
351 | } |
||
352 | |||
353 | protected function applyFileIdFilter( |
||
354 | SpyFileQuery $query, |
||
355 | FileAttachmentCriteriaTransfer $fileAttachmentCriteriaTransfer |
||
356 | ): SpyFileQuery { |
||
357 | $fileIds = $fileAttachmentCriteriaTransfer->getFileAttachmentConditionsOrFail()->getIdFiles(); |
||
358 | if ($fileIds !== []) { |
||
359 | $query |
||
360 | ->filterByIdFile_In($fileIds); |
||
361 | } |
||
362 | |||
363 | return $query; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @param \Propel\Runtime\ActiveQuery\ModelCriteria $query |
||
368 | * @param \ArrayObject<array-key, \Generated\Shared\Transfer\SortTransfer> $sortTransfers |
||
369 | * |
||
370 | * @return \Propel\Runtime\ActiveQuery\ModelCriteria |
||
371 | */ |
||
372 | protected function applyFileAttachmentSorting(ModelCriteria $query, ArrayObject $sortTransfers): ModelCriteria |
||
373 | { |
||
374 | $fileAttachmentSortFieldMapping = $this->getFileAttachmentSortFieldMapping(); |
||
375 | foreach ($sortTransfers as $sortTransfer) { |
||
376 | $query |
||
377 | ->groupBy($fileAttachmentSortFieldMapping[$sortTransfer->getFieldOrFail()] ?? $sortTransfer->getFieldOrFail()) |
||
378 | ->orderBy( |
||
379 | $fileAttachmentSortFieldMapping[$sortTransfer->getFieldOrFail()] ?? $sortTransfer->getFieldOrFail(), |
||
380 | $sortTransfer->getIsAscending() ? Criteria::ASC : Criteria::DESC, |
||
381 | ); |
||
382 | } |
||
383 | |||
384 | return $query; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @return array<string, string> |
||
389 | */ |
||
390 | protected function getFileAttachmentSortFieldMapping(): array |
||
396 | ]; |
||
397 | } |
||
398 | |||
399 | protected function addSubCriteria(?AbstractCriterion $subCriteria, ?AbstractCriterion $criteria): ?AbstractCriterion |
||
400 | { |
||
401 | if (!$subCriteria) { |
||
412 | } |
||
413 | } |
||
414 |