Completed
Push — master ( 88e766...b7d603 )
by Paweł
16s
created

ThemeExtension::getThemeLogoPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 Sourcefabric z.u. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Twig;
18
19
use SWP\Bundle\CoreBundle\Context\ScopeContextInterface;
20
use SWP\Bundle\CoreBundle\Model\TenantInterface;
21
use SWP\Bundle\CoreBundle\Theme\Provider\ThemeLogoProviderInterface;
22
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
23
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
24
use Twig\Extension\AbstractExtension;
25
use Twig\TwigFunction;
26
27
final class ThemeExtension extends AbstractExtension
28
{
29
    /**
30
     * @var ThemeLogoProviderInterface
31
     */
32
    private $themeLogoProvider;
33
34
    /**
35
     * @var TenantContextInterface
36
     */
37
    private $tenantContext;
38
39
    /**
40
     * @var SettingsManagerInterface
41
     */
42
    private $settingsManager;
43
44
    /**
45
     * @var string
46
     */
47
    private $env;
48
49
    /**
50
     * ThemeExtension constructor.
51
     *
52
     * @param ThemeLogoProviderInterface $themeLogoProvider
53
     * @param TenantContextInterface     $tenantContext
54
     * @param SettingsManagerInterface   $settingsManager
55
     * @param string                     $env
56
     */
57
    public function __construct(
58
        ThemeLogoProviderInterface $themeLogoProvider,
59
        TenantContextInterface $tenantContext,
60
        SettingsManagerInterface $settingsManager,
61
        string $env
62
    ) {
63
        $this->themeLogoProvider = $themeLogoProvider;
64
        $this->tenantContext = $tenantContext;
65
        $this->settingsManager = $settingsManager;
66
        $this->env = $env;
67
    }
68
69
    /**
70
     * @return TwigFunction[]
71
     */
72
    public function getFunctions(): array
73
    {
74
        return [
75
            new TwigFunction('themeLogo', [$this, 'getThemeLogoPath']),
76
            new TwigFunction('themeSetting', [$this, 'getThemeSetting']),
77
        ];
78
    }
79
80
    /**
81
     * @param string $fallBackPath
82
     *
83
     * @return string
84
     */
85
    public function getThemeLogoPath(string $fallBackPath): string
86
    {
87
        $link = $this->themeLogoProvider->getLogoLink();
88
89
        if ('' === $link) {
90
            return $fallBackPath;
91
        }
92
93
        return $link;
94
    }
95
96
    /**
97
     * @param string $setting
98
     *
99
     * @return string
100
     *
101
     * @throws \Exception
102
     */
103
    public function getThemeSetting(string $setting): string
104
    {
105
        try {
106
            return $this->getSetting($setting);
107
        } catch (\Exception $e) {
108
            if ('prod' === $this->env) {
109
                return '';
110
            }
111
112
            throw $e;
113
        }
114
    }
115
116
    private function getSetting(string $setting): string
117
    {
118
        /** @var TenantInterface $tenant */
119
        $tenant = $this->tenantContext->getTenant();
120
121
        return (string) $this->settingsManager->get(
122
            $setting,
123
            ScopeContextInterface::SCOPE_THEME,
124
            $tenant
125
        );
126
    }
127
}
128