Completed
Push — master ( a24ccc...b6e315 )
by Paweł
16s
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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);
0 ignored issues
show
Bug introduced by
It seems like $tenant defined by $this->tenantRepository-...$data->getTenantCode()) on line 81 can be null; however, SWP\Component\MultiTenan...tInterface::setTenant() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
83
        }
84
    }
85
86
    /**
87
     * @param ObjectEvent $event
88
     */
89
    public function onPostSerialize(ObjectEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
90
    {
91
        if ($this->originalTenant && $this->tenantContext->getTenant() !== $this->originalTenant) {
92
            $this->tenantContext->setTenant($this->originalTenant);
93
        }
94
    }
95
}
96