Completed
Push — master ( 1758db...0d3940 )
by Jordi Sala
02:09
created

src/Listener/ORM/MediaEventSubscriber.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Listener\ORM;
15
16
use Doctrine\Common\EventArgs;
17
use Doctrine\ORM\Events;
18
use Sonata\ClassificationBundle\Model\CategoryInterface;
19
use Sonata\MediaBundle\Listener\BaseMediaEventSubscriber;
20
use Sonata\MediaBundle\Model\MediaInterface;
21
22
/**
23
 * @final since sonata-project/media-bundle 3.21.0
24
 */
25
class MediaEventSubscriber extends BaseMediaEventSubscriber
26
{
27
    /**
28
     * @var CategoryInterface[]
29
     */
30
    protected $rootCategories;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getSubscribedEvents()
36
    {
37
        return [
38
            Events::prePersist,
39
            Events::preUpdate,
40
            Events::preRemove,
41
            Events::postUpdate,
42
            Events::postRemove,
43
            Events::postPersist,
44
            Events::onClear,
45
        ];
46
    }
47
48
    public function onClear(): void
49
    {
50
        $this->rootCategories = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Son...del\CategoryInterface>> of property $rootCategories.

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

Loading history...
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function recomputeSingleEntityChangeSet(EventArgs $args): void
57
    {
58
        $em = $args->getEntityManager();
59
60
        $em->getUnitOfWork()->recomputeSingleEntityChangeSet(
61
            $em->getClassMetadata(\get_class($args->getEntity())),
62
            $args->getEntity()
63
        );
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getMedia(EventArgs $args)
70
    {
71
        $media = $args->getEntity();
72
73
        if (!$media instanceof MediaInterface) {
74
            return $media;
75
        }
76
77
        if ($this->container->has('sonata.media.manager.category') && !$media->getCategory()) {
78
            $media->setCategory($this->getRootCategory($media));
79
        }
80
81
        return $media;
82
    }
83
84
    /**
85
     * @throws \RuntimeException
86
     *
87
     * @return CategoryInterface
88
     */
89
    protected function getRootCategory(MediaInterface $media)
90
    {
91
        if (!$this->rootCategories) {
92
            $this->rootCategories = $this->container->get('sonata.media.manager.category')->getRootCategories(false);
93
        }
94
95
        if (!\array_key_exists($media->getContext(), $this->rootCategories)) {
96
            throw new \RuntimeException(sprintf('There is no main category related to context: %s', $media->getContext()));
97
        }
98
99
        return $this->rootCategories[$media->getContext()];
100
    }
101
}
102