TenantableListener   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 73
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A enable() 0 12 3
A disable() 0 8 2
A getSubscribedEvents() 0 8 1
A lazyLoad() 0 6 2
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
    public function __construct(RegistryInterface $doctrine, TenantContextInterface $tenantContext)
50
    {
51
        $this->doctrine = $doctrine;
52
        $this->tenantContext = $tenantContext;
53
    }
54
55
    /**
56
     * Enables tenantable filter on kernel.request.
57
     */
58
    public function enable()
59
    {
60
        $this->lazyLoad();
61
62
        $tenant = $this->tenantContext->getTenant();
63
        if ($tenant && $tenant->getId()) {
64
            $this->entityManager
65
                ->getFilters()
66
                ->enable('tenantable')
67
                ->setParameter('tenantCode', $tenant->getCode());
68
        }
69
    }
70
71
    /**
72
     * Disabled tenantable filter.
73
     */
74
    public function disable()
75
    {
76
        $this->lazyLoad();
77
        $filters = $this->entityManager->getFilters();
78
        if ($filters->isEnabled('tenantable')) {
79
            $filters->disable('tenantable');
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getSubscribedEvents()
87
    {
88
        return [
89
            KernelEvents::REQUEST => 'enable',
90
            MultiTenancyEvents::TENANTABLE_ENABLE => 'enable',
91
            MultiTenancyEvents::TENANTABLE_DISABLE => 'disable',
92
        ];
93
    }
94
95
    private function lazyLoad()
96
    {
97
        if (null === $this->entityManager) {
98
            $this->entityManager = $this->doctrine->getManager();
99
        }
100
    }
101
}
102