|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sulu GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\CommentBundle\EventSubscriber; |
|
13
|
|
|
|
|
14
|
|
|
use JMS\Serializer\EventDispatcher\Events; |
|
15
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
16
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
17
|
|
|
use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupRuleInterface; |
|
18
|
|
|
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface; |
|
19
|
|
|
use Sulu\Bundle\CommentBundle\Entity\Comment; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
21
|
|
|
|
|
22
|
|
|
class CommentSerializerEventSubscriber implements EventSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var MediaManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $mediaManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var RequestStack |
|
31
|
|
|
*/ |
|
32
|
|
|
private $requestStack; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(MediaManagerInterface $mediaManager, RequestStack $requestStack) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->mediaManager = $mediaManager; |
|
37
|
|
|
$this->requestStack = $requestStack; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function getSubscribedEvents() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
[ |
|
44
|
|
|
'event' => Events::POST_SERIALIZE, |
|
45
|
|
|
'format' => 'json', |
|
46
|
|
|
'method' => 'onPostSerialize', |
|
47
|
|
|
], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function onPostSerialize(ObjectEvent $event) |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var Comment $comment */ |
|
54
|
|
|
$comment = $event->getObject(); |
|
55
|
|
|
if (!$comment instanceof Comment || !$creator = $comment->getCreator()) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$contact = $creator->getContact(); |
|
60
|
|
|
if (!$avatar = $contact->getAvatar()) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$avatar = $this->mediaManager->getById($avatar->getId(), $this->requestStack->getCurrentRequest()->getLocale()); |
|
65
|
|
|
|
|
66
|
|
|
$event->getVisitor()->addData('creatorAvatar', $event->getContext()->accept([ |
|
|
|
|
|
|
67
|
|
|
'id' => $avatar->getId(), |
|
68
|
|
|
'title' => $avatar->getTitle(), |
|
69
|
|
|
'description' => $avatar->getDescription(), |
|
70
|
|
|
'credits' => $avatar->getCredits(), |
|
71
|
|
|
'name' => $avatar->getName(), |
|
72
|
|
|
'formats' => $avatar->getFormats(), |
|
73
|
|
|
'url' => $avatar->getUrl(), |
|
74
|
|
|
])); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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 implementation 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 interface: