Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

TenantAwareThemeContext::getTheme()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 0
crap 4.0466
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. 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\CoreBundle\Theme;
16
17
use Doctrine\Common\Cache\CacheProvider;
18
use SWP\Bundle\CoreBundle\Exception\NoThemeException;
19
use SWP\Bundle\CoreBundle\Theme\Helper\ThemeHelper;
20
use SWP\Component\Common\Model\ThemeAwareTenantInterface;
21
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
22
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
23
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
24
25
/**
26
 * Class TenantAwareThemeContext.
27
 */
28
final class TenantAwareThemeContext implements ThemeContextInterface
29
{
30
    /**
31
     * @var TenantContextInterface
32
     */
33
    private $tenantContext;
34
35
    /**
36
     * @var ThemeRepositoryInterface
37
     */
38
    private $themeRepository;
39
40
    /**
41
     * @var CacheProvider
42
     */
43
    private $cacheService;
44
45
    /**
46
     * @var array
47
     */
48
    private $themes;
49
50
    /**
51
     * TenantAwareThemeContext constructor.
52
     *
53
     * @param TenantContextInterface   $tenantContext   Tenant context
54
     * @param ThemeRepositoryInterface $themeRepository Theme repository
55
     * @param CacheProvider            $cacheService    Cache Service
56
     */
57 87
    public function __construct(
58
        TenantContextInterface $tenantContext,
59
        ThemeRepositoryInterface $themeRepository,
60
        CacheProvider $cacheService
61
    ) {
62 87
        $this->tenantContext = $tenantContext;
63 87
        $this->themeRepository = $themeRepository;
64 87
        $this->cacheService = $cacheService;
65 87
        $this->themes = [];
66 87
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 38
    public function getTheme()
72
    {
73
        /* @var ThemeAwareTenantInterface $tenant */
74 38
        $tenant = $this->tenantContext->getTenant();
75
76 38
        $key = md5($tenant->getCode());
77 38
        if (array_key_exists($key, $this->themes)) {
78 33
            return $this->themes[$key];
79
        }
80
81 38
        if ($this->cacheService->contains('theme_'.$key)) {
82
            return $this->themes[$key] = $this->cacheService->fetch('theme_'.$key);
83
        }
84
85 38
        $theme = $this->themeRepository->findOneByName($this->resolveThemeName($tenant));
86 38
        unset($tenant);
87
88 38
        if (null === $theme) {
89
            throw new NoThemeException();
90
        }
91
92 38
        $this->themes[$key] = $theme;
93 38
        $this->cacheService->save('theme_'.$key, $theme, 600);
94
95 38
        return $theme;
96
    }
97
98 38
    private function resolveThemeName(ThemeAwareTenantInterface $tenant)
99
    {
100 38
        $themeName = $tenant->getThemeName();
101 38
        if (null !== $themeName) {
102 38
            return $tenant->getThemeName().ThemeHelper::SUFFIX_SEPARATOR.$tenant->getCode();
103
        }
104
105
        return $themeName;
106
    }
107
}
108