Completed
Pull Request — master (#4240)
by Axel
05:34
created

BrandingExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeModule\Twig\Extension;
15
16
use Twig\Environment;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
use Zikula\Bundle\CoreBundle\Site\SiteDefinitionInterface;
20
use Zikula\ThemeModule\Engine\Asset;
21
22
class BrandingExtension extends AbstractExtension
23
{
24
    /**
25
     * @var Environment
26
     */
27
    private $twig;
28
29
    /**
30
     * @var SiteDefinitionInterface
31
     */
32
    private $site;
33
34
    /**
35
     * @var Asset
36
     */
37
    private $assetHelper;
38
39
    public function __construct(
40
        Environment $twig,
41
        SiteDefinitionInterface $site,
42
        Asset $assetHelper
43
    ) {
44
        $this->twig = $twig;
45
        $this->site = $site;
46
        $this->assetHelper = $assetHelper;
47
    }
48
49
    public function getFunctions()
50
    {
51
        return [
52
            new TwigFunction('siteName', [$this, 'getSiteName']),
53
            new TwigFunction('siteSlogan', [$this, 'getSiteSlogan']),
54
            new TwigFunction('siteBranding', [$this, 'getSiteBrandingMarkup'], ['is_safe' => ['html']]),
55
            new TwigFunction('siteImagePath', [$this, 'getSiteImagePath'])
56
        ];
57
    }
58
59
    /**
60
     * Returns site name.
61
     */
62
    public function getSiteName(): string
63
    {
64
        return $this->site->getName();
65
    }
66
67
    /**
68
     * Returns site slogan.
69
     */
70
    public function getSiteSlogan(): string
71
    {
72
        return $this->site->getSlogan();
73
    }
74
75
    /**
76
     * Returns site branding markup.
77
     */
78
    public function getSiteBrandingMarkup(): string
79
    {
80
        return $this->twig->render('@ZikulaThemeModule/Engine/manifest.html.twig');
81
    }
82
83
    /**
84
     * Returns site image path.
85
     */
86
    public function getSiteImagePath(string $imageType = ''): string
87
    {
88
        if (!in_array($imageType, ['logo', 'mobileLogo', 'icon'], true)) {
89
            $imageType = 'logo';
90
        }
91
92
        $accessor = 'get' . ucfirst($imageType) . 'Path';
93
94
        $assetPath = $this->site->$accessor();
95
96
        try {
97
            $imagePath = $this->assetHelper->resolve($assetPath);
98
        } catch (Exception $exception) {
0 ignored issues
show
Bug introduced by
The type Zikula\ThemeModule\Twig\Extension\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
99
            // fall back to default
100
            $assetPath = '@CoreBundle:images/';
101
            if ('logo' === $imageType) {
102
                $assetPath .= 'logo_with_title.png';
103
            } elseif ('mobileLogo' === $imageType) {
104
                $assetPath .= 'zk-power.png';
105
            } elseif ('icon' === $imageType) {
106
                $assetPath .= 'logo.gif';
107
            }
108
109
            $imagePath = $this->assetHelper->resolve($assetPath);
110
        }
111
112
        return $imagePath;
113
    }
114
}
115