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
|
|
|
public function getSubscribedEvents() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
Events::prePersist, |
36
|
|
|
Events::preUpdate, |
37
|
|
|
Events::preRemove, |
38
|
|
|
Events::postUpdate, |
39
|
|
|
Events::postRemove, |
40
|
|
|
Events::postPersist, |
41
|
|
|
Events::onClear, |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function onClear(): void |
46
|
|
|
{ |
47
|
|
|
$this->rootCategories = null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function recomputeSingleEntityChangeSet(EventArgs $args): void |
51
|
|
|
{ |
52
|
|
|
$em = $args->getEntityManager(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$em->getUnitOfWork()->recomputeSingleEntityChangeSet( |
55
|
|
|
$em->getClassMetadata(\get_class($args->getEntity())), |
56
|
|
|
$args->getEntity() |
|
|
|
|
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getMedia(EventArgs $args) |
61
|
|
|
{ |
62
|
|
|
$media = $args->getEntity(); |
63
|
|
|
|
64
|
|
|
if (!$media instanceof MediaInterface) { |
65
|
|
|
return $media; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->container->has('sonata.media.manager.category') && !$media->getCategory()) { |
69
|
|
|
$media->setCategory($this->getRootCategory($media)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $media; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @throws \RuntimeException |
77
|
|
|
* |
78
|
|
|
* @return CategoryInterface |
79
|
|
|
*/ |
80
|
|
|
protected function getRootCategory(MediaInterface $media) |
81
|
|
|
{ |
82
|
|
|
if (!$this->rootCategories) { |
83
|
|
|
$this->rootCategories = $this->container->get('sonata.media.manager.category')->getRootCategories(false); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!\array_key_exists($media->getContext(), $this->rootCategories)) { |
87
|
|
|
throw new \RuntimeException(sprintf('There is no main category related to context: %s', $media->getContext())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->rootCategories[$media->getContext()]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: