1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\EventSubscriber\EntitySubscriber; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
11
|
|
|
use Doctrine\ORM\Events; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Page\Dynamic\DynamicContent; |
13
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableInterface; |
14
|
|
|
|
15
|
|
|
class SortableInterfaceSubscriber implements EntitySubscriberInterface |
16
|
|
|
{ |
17
|
|
|
private $managerRegistry; |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
ManagerRegistry $managerRegistry |
21
|
|
|
) { |
22
|
|
|
$this->managerRegistry = $managerRegistry; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getSubscribedEvents(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
Events::prePersist |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function supportsEntity($entity = null): bool |
36
|
|
|
{ |
37
|
|
|
return $entity instanceof SortableInterface; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param LifecycleEventArgs $eventArgs |
42
|
|
|
* @param SortableInterface $entity |
43
|
|
|
*/ |
44
|
|
|
public function prePersist(/** @scrutinizer ignore-unused */ LifecycleEventArgs $eventArgs, SortableInterface $entity): void |
45
|
|
|
{ |
46
|
|
|
if ( |
47
|
|
|
$entity instanceof DynamicContent && |
48
|
|
|
$entity->getSort() === null && |
49
|
|
|
($collection = $entity->getSortCollection()) === null |
|
|
|
|
50
|
|
|
) { |
51
|
|
|
$resourceClass = \get_class($entity); |
52
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
53
|
|
|
if ($manager) { |
54
|
|
|
$repository = $manager->getRepository($resourceClass); |
55
|
|
|
$collection = new ArrayCollection($repository->findAll()); |
56
|
|
|
if ($manager instanceof EntityManagerInterface) { |
57
|
|
|
$scheduledInsertions = $manager->getUnitOfWork()->getScheduledEntityInsertions(); |
58
|
|
|
foreach ($scheduledInsertions as $scheduledInsertion) { |
59
|
|
|
if ($scheduledInsertion !== $entity && is_a($scheduledInsertion, $resourceClass)) { |
60
|
|
|
$collection->add($scheduledInsertion); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$entity->setSort($entity->calculateSort(true, $collection)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.