Completed
Push — master ( 9107b2...4da1f4 )
by Paweł
19s
created

CachedTenantContext::setTenant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2017 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 2017 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Context;
16
17
use Doctrine\Common\Cache\Cache;
18
use Doctrine\ORM\EntityManager;
19
use SWP\Bundle\CoreBundle\Model\Route;
20
use SWP\Bundle\MultiTenancyBundle\Context\TenantContext;
21
use SWP\Component\MultiTenancy\Model\TenantInterface;
22
use SWP\Component\MultiTenancy\Resolver\TenantResolverInterface;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
26
class CachedTenantContext extends TenantContext implements CachedTenantContextInterface
27
{
28
    /**
29
     * @var Cache
30
     */
31
    protected $cacheProvider;
32
33
    /**
34
     * @var EntityManager
35
     */
36
    protected $entityManager;
37
38
    /**
39
     * CachedTenantContext constructor.
40
     *
41
     * @param TenantResolverInterface  $tenantResolver
42
     * @param RequestStack             $requestStack
43
     * @param EventDispatcherInterface $dispatcher
44
     * @param Cache                    $cacheProvider
45
     * @param EntityManager            $entityManager
46
     */
47
    public function __construct(TenantResolverInterface $tenantResolver, RequestStack $requestStack, EventDispatcherInterface $dispatcher, Cache $cacheProvider, EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
48
    {
49
        $this->tenantResolver = $tenantResolver;
50
        $this->requestStack = $requestStack;
51
        $this->dispatcher = $dispatcher;
52
        $this->cacheProvider = $cacheProvider;
53
        $this->entityManager = $entityManager;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getTenant()
60
    {
61
        if (null === $this->tenant) {
62
            $currentRequest = $this->requestStack->getCurrentRequest();
63
            if (null !== $currentRequest) {
64
                $cacheKey = self::getCacheKey($currentRequest->getHost());
65
                if ($this->cacheProvider->contains($cacheKey)) {
66
                    $tenant = $this->cacheProvider->fetch($cacheKey);
67
                    // solution for Symfony Route heavy serialization
68
                    if (null !== $tenant->getHomepage()) {
69
                        $tenant->setHomepage($this->entityManager->find(Route::class, $tenant->getHomepage()->getId()));
70
                    }
71
                    $this->setTenant($tenant);
72
                } else {
73
                    $tenant = $this->tenantResolver->resolve(
74
                        $currentRequest ? $currentRequest->getHost() : null
75
                    );
76
                    $this->cacheProvider->save($cacheKey, $tenant);
77
                    $this->setTenant($tenant);
78
                }
79
            }
80
        }
81
82
        return $this->tenant;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setTenant(TenantInterface $tenant)
89
    {
90
        parent::setTenant($tenant);
91
        $host = $tenant->getDomainName();
92
        if ($subdomain = $tenant->getSubdomain()) {
93
            $host = $subdomain.'.'.$host;
94
        }
95
        $this->cacheProvider->delete(self::getCacheKey($host));
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public static function getCacheKey($host)
102
    {
103
        return 'tenant_cache__'.$host;
104
    }
105
}
106