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