TenantKernel   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
getAllTenants() 0 1 ?
getCurrentTenantKey() 0 1 ?
A __construct() 0 6 1
A initializeContainer() 0 10 1
A getTenantRegistry() 0 11 2
A handle() 0 27 5
1
<?php
2
3
namespace Vivait\TenantBundle\Kernel;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
use Symfony\Component\HttpKernel\HttpKernelInterface;
8
use Symfony\Component\HttpKernel\Kernel;
9
use Vivait\TenantBundle\Registry\TenantRegistry;
10
11
abstract class TenantKernel extends Kernel {
12
    /**
13
     * @var TenantRegistry
14
     */
15
    private $tenantRegistry;
16
17
    /**
18
     * @var bool Enable tenanting?
19
     */
20
    public $enableTenanting;
21
22
    public function __construct($environment, $debug)
23
    {
24
        $this->enableTenanting = !$debug;
25
26
        parent::__construct($environment, $debug);
27
    }
28
29
    protected function initializeContainer()
30
    {
31
        parent::initializeContainer();
32
33
        // Inject the registry to the container
34
        $this->getContainer()->set(
35
            'vivait_tenant.registry',
36
            $this->getTenantRegistry()
37
        );
38
    }
39
40
    /**
41
     * Provides an array of all Tenants
42
     *
43
     * For example:
44
     * <code>
45
     * <?php
46
     * $configProvider = new \Vivait\TenantBundle\Provider\ConfigProvider( __DIR__ . '/config/' );
47
     * return $configProvider->loadTenants();
48
     * ?>
49
     * </code>
50
     * @return \Vivait\TenantBundle\Model\Tenant[]
51
     */
52
    abstract protected function getAllTenants();
53
54
    public function getTenantRegistry() {
55
        if ($this->tenantRegistry === null) {
56
            $tenants = $this->getAllTenants();
57
58
            $this->tenantRegistry = new TenantRegistry(
59
                $tenants
60
            );
61
        }
62
63
        return $this->tenantRegistry;
64
    }
65
66
    /**
67
     * Provides the current tenant's key
68
     * @param Request $request
69
     * @return string The current tenant's key
70
     */
71
    abstract protected function getCurrentTenantKey(Request $request);
72
73
    public function handle(
74
        Request $request,
75
        $type = HttpKernelInterface::MASTER_REQUEST,
76
        $catch = true
77
    ) {
78
        if (false === $this->booted && $this->enableTenanting) {
79
            // Find and set the current tenant
80
            try {
81
                $tenant = $this->getCurrentTenantKey( $request );
82
                $this->getTenantRegistry()->setCurrent( $tenant );
83
84
                // Change the environment to the tenant's environment
85
                $this->environment = 'tenant_' . $tenant;
86
            }
87
            catch (\OutOfBoundsException $e) {
88
                throw new NotFoundHttpException('Could not find tenant');
89
            }
90
            // Could not match a tenant, use default
91
            // TODO: I'm not convinced this is the best behaviour
92
            catch (\RuntimeException $e) {
93
            }
94
95
            $this->boot();
96
        }
97
98
        return parent::handle( $request, $type, $catch );
99
    }
100
}
101