Completed
Pull Request — 2.0 (#945)
by Paweł
09:47
created

TenantHandler::onPostSerialize()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25

Duplication

Lines 8
Ratio 32 %

Importance

Changes 0
Metric Value
dl 8
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 5
nop 1
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 SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
28
use SWP\Bundle\CoreBundle\Context\ScopeContext;
29
use SWP\Bundle\CoreBundle\Model\Tenant;
30
use SWP\Bundle\CoreBundle\Model\TenantInterface;
31
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
32
use SWP\Component\Common\Criteria\Criteria;
33
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface;
34
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
35
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
36
use Symfony\Component\HttpFoundation\RequestStack;
37
38
final class TenantHandler implements EventSubscriberInterface, SubscribingHandlerInterface
39
{
40
    private $settingsManager;
41
42
    private $requestStack;
43
44
    private $routeRepository;
45
46
    private $contentListRepository;
47
48
    private $tenantContext;
49
50
    private $tenantRepository;
51
52
    public function __construct(
53
        SettingsManagerInterface $settingsManager,
54
        RequestStack $requestStack,
55
        RouteRepositoryInterface $routeRepository,
56
        ContentListRepositoryInterface $contentListRepository,
57
        TenantContextInterface $tenantContext,
58
        TenantRepositoryInterface $tenantRepository
59
    ) {
60
        $this->settingsManager = $settingsManager;
61
        $this->requestStack = $requestStack;
62
        $this->routeRepository = $routeRepository;
63
        $this->contentListRepository = $contentListRepository;
64
        $this->tenantContext = $tenantContext;
65
        $this->tenantRepository = $tenantRepository;
66
    }
67
68
    public static function getSubscribedEvents(): array
69
    {
70
        return [
71
            [
72
                'event' => Events::POST_SERIALIZE,
73
                'class' => Tenant::class,
74
                'method' => 'onPostSerialize',
75
            ],
76
        ];
77
    }
78
79
    public static function getSubscribingMethods()
80
    {
81
        return [
82
            [
83
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
84
                'format' => 'json',
85
                'type' => TenantInterface::class,
86
                'method' => 'serializeToJson',
87
            ],
88
        ];
89
    }
90
91
    public function onPostSerialize(ObjectEvent $event): void
92
    {
93
        $tenant = $event->getObject();
94
        $originalTenant = $this->tenantContext->getTenant();
95
        $this->tenantContext->setTenant($tenant);
96
97
        /** @var JsonSerializationVisitor $visitor */
98
        $visitor = $event->getVisitor();
99
        $visitor->visitProperty(new StaticPropertyMetadata('', 'fbia_enabled', null), $this->settingsManager->get('fbia_enabled', ScopeContext::SCOPE_TENANT, $tenant, false));
100
        $visitor->visitProperty(new StaticPropertyMetadata('', 'paywall_enabled', null), $this->settingsManager->get('paywall_enabled', ScopeContext::SCOPE_TENANT, $tenant, false));
101
102
        $masterRequest = $this->requestStack->getMasterRequest();
103
        if (null !== $masterRequest && (null !== $masterRequest->get('withRoutes') || null !== $masterRequest->get('withContentLists'))) {
104 View Code Duplication
            if (null !== $masterRequest->get('withRoutes')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
105
                $routes = $this->routeRepository->getQueryByCriteria(new Criteria(), [], 'r')->getQuery()->getResult();
106
                $visitor->visitProperty(new StaticPropertyMetadata('', 'routes', null), $routes);
107
            }
108
109 View Code Duplication
            if (null !== $masterRequest->get('withContentLists')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
110
                $contentLists = $this->contentListRepository->getQueryByCriteria(new Criteria(), [], 'cl')->getQuery()->getResult();
111
                $visitor->visitProperty(new StaticPropertyMetadata('', 'content_lists', null), $contentLists);
112
            }
113
        }
114
        $this->tenantContext->setTenant($originalTenant);
115
    }
116
117
    public function serializeToJson(
118
        JsonSerializationVisitor $visitor,
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
        string $tenantCode,
120
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
        Context $context
122
    ) {
123
        /** @var TenantInterface $tenant */
124
        $tenant = $this->tenantRepository->findOneByCode($tenantCode);
125
        if (null === $tenant) {
126
            return;
127
        }
128
129
        $data = $context->getNavigator()->accept($tenant);
130
        unset($data['articles_count'], $data['created_at'], $data['enabled'], $data['organization'],$data['theme_name'], $data['updated_at']);
131
132
        return $data;
133
    }
134
}
135