Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
84.85% covered (warning)
84.85%
28 / 33
70.59% covered (warning)
70.59%
12 / 17
66.67% covered (warning)
66.67%
10 / 15
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Extension
84.85% covered (warning)
84.85%
28 / 33
70.59% covered (warning)
70.59%
12 / 17
66.67% covered (warning)
66.67%
10 / 15
69.23% covered (warning)
69.23%
9 / 13
23.33
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilters
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatDate
60.00% covered (warning)
60.00%
3 / 5
60.00% covered (warning)
60.00%
3 / 5
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
5.67
 shortDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shortDateTime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 version
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 serverType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParameter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isModeMaintenance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isPageInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageInfoId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Twig;
4
5use App\Application;
6use App\ExceptionApplication;
7use App\Parameter;
8use DateTime;
9use Twig\Extension\AbstractExtension;
10use Twig\TwigFilter;
11use Twig\TwigFunction;
12
13class Extension extends AbstractExtension
14{
15    public Application $application;
16    public Parameter $parameter;
17
18    public function __construct(Application $application, Parameter $parameter)
19    {
20        $this->application = $application;
21        $this->parameter = $parameter;
22    }
23
24    /**
25     * @return TwigFunction[]
26     */
27    public function getFunctions(): array
28    {
29        return [
30            new TwigFunction('serverType', [$this, 'serverType'], ['is_safe' => ['html']]),
31            new TwigFunction('version', [$this, 'version'], ['is_safe' => ['html']]),
32            new TwigFunction('getParameter', [$this, 'getParameter'], ['is_safe' => ['html']]),
33            new TwigFunction(
34                'isModeMaintenance',
35                [$this, 'isModeMaintenance'],
36                ['is_safe' => ['html']]
37            ),
38            new TwigFunction('isPageInfo', [$this, 'isPageInfo'], ['is_safe' => ['html']]),
39            new TwigFunction('getPageInfo', [$this, 'getPageInfo'], ['is_safe' => ['html']]),
40            new TwigFunction('getPageInfoId', [$this, 'getPageInfoId'], ['is_safe' => ['html']]),
41        ];
42    }
43
44    /**
45     * @return TwigFilter[]
46     */
47    public function getFilters(): array
48    {
49        return [
50            'shortDate' => new TwigFilter('shortDate', [$this, 'shortDate'], ['is_safe' => ['html']]),
51            'shortDateTime' => new TwigFilter('shortDateTime', [$this, 'shortDateTime'], ['is_safe' => ['html']]),
52        ];
53    }
54
55    /**
56     * @throws ExceptionApplication
57     */
58    protected function formatDate(mixed $dateTime, string $format): ?string
59    {
60        if (!$dateTime) {
61            return null;
62        }
63        if ($dateTime instanceof DateTime) {
64            return $dateTime->format($format);
65        } else {
66            throw new ExceptionApplication('Not a datetime object');
67        }
68    }
69
70    public function shortDate(?DateTime $dateTime): ?string
71    {
72        return $this->formatDate($dateTime, Application::FORMAT_SHORT_DATE);
73    }
74
75    public function shortDateTime(?DateTime $dateTime): ?string
76    {
77        return $this->formatDate($dateTime, Application::FORMAT_SHORT_DATETIME);
78    }
79
80    public function version(): string
81    {
82        return $this->application->getVersion();
83    }
84
85    public function serverType(): string
86    {
87        return $this->application->getServerType();
88    }
89
90    public function getParameter(string $parameter): mixed
91    {
92        return $this->parameter->{$parameter};
93    }
94
95    /**
96     * Indique si le site BO est en mode de maintenance.
97     */
98    public function isModeMaintenance(): bool
99    {
100        return (bool) $this->parameter->modeMaintenance;
101    }
102
103    /**
104     * Indique si un message doit être affiché.
105     */
106    public function isPageInfo(): bool
107    {
108        return '' !== $this->parameter->pageInfo;
109    }
110
111    /**
112     * Contenu du message pour le BO à afficher.
113     */
114    public function getPageInfo(): string
115    {
116        return $this->parameter->pageInfo;
117    }
118
119    /**
120     * Retourne l'ID du message pour le BO à afficher.
121     */
122    public function getPageInfoId(): string
123    {
124        return hash('sha512', $this->parameter->pageInfo);
125    }
126}