Completed
Push — master ( 896e9e...155c91 )
by Craig
05:56
created

PageVarRuntime   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pageGetVar() 0 7 2
A __construct() 0 6 1
A pageSetVar() 0 7 3
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 InvalidArgumentException;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\ThemeModule\Engine\ParameterBag;
20
21
class PageVarRuntime implements RuntimeExtensionInterface
22
{
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    /**
29
     * @var ParameterBag
30
     */
31
    private $pageVars;
32
33
    public function __construct(
34
        TranslatorInterface $translator,
35
        ParameterBag $pageVars
36
    ) {
37
        $this->translator = $translator;
38
        $this->pageVars = $pageVars;
39
    }
40
41
    /**
42
     * Zikula imposes no restriction on page variable names.
43
     * Typical usage is to set `title` `meta.charset` `lang` etc.
44
     * array values are set using `.` in the `$name` string (e.g. `meta.charset`)
45
     */
46
    public function pageSetVar(string $name, string $value): void
47
    {
48
        if (empty($name) || empty($value)) {
49
            throw new InvalidArgumentException($this->translator->trans('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
50
        }
51
52
        $this->pageVars->set($name, $value);
53
    }
54
55
    public function pageGetVar(string $name, string $default = '')
56
    {
57
        if (empty($name)) {
58
            throw new InvalidArgumentException($this->translator->trans('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
59
        }
60
61
        return $this->pageVars->get($name, $default);
62
    }
63
}
64