|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Serializer; |
|
18
|
|
|
|
|
19
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
20
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
21
|
|
|
use SWP\Bundle\CoreBundle\Model\Article; |
|
22
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
|
23
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
24
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class TenantAwareArticleSerializationSubscriber implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var TenantContextInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $tenantContext; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var TenantRepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $tenantRepository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var TenantInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $originalTenant; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* TenantAwareArticleSerializationSubscriber constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param TenantContextInterface $tenantContext |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(TenantContextInterface $tenantContext, TenantRepositoryInterface $tenantRepository) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->tenantContext = $tenantContext; |
|
51
|
|
|
$this->tenantRepository = $tenantRepository; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function getSubscribedEvents() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
[ |
|
61
|
|
|
'event' => 'serializer.post_serialize', |
|
62
|
|
|
'class' => Article::class, |
|
63
|
|
|
'method' => 'onPostSerialize', |
|
64
|
|
|
], |
|
65
|
|
|
[ |
|
66
|
|
|
'event' => 'serializer.pre_serialize', |
|
67
|
|
|
'class' => Article::class, |
|
68
|
|
|
'method' => 'onPreSerialize', |
|
69
|
|
|
], |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ObjectEvent $event |
|
75
|
|
|
*/ |
|
76
|
|
|
public function onPreSerialize(ObjectEvent $event) |
|
77
|
|
|
{ |
|
78
|
|
|
$data = $event->getObject(); |
|
79
|
|
|
if ($this->tenantContext->getTenant()->getCode() !== $data->getTenantCode()) { |
|
80
|
|
|
$this->originalTenant = $this->tenantContext->getTenant(); |
|
81
|
|
|
$tenant = $this->tenantRepository->findOneByCode($data->getTenantCode()); |
|
82
|
|
|
$this->tenantContext->setTenant($tenant); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param ObjectEvent $event |
|
88
|
|
|
*/ |
|
89
|
|
|
public function onPostSerialize(ObjectEvent $event) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->originalTenant && $this->tenantContext->getTenant() !== $this->originalTenant) { |
|
92
|
|
|
$this->tenantContext->setTenant($this->originalTenant); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: