TenantContext::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Component.
5
 *
6
 * Copyright 2015 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 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\MultiTenancyBundle\Context;
16
17
use SWP\Bundle\CoreBundle\Document\Tenant;
18
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
19
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
20
use SWP\Component\MultiTenancy\Model\TenantInterface;
21
use SWP\Component\MultiTenancy\Resolver\TenantResolverInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\EventDispatcher\GenericEvent;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
26
/**
27
 * Class TenantContext.
28
 */
29
class TenantContext implements TenantContextInterface
30
{
31
    /**
32
     * @var TenantInterface
33
     */
34
    protected $tenant;
35
36
    /**
37
     * @var TenantResolverInterface
38
     */
39
    protected $tenantResolver;
40
41
    /**
42
     * @var RequestStack
43
     */
44
    protected $requestStack;
45
46
    /**
47
     * @var EventDispatcherInterface
48
     */
49
    protected $dispatcher;
50
51
    /**
52
     * TenantContext constructor.
53
     */
54
    public function __construct(TenantResolverInterface $tenantResolver, RequestStack $requestStack, EventDispatcherInterface $dispatcher)
55
    {
56
        $this->tenantResolver = $tenantResolver;
57
        $this->requestStack = $requestStack;
58
        $this->dispatcher = $dispatcher;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getTenant()
65
    {
66
        if (null === $this->tenant) {
67
            $currentRequest = $this->requestStack->getCurrentRequest();
68
69
            if (null !== $currentRequest && false !== strpos($currentRequest->getRequestUri(), '_profiler')) {
70
                $profilerTenant = new Tenant();
71
                $profilerTenant->setDomainName($currentRequest->getHost());
72
                $this->setTenant($profilerTenant);
73
74
                return $this->tenant;
75
            }
76
77
            $this->setTenant($this->tenantResolver->resolve(
78
                $currentRequest ? $currentRequest->getHost() : null
79
            ));
80
        }
81
82
        return $this->tenant;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setTenant(TenantInterface $tenant)
89
    {
90
        $this->tenant = $tenant;
91
92
        $this->dispatcher->dispatch(MultiTenancyEvents::TENANT_SET, new GenericEvent($tenant));
93
    }
94
}
95