Test Failed
Push — develop ( 92c10d...ff12cf )
by Daniel
05:05
created

SortableInterfaceSubscriber::prePersist()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 8.0555
c 0
b 0
f 0
cc 9
nc 4
nop 2
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
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $collection is correct as $entity->getSortCollection() targeting Silverback\ApiComponentB...nt::getSortCollection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
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