Test Setup Failed
Pull Request — master (#4522)
by Craig
08:26 queued 03:47
created

ThemeRuntime::getPreviewImagePath()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
nc 5
nop 2
dl 0
loc 17
rs 9.9666
c 1
b 0
f 1
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\Runtime;
15
16
use Exception;
17
use InvalidArgumentException;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface;
20
use Zikula\ThemeModule\Engine\Asset;
21
use Zikula\ThemeModule\Engine\AssetBag;
22
23
class ThemeRuntime implements RuntimeExtensionInterface
24
{
25
    /**
26
     * @var PageAssetApiInterface
27
     */
28
    private $pageAssetApi;
29
30
    /**
31
     * @var Asset
32
     */
33
    private $assetHelper;
34
35
    public function __construct(
36
        PageAssetApiInterface $pageAssetApi,
37
        Asset $assetHelper
38
    ) {
39
        $this->pageAssetApi = $pageAssetApi;
40
        $this->assetHelper = $assetHelper;
41
    }
42
43
    /**
44
     * Zikula allows only the following asset types:
45
     * <ul>
46
     *  <li>stylesheet</li>
47
     *  <li>javascript</li>
48
     *  <li>header</li>
49
     *  <li>footer</li>
50
     * </ul>
51
     */
52
    public function pageAddAsset(string $type, string $value, int $weight = AssetBag::WEIGHT_DEFAULT): void
53
    {
54
        $this->pageAssetApi->add($type, $value, $weight);
55
    }
56
57
    /**
58
     * Get path to theme preview image.
59
     */
60
    public function getPreviewImagePath(string $themeName, string $size = 'medium'): string
61
    {
62
        if (!isset($themeName)) {
63
            throw new InvalidArgumentException('Invalid theme name.');
64
        }
65
66
        if (!in_array($size, ['large', 'medium', 'small'], true)) {
67
            $size = 'medium';
68
        }
69
70
        try {
71
            $imagePath = $this->assetHelper->resolve('@' . $themeName . ':images/preview_' . $size . '.png');
72
        } catch (Exception $exception) {
73
            $imagePath = $this->assetHelper->resolve('@ZikulaThemeModule:images/preview_' . $size . '.png');
74
        }
75
76
        return $imagePath;
77
    }
78
79
    /**
80
     * Resolves a given asset path.
81
     */
82
    public function getAssetPath(string $path): string
83
    {
84
        return $this->assetHelper->resolve($path);
85
    }
86
}
87