App   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 8 3
A get() 0 6 2
A __construct() 0 3 1
1
<?php
2
3
namespace pjpawel\Magis\MagisBundle\Service;
4
5
use pjpawel\Magis\Helper\AppContainerInterface;
6
use pjpawel\Magis\Exception\TemplateException;
7
use Symfony\Bridge\Twig\AppVariable;
8
9
/**
10
 * @codeCoverageIgnore
11
 *
12
 * @author Paweł Podgórski <[email protected]>
13
 */
14
class App implements AppContainerInterface
15
{
16
17
    /**
18
     * <id>:<function_name>
19
     *
20
     * array<string, string>
21
     */
22
    private const VARIABLE_LIST = [
23
        'app.user' => 'getUser',
24
        'app.request' => 'getRequest',
25
        'app.session' => 'getSession',
26
        'app.environment' => 'getEnvironment',
27
        'app.debug' => 'getDebug',
28
        'app.flashes' =>'getFlashes'
29
    ];
30
31
    /**
32
     * @var array<string, mixed>
33
     */
34
    private array $variables = [];
35
36
    public function __construct(AppVariable $appVariable)
37
    {
38
        $this->update($appVariable);
39
    }
40
41
    /**
42
     * @param string $id Call id without 'app' prefix, e.g. 'app.user' you should call get('user')
43
     * @return mixed
44
     * @throws TemplateException
45
     */
46
    public function get(string $id): mixed
47
    {
48
        if (array_key_exists($id, $this->variables)) {
49
            return $this->variables[$id];
50
        } else {
51
            throw new TemplateException(sprintf('Undefined %s variable from App', $id));
52
        }
53
    }
54
55
    public function update(AppVariable $appVariable): void
56
    {
57
        foreach (self::VARIABLE_LIST as $id => $function) {
58
            $id = substr($id, 4);
59
            try {
60
                $this->variables[$id] = $appVariable->$function();
61
            } catch (\Exception $e) {
62
                $this->variables[$id] = null;
63
            }
64
        }
65
    }
66
}