Completed
Push — master ( 6b1909...2eb306 )
by Rafał
16:03
created

TenantableListener::lazyLoad()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\MultiTenancyBundle\EventListener;
16
17
use Doctrine\ORM\EntityManagerInterface;
18
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
19
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
20
use Symfony\Bridge\Doctrine\RegistryInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24
/**
25
 * TenantableListener class.
26
 *
27
 * It makes sure all SELECT queries are tenant aware.
28
 */
29
class TenantableListener implements EventSubscriberInterface
30
{
31
    /**
32
     * @var TenantContextInterface
33
     */
34
    protected $tenantContext;
35
36
    /**
37
     * @var EntityManagerInterface
38
     */
39
    protected $entityManager;
40
41
    /**
42
     * @var RegistryInterface
43
     */
44
    protected $doctrine;
45
46
    /**
47
     * Construct.
48
     *
49
     * @param RegistryInterface      $doctrine
50
     * @param TenantContextInterface $tenantContext
51
     */
52
    public function __construct(RegistryInterface $doctrine, TenantContextInterface $tenantContext)
53
    {
54
        $this->doctrine = $doctrine;
55
        $this->tenantContext = $tenantContext;
56
    }
57
58
    /**
59
     * Enables tenantable filter on kernel.request.
60
     */
61
    public function enable()
62
    {
63
        $this->lazyLoad();
64
        $tenant = $this->tenantContext->getTenant();
65
66
        if ($tenant && $tenant->getId()) {
67
            $this->entityManager
68
                ->getFilters()
69
                ->enable('tenantable')
70
                ->setParameter('tenantCode', $tenant->getCode());
71
        }
72
    }
73
74
    /**
75
     * Disabled tenantable filter.
76
     */
77
    public function disable()
78
    {
79
        $this->lazyLoad();
80
        $filters = $this->entityManager->getFilters();
81
82
        if ($filters->isEnabled('tenantable')) {
83
            $filters->disable('tenantable');
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public static function getSubscribedEvents()
91
    {
92
        return [
93
            KernelEvents::REQUEST => 'enable',
94
            MultiTenancyEvents::TENANTABLE_ENABLE => 'enable',
95
            MultiTenancyEvents::TENANTABLE_DISABLE => 'disable',
96
        ];
97
    }
98
99
    private function lazyLoad()
100
    {
101
        if (null === $this->entityManager) {
102
            $this->entityManager = $this->doctrine->getEntityManager();
103
        }
104
    }
105
}
106