Total Complexity | 114 |
Total Lines | 628 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like DocumentVoter 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 DocumentVoter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DocumentVoter extends Voter |
||
26 | { |
||
27 | const CREATE = "DOCUMENT_CREATE"; |
||
28 | const CREATE_ANONYMOUSLY = "DOCUMENT_CREATE_ANONYMOUSLY"; |
||
29 | const UPDATE = "DOCUMENT_UPDATE"; |
||
30 | const LIST = "DOCUMENT_LIST"; |
||
31 | const LIST_REGISTERED = "DOCUMENT_LIST_REGISTERED"; |
||
32 | const LIST_IN_PROGRESS = "DOCUMENT_LIST_IN_PROGRESS"; |
||
33 | |||
34 | const DISCARD = "DOCUMENT_DISCARD"; |
||
35 | |||
36 | const DELETE_LOCALLY = "DOCUMENT_DELETE_LOCALLY"; |
||
37 | const DELETE_WORKING_COPY = "DOCUMENT_DELETE_WORKING_COPY"; |
||
38 | const DUPLICATE = "DOCUMENT_DUPLICATE"; |
||
39 | const RELEASE_PUBLISH = "DOCUMENT_RELEASE_PUBLISH"; |
||
40 | const RELEASE_ACTIVATE = "DOCUMENT_RELEASE_ACTIVATE"; |
||
41 | const REGISTER = "DOCUMENT_REGISTER"; |
||
42 | const SHOW_DETAILS = "DOCUMENT_SHOW_DETAILS"; |
||
43 | const CANCEL_LIST_TASK = "DOCUMENT_CANCEL_LIST_TASK"; |
||
44 | const UPLOAD_FILES = "DOCUMENT_UPLOAD_FILES"; |
||
45 | const EDIT = "DOCUMENT_EDIT"; |
||
46 | const POSTPONE = "DOCUMENT_POSTPONE"; |
||
47 | const DOUBLET_CHECK = "DOCUMENT_DOUBLET_CHECK"; |
||
48 | const SUGGEST_RESTORE = "DOCUMENT_SUGGEST_RESTORE"; |
||
49 | const SUGGEST_MODIFICATION = "DOCUMENT_SUGGEST_MODIFICATION"; |
||
50 | const SUGGESTION_ACCEPT = "DOCUMENT_SUGGESTION_ACCEPT"; |
||
51 | const EDIT_ANONYMOUSLY = "DOCUMENT_EDIT_ANONYMOUSLY"; |
||
52 | const REGISTER_ANONYMOUSLY = "DOCUMENT_REGISTER_ANONYMOUSLY"; |
||
53 | const DELETE_ANONYMOUSLY = "DOCUMENT_DELETE_ANONYMOUSLY"; |
||
54 | |||
55 | /** |
||
56 | * editingLockService |
||
57 | * |
||
58 | * @var \EWW\Dpf\Services\Document\EditingLockService |
||
59 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
60 | */ |
||
61 | protected $editingLockService = null; |
||
62 | |||
63 | /** |
||
64 | * workflow |
||
65 | * |
||
66 | * @var DocumentWorkflow |
||
67 | */ |
||
68 | protected $workflow; |
||
69 | |||
70 | public function __construct() |
||
71 | { |
||
72 | $this->workflow = DocumentWorkflow::getWorkflow(); |
||
|
|||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Returns all supported attributes. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | protected static function getAttributes() |
||
82 | { |
||
83 | return array( |
||
84 | self::CREATE, |
||
85 | self::CREATE_ANONYMOUSLY, |
||
86 | self::UPDATE, |
||
87 | self::LIST, |
||
88 | self::LIST_REGISTERED, |
||
89 | self::LIST_IN_PROGRESS, |
||
90 | self::DISCARD, |
||
91 | self::DELETE_LOCALLY, |
||
92 | self::DELETE_WORKING_COPY, |
||
93 | self::DUPLICATE, |
||
94 | self::RELEASE_PUBLISH, |
||
95 | self::RELEASE_ACTIVATE, |
||
96 | self::REGISTER, |
||
97 | self::SHOW_DETAILS, |
||
98 | self::CANCEL_LIST_TASK, |
||
99 | self::UPLOAD_FILES, |
||
100 | self::EDIT, |
||
101 | self::POSTPONE, |
||
102 | self::DOUBLET_CHECK, |
||
103 | self::SUGGEST_RESTORE, |
||
104 | self::SUGGEST_MODIFICATION, |
||
105 | self::SUGGESTION_ACCEPT, |
||
106 | self::EDIT_ANONYMOUSLY, |
||
107 | self::REGISTER_ANONYMOUSLY, |
||
108 | self::DELETE_ANONYMOUSLY |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Determines if the voter supports the given attribute. |
||
115 | * |
||
116 | * @param string $attribute |
||
117 | * @param mixed $subject |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public static function supports($attribute, $subject = NULL) |
||
121 | { |
||
122 | if (!in_array($attribute, self::getAttributes())) { |
||
123 | return FALSE; |
||
124 | } |
||
125 | |||
126 | if (!$subject instanceof Document && !is_null($subject)) { |
||
127 | return FALSE; |
||
128 | } |
||
129 | |||
130 | return TRUE; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Determines if access for the given attribute and subject is allowed. |
||
135 | * |
||
136 | * @param string $attribute |
||
137 | * @param mixed $subject |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function voteOnAttribute($attribute, $subject = NULL) |
||
141 | { |
||
142 | if (!$subject instanceof Document) { |
||
143 | return FALSE; |
||
144 | } |
||
145 | |||
146 | switch ($attribute) { |
||
147 | |||
148 | case self::CREATE: |
||
149 | return $this->defaultAccess($subject); |
||
150 | break; |
||
151 | |||
152 | case self::CREATE_ANONYMOUSLY: |
||
153 | return $this->canCreateAnonymously($subject); |
||
154 | break; |
||
155 | |||
156 | case self::UPDATE: |
||
157 | return $this->canUpdate($subject); |
||
158 | break; |
||
159 | |||
160 | case self::LIST: |
||
161 | return $this->defaultAccess(); |
||
162 | break; |
||
163 | |||
164 | case self::LIST_REGISTERED: |
||
165 | return $this->defaultAccess(); |
||
166 | break; |
||
167 | |||
168 | case self::LIST_IN_PROGRESS: |
||
169 | return $this->defaultAccess(); |
||
170 | break; |
||
171 | |||
172 | case self::DISCARD: |
||
173 | return $this->canDiscard($subject); |
||
174 | break; |
||
175 | |||
176 | case self::DELETE_LOCALLY: |
||
177 | return $this->canDeleteLocally($subject); |
||
178 | break; |
||
179 | |||
180 | case self::DELETE_WORKING_COPY: |
||
181 | return $this->canDeleteWorkingCopy($subject); |
||
182 | break; |
||
183 | |||
184 | case self::DUPLICATE: |
||
185 | return $this->canDuplicate($subject); |
||
186 | break; |
||
187 | |||
188 | case self::RELEASE_PUBLISH: |
||
189 | return $this->canReleasePublish($subject); |
||
190 | break; |
||
191 | |||
192 | case self::RELEASE_ACTIVATE: |
||
193 | return $this->canReleaseActivate($subject); |
||
194 | break; |
||
195 | |||
196 | case self::REGISTER: |
||
197 | return $this->canRegister($subject); |
||
198 | break; |
||
199 | |||
200 | case self::SHOW_DETAILS: |
||
201 | return $this->canShowDetails($subject); |
||
202 | |||
203 | case self::EDIT_ANONYMOUSLY: |
||
204 | return $this->canEditAnonymously($subject); |
||
205 | break; |
||
206 | |||
207 | case self::CANCEL_LIST_TASK: |
||
208 | return $this->defaultAccess(); |
||
209 | break; |
||
210 | |||
211 | case self::UPLOAD_FILES: |
||
212 | return $this->canUpdate($subject); |
||
213 | break; |
||
214 | |||
215 | case self::EDIT: |
||
216 | return $this->canEdit($subject); |
||
217 | break; |
||
218 | |||
219 | case self::POSTPONE: |
||
220 | return $this->canPostpone($subject); |
||
221 | break; |
||
222 | |||
223 | case self::DOUBLET_CHECK: |
||
224 | return $this->librarianOnly(); |
||
225 | break; |
||
226 | |||
227 | case self::SUGGEST_RESTORE: |
||
228 | return $this->canSuggestRestore($subject); |
||
229 | break; |
||
230 | |||
231 | case self::SUGGEST_MODIFICATION: |
||
232 | return $this->canSuggestModification($subject); |
||
233 | break; |
||
234 | |||
235 | case self::SUGGESTION_ACCEPT: |
||
236 | return $this->canSuggestionAccept($subject); |
||
237 | break; |
||
238 | |||
239 | case self::REGISTER_ANONYMOUSLY: |
||
240 | return $this->canRegisterAnonymously($subject); |
||
241 | break; |
||
242 | |||
243 | case self::DELETE_ANONYMOUSLY: |
||
244 | return $this->canEditAnonymously($subject); |
||
245 | break; |
||
246 | } |
||
247 | |||
248 | /** @var $logger \TYPO3\CMS\Core\Log\Logger */ |
||
249 | $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); |
||
250 | $logger->warning('DocumentVoter::voteOnAttribute(): Unknown attribute. This code should not be reached!', |
||
251 | [ |
||
252 | 'attribute' => $attribute |
||
253 | ] |
||
254 | ); |
||
255 | |||
256 | return false; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @return bool |
||
261 | */ |
||
262 | protected function defaultAccess() |
||
263 | { |
||
264 | return ( |
||
265 | $this->security->getUserRole() === Security::ROLE_LIBRARIAN || |
||
266 | $this->security->getUserRole() === Security::ROLE_RESEARCHER |
||
267 | ); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return bool |
||
272 | */ |
||
273 | protected function librarianOnly() |
||
274 | { |
||
275 | return $this->security->getUserRole() === Security::ROLE_LIBRARIAN; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
280 | * @return bool |
||
281 | */ |
||
282 | protected function canDiscard($document) |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
304 | * @return bool |
||
305 | */ |
||
306 | protected function canShowDetails($document) |
||
307 | { |
||
308 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN) { |
||
309 | return ( |
||
310 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE || |
||
311 | $document->getCreator() === $this->security->getUser()->getUid() |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | if ($this->security->getUserRole() === Security::ROLE_RESEARCHER) { |
||
316 | return ( |
||
317 | $document->getCreator() === $this->security->getUser()->getUid() || |
||
318 | ( |
||
319 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE && |
||
320 | $document->getState() !== DocumentWorkflow::STATE_DISCARDED_NONE && |
||
321 | $document->getState() !== DocumentWorkflow::STATE_NONE_DELETED |
||
322 | ) |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | return FALSE; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
331 | * @return bool |
||
332 | */ |
||
333 | protected function canRegister($document) |
||
334 | { |
||
335 | if ( |
||
336 | $this->workflow->can($document, \EWW\Dpf\Domain\Workflow\DocumentWorkflow::TRANSITION_REGISTER) && |
||
337 | $document->getCreator() === $this->security->getUser()->getUid() |
||
338 | ) { |
||
339 | return TRUE; |
||
340 | } |
||
341 | |||
342 | return FALSE; |
||
343 | } |
||
344 | |||
345 | |||
346 | /** |
||
347 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
348 | * @return bool |
||
349 | */ |
||
350 | protected function canReleasePublish($document) |
||
351 | { |
||
352 | if ($this->isDocumentLocked($document)) { |
||
353 | return FALSE; |
||
354 | } |
||
355 | |||
356 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN) { |
||
357 | return $this->workflow->can($document, |
||
358 | \EWW\Dpf\Domain\Workflow\DocumentWorkflow::TRANSITION_RELEASE_PUBLISH); |
||
359 | } |
||
360 | |||
361 | return FALSE; |
||
362 | } |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
367 | * @return bool |
||
368 | */ |
||
369 | protected function canReleaseActivate($document) |
||
370 | { |
||
371 | if ($this->isDocumentLocked($document)) { |
||
372 | return FALSE; |
||
373 | } |
||
374 | |||
375 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN) { |
||
376 | return $this->workflow->can($document, |
||
377 | \EWW\Dpf\Domain\Workflow\DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE); |
||
378 | } |
||
379 | |||
380 | return FALSE; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
385 | * @return bool |
||
386 | */ |
||
387 | protected function canDeleteLocally($document) |
||
406 | } |
||
407 | |||
408 | |||
409 | /** |
||
410 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
411 | * @return bool |
||
412 | */ |
||
413 | protected function canDeleteWorkingCopy($document) |
||
414 | { |
||
415 | if ($document->isTemporary() || $this->isDocumentLocked($document)) { |
||
416 | return FALSE; |
||
417 | } |
||
418 | |||
419 | if ($this->workflow->can($document, \EWW\Dpf\Domain\Workflow\DocumentWorkflow::TRANSITION_DELETE_WORKING_COPY)) { |
||
420 | return $this->security->getUserRole() === Security::ROLE_LIBRARIAN; |
||
421 | } |
||
422 | |||
423 | return FALSE; |
||
424 | } |
||
425 | |||
426 | |||
427 | /** |
||
428 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function canEdit($document) |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
457 | * @return bool |
||
458 | */ |
||
459 | protected function canUpdate($document) |
||
460 | { |
||
461 | if ($this->isDocumentLocked($document)) { |
||
462 | return FALSE; |
||
463 | } |
||
464 | |||
465 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN) { |
||
466 | return ( |
||
467 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE || |
||
468 | $document->getCreator() === $this->security->getUser()->getUid() |
||
469 | ); |
||
470 | } |
||
471 | |||
472 | |||
473 | if ($document->getCreator() === $this->security->getUser()->getUid()) { |
||
474 | return ( |
||
475 | $document->getState() === DocumentWorkflow::STATE_NEW_NONE || |
||
476 | $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE |
||
477 | ); |
||
478 | } |
||
479 | |||
480 | return false; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
485 | * @return bool |
||
486 | */ |
||
487 | protected function canSuggestRestore($document) |
||
488 | { |
||
489 | if ($this->security->getUserRole() === Security::ROLE_RESEARCHER) { |
||
490 | |||
491 | $objectManager =GeneralUtility::makeInstance(ObjectManager::class); |
||
492 | $documentRepository = $objectManager->get(DocumentRepository::class); |
||
493 | |||
494 | $linkedDocument = $documentRepository->findOneByLinkedUid($document->getUid()); |
||
495 | |||
496 | if (!$linkedDocument && $document->getObjectIdentifier()) { |
||
497 | $linkedDocument = $documentRepository->findOneByLinkedUid($document->getObjectIdentifier()); |
||
498 | } |
||
499 | |||
500 | return ( |
||
501 | $document->getState() === DocumentWorkflow::STATE_DISCARDED_NONE || |
||
502 | $document->getState() === DocumentWorkflow::STATE_NONE_DELETED |
||
503 | ) && !$linkedDocument; |
||
504 | } |
||
505 | |||
506 | return FALSE; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
511 | * @return bool |
||
512 | */ |
||
513 | protected function canSuggestModification($document) |
||
514 | { |
||
515 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
516 | $documentRepository = $objectManager->get(DocumentRepository::class); |
||
517 | |||
518 | $linkedDocument = $documentRepository->findOneByLinkedUid($document->getUid()); |
||
519 | if (!$linkedDocument && $document->getObjectIdentifier()) { |
||
520 | $linkedDocument = $documentRepository->findOneByLinkedUid($document->getObjectIdentifier()); |
||
521 | } |
||
522 | |||
523 | if ($this->security->getUserRole() === Security::ROLE_RESEARCHER) { |
||
524 | return ( |
||
525 | ( |
||
526 | $document->getCreator() !== $this->security->getUser()->getUid() && |
||
527 | $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE |
||
528 | ) || |
||
529 | ( |
||
530 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE && |
||
531 | $document->getState() !== DocumentWorkflow::STATE_REGISTERED_NONE |
||
532 | ) |
||
533 | ) && !$linkedDocument; |
||
534 | } |
||
535 | |||
536 | return FALSE; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
541 | * @return bool |
||
542 | */ |
||
543 | protected function canSuggestionAccept($document) |
||
544 | { |
||
545 | // TODO: What if a document should be restored? |
||
546 | |||
547 | return $this->librarianOnly(); |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
552 | * @return bool |
||
553 | */ |
||
554 | protected function canPostpone($document) |
||
555 | { |
||
556 | if ($this->isDocumentLocked($document)) { |
||
557 | return FALSE; |
||
558 | } |
||
559 | |||
560 | if ($this->workflow->can($document, \EWW\Dpf\Domain\Workflow\DocumentWorkflow::TRANSITION_POSTPONE)) { |
||
561 | return $this->security->getUserRole() === Security::ROLE_LIBRARIAN; |
||
562 | } |
||
563 | |||
564 | return FALSE; |
||
565 | } |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
570 | * @return bool |
||
571 | */ |
||
572 | protected function canCreateAnonymously($document) |
||
573 | { |
||
574 | if ($this->security->getUserRole()) { |
||
575 | return FALSE; |
||
576 | } |
||
577 | |||
578 | return TRUE; |
||
579 | } |
||
580 | |||
581 | /** |
||
582 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
583 | * @return bool |
||
584 | */ |
||
585 | protected function canDuplicate($document) |
||
586 | { |
||
587 | if ($this->security->getUserRole() === Security::ROLE_LIBRARIAN) { |
||
588 | return ( |
||
589 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE || |
||
590 | $document->getCreator() === $this->security->getUser()->getUid() |
||
591 | ); |
||
592 | } |
||
593 | |||
594 | if ($this->security->getUserRole() === Security::ROLE_RESEARCHER) { |
||
595 | return ( |
||
596 | $document->getCreator() === $this->security->getUser()->getUid() || |
||
597 | $document->getState() !== DocumentWorkflow::STATE_NEW_NONE |
||
598 | ); |
||
599 | } |
||
600 | |||
601 | return FALSE; |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
606 | * @return bool |
||
607 | */ |
||
608 | protected function isDocumentLocked($document) |
||
609 | { |
||
610 | $identifier = $document->getObjectIdentifier()? $document->getObjectIdentifier() : $document->getUid(); |
||
611 | return $this->editingLockService->isLocked($identifier, $this->security->getUser()->getUid()); |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
616 | * @return bool |
||
617 | */ |
||
618 | protected function canEditAnonymously($document) |
||
619 | { |
||
620 | if ($this->security->getUserRole()) { |
||
621 | return FALSE; |
||
622 | } |
||
623 | |||
624 | return ( |
||
625 | $document->getCreator() === 0 |
||
626 | && $document->isTemporary() |
||
627 | && $document->getState() === DocumentWorkflow::STATE_NEW_NONE |
||
628 | ); |
||
629 | |||
630 | return FALSE; |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * @param \EWW\Dpf\Domain\Model\Document $document |
||
635 | * @return bool |
||
636 | */ |
||
637 | protected function canRegisterAnonymously($document) |
||
653 | } |
||
654 | |||
655 | } |
||
656 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..