Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.50% covered (danger)
37.50%
12 / 32
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateFinder
37.50% covered (danger)
37.50%
12 / 32
57.14% covered (warning)
57.14%
4 / 7
87.56
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCustomizedTemplate
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getTemplateBySettings
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 getExistingTemplate
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 isTemplateReadable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTemplatePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 *
5 * @package Zmscalldisplay
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmscalldisplay\Helper;
11
12use BO\Zmscalldisplay\Exception\TemplateNotFound;
13use BO\Zmsentities\Schema\Entity;
14use BO\Zmsentities\Scope;
15use BO\Zmsentities\Cluster;
16use BO\Zmsentities\Department;
17
18class TemplateFinder
19{
20    protected $defaultTemplate;
21    protected $subPath;
22
23    protected $template;
24
25    public function __construct($defaultTemplate = "defaultplatz", $subPath = '/page/customized')
26    {
27        $this->subPath = $subPath;
28        $this->template = $subPath . '/' . $defaultTemplate . '.twig';
29        if ($this->isTemplateReadable($this->template)) {
30            $this->defaultTemplate = $defaultTemplate;
31        } else {
32            throw new TemplateNotFound("Could not find template $this->template");
33        }
34    }
35
36    /** @psalm-api */
37    public function getTemplate()
38    {
39        return $this->template;
40    }
41
42    public function setCustomizedTemplate($calldisplay): static
43    {
44        $template = null;
45        if ($this->defaultTemplate == 'defaultplatz') {
46            $template = $this->getTemplateBySettings($calldisplay);
47        }
48        $this->template = ($template) ? $template : $this->template;
49        return $this;
50    }
51
52    protected function getTemplateBySettings($calldisplay)
53    {
54        $template = null;
55        //look for customized templates by single scope or single cluster
56        if ($calldisplay->getScopeList()->getFirst()) {
57            $entity = new Scope($calldisplay->getScopeList()->getFirst());
58            $template = $this->getExistingTemplate($entity);
59        }
60        //look for customized template in clusterlist, overwrite template before
61        foreach ($calldisplay->getClusterList() as $entity) {
62            $entity = new Cluster($entity);
63            if ($this->getExistingTemplate($entity)) {
64                $template = $this->getExistingTemplate($entity);
65                break;
66            }
67        }
68        //look for customized template in departmentlist, overwrite template before
69        foreach ($calldisplay->organisation['departments'] as $departmentData) {
70            $entity = new Department($departmentData);
71            if ($this->getExistingTemplate($entity)) {
72                $template = $this->getExistingTemplate($entity);
73                break;
74            }
75        }
76
77        return $template;
78    }
79
80    protected function getExistingTemplate(Entity $entity): string|null
81    {
82        $path = $this->subPath . '/calldisplay_' . $entity->getEntityName() . '_' . $entity->getId() . '.twig';
83        if ($entity->hasId() && $this->isTemplateReadable($path)) {
84            return $path;
85        }
86
87        return null;
88    }
89
90    protected function isTemplateReadable(string $path): bool
91    {
92        return is_readable($this->getTemplatePath() . $path);
93    }
94
95    public function getTemplatePath(): string
96    {
97        return realpath(__DIR__) . '/../../../templates';
98    }
99}