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\Events; |
20
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
21
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
22
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
23
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
24
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
25
|
|
|
use JMS\Serializer\Metadata\StaticPropertyMetadata; |
26
|
|
|
use JMS\Serializer\Context; |
27
|
|
|
use JMS\Serializer\SerializationContext; |
28
|
|
|
use JMS\Serializer\SerializerInterface; |
29
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface; |
30
|
|
|
use SWP\Bundle\CoreBundle\Context\ScopeContext; |
31
|
|
|
use SWP\Bundle\CoreBundle\Model\Tenant; |
32
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
33
|
|
|
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
34
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
35
|
|
|
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface; |
36
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
37
|
|
|
use SWP\Component\MultiTenancy\Provider\TenantProviderInterface; |
38
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
39
|
|
|
|
40
|
|
|
final class TenantHandler implements EventSubscriberInterface, SubscribingHandlerInterface |
41
|
|
|
{ |
42
|
|
|
private $settingsManager; |
43
|
|
|
|
44
|
|
|
private $requestStack; |
45
|
|
|
|
46
|
|
|
private $routeRepository; |
47
|
|
|
|
48
|
|
|
private $contentListRepository; |
49
|
|
|
|
50
|
|
|
private $tenantContext; |
51
|
|
|
|
52
|
|
|
private $internalCache = []; |
53
|
|
|
|
54
|
|
|
private $serializer; |
55
|
|
|
|
56
|
|
|
private $tenantProvider; |
57
|
|
|
|
58
|
|
|
public function __construct( |
59
|
|
|
SettingsManagerInterface $settingsManager, |
60
|
|
|
RequestStack $requestStack, |
61
|
|
|
RouteRepositoryInterface $routeRepository, |
62
|
|
|
ContentListRepositoryInterface $contentListRepository, |
63
|
|
|
TenantContextInterface $tenantContext, |
64
|
|
|
TenantProviderInterface $tenantProvider, |
65
|
|
|
SerializerInterface $serializer |
66
|
|
|
) { |
67
|
|
|
$this->settingsManager = $settingsManager; |
68
|
|
|
$this->requestStack = $requestStack; |
69
|
|
|
$this->routeRepository = $routeRepository; |
70
|
|
|
$this->contentListRepository = $contentListRepository; |
71
|
|
|
$this->tenantContext = $tenantContext; |
72
|
|
|
$this->serializer = $serializer; |
73
|
|
|
$this->tenantProvider = $tenantProvider; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function getSubscribedEvents(): array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
[ |
80
|
|
|
'event' => Events::POST_SERIALIZE, |
81
|
|
|
'class' => Tenant::class, |
82
|
|
|
'method' => 'onPostSerialize', |
83
|
|
|
], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function getSubscribingMethods(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
[ |
91
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
92
|
|
|
'format' => 'json', |
93
|
|
|
'type' => TenantInterface::class, |
94
|
|
|
'method' => 'serializeToJson', |
95
|
|
|
], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function onPostSerialize(ObjectEvent $event): void |
100
|
|
|
{ |
101
|
|
|
/** @var TenantInterface $tenant */ |
102
|
|
|
$tenant = $event->getObject(); |
103
|
|
|
/** @var JsonSerializationVisitor $visitor */ |
104
|
|
|
$visitor = $event->getVisitor(); |
105
|
|
|
|
106
|
|
|
if (isset($this->internalCache[$tenant->getCode()])) { |
107
|
|
|
$cachedData = $this->internalCache[$tenant->getCode()]; |
108
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'fbia_enabled', null), $cachedData['settings']['fbiaEnabled']); |
109
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'paywall_enabled', null), $cachedData['settings']['paywallEnabled']); |
110
|
|
|
if (isset($cachedData['routes'])) { |
111
|
|
|
$visitor->visitProperty( |
112
|
|
|
new StaticPropertyMetadata('', 'routes', null, ['api_routes_list']), |
113
|
|
|
$cachedData['routes'] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
if (isset($cachedData['contentLists'])) { |
117
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'content_lists', null), $cachedData['contentLists']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$originalTenant = $this->tenantContext->getTenant(); |
124
|
|
|
if ($originalTenant->getCode() !== $tenant->getCode()) { |
125
|
|
|
$this->tenantContext->setTenant($tenant); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$fbiaEnabled = $this->settingsManager->get('fbia_enabled', ScopeContext::SCOPE_TENANT, $tenant, false); |
129
|
|
|
$paywallEnabled = $this->settingsManager->get('paywall_enabled', ScopeContext::SCOPE_TENANT, $tenant, false); |
130
|
|
|
$this->internalCache[$tenant->getCode()]['settings'] = [ |
131
|
|
|
'fbiaEnabled' => $fbiaEnabled, |
132
|
|
|
'paywallEnabled' => $paywallEnabled, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'fbia_enabled', null), $fbiaEnabled); |
136
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'paywall_enabled', null), $paywallEnabled); |
137
|
|
|
|
138
|
|
|
$masterRequest = $this->requestStack->getMasterRequest(); |
139
|
|
|
if (null !== $masterRequest && (null !== $masterRequest->get('withRoutes') || null !== $masterRequest->get('withContentLists'))) { |
140
|
|
View Code Duplication |
if (null !== $masterRequest->get('withRoutes')) { |
|
|
|
|
141
|
|
|
$routes = $this->routeRepository->getQueryByCriteria(new Criteria(['maxResults' => 9999]), [], 'r')->getQuery()->getResult(); |
142
|
|
|
$routesArray = $this->serializer->toArray($routes, SerializationContext::create()->setGroups(['Default', 'api_routes_list'])); |
|
|
|
|
143
|
|
|
$this->internalCache[$tenant->getCode()]['routes'] = $routesArray; |
144
|
|
|
|
145
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'routes', null, ['api_routes_list']), $routesArray); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
if (null !== $masterRequest->get('withContentLists')) { |
|
|
|
|
149
|
|
|
$contentLists = $this->contentListRepository->getQueryByCriteria(new Criteria(['maxResults' => 9999]), [], 'cl')->getQuery()->getResult(); |
150
|
|
|
$contentListsArray = $this->serializer->toArray($contentLists, SerializationContext::create()->setGroups(['Default', 'api'])); |
|
|
|
|
151
|
|
|
$this->internalCache[$tenant->getCode()]['contentLists'] = $contentListsArray; |
152
|
|
|
|
153
|
|
|
$visitor->visitProperty(new StaticPropertyMetadata('', 'content_lists', null), $contentListsArray); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($originalTenant->getCode() !== $tenant->getCode()) { |
158
|
|
|
$this->tenantContext->setTenant($originalTenant); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function serializeToJson( |
163
|
|
|
JsonSerializationVisitor $visitor, |
|
|
|
|
164
|
|
|
string $tenantCode, |
165
|
|
|
array $type, |
|
|
|
|
166
|
|
|
Context $context |
167
|
|
|
) { |
168
|
|
|
$tenant = $this->tenantProvider->findOneByCode($tenantCode); |
169
|
|
|
if (null === $tenant) { |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$data = $context->getNavigator()->accept($tenant); |
174
|
|
|
unset($data['articles_count'], $data['created_at'], $data['enabled'], $data['organization'],$data['theme_name'], $data['updated_at']); |
175
|
|
|
|
176
|
|
|
return $data; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.