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; |
| 14 | use BO\Zmsentities\Scope; |
| 15 | use BO\Zmsentities\Cluster; |
| 16 | use BO\Zmsentities\Department; |
| 17 | |
| 18 | class 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 | public function getTemplate() |
| 37 | { |
| 38 | return $this->template; |
| 39 | } |
| 40 | |
| 41 | public function setCustomizedTemplate($calldisplay) |
| 42 | { |
| 43 | $template = null; |
| 44 | if ($this->defaultTemplate == 'defaultplatz') { |
| 45 | $template = $this->getTemplateBySettings($calldisplay); |
| 46 | } |
| 47 | $this->template = ($template) ? $template : $this->template; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | protected function getTemplateBySettings($calldisplay) |
| 52 | { |
| 53 | $template = null; |
| 54 | //look for customized templates by single scope or single cluster |
| 55 | if ($calldisplay->getScopeList()->getFirst()) { |
| 56 | $entity = new Scope($calldisplay->getScopeList()->getFirst()); |
| 57 | $template = $this->getExistingTemplate($entity); |
| 58 | } |
| 59 | //look for customized template in clusterlist, overwrite template before |
| 60 | foreach ($calldisplay->getClusterList() as $entity) { |
| 61 | $entity = new Cluster($entity); |
| 62 | if ($this->getExistingTemplate($entity)) { |
| 63 | $template = $this->getExistingTemplate($entity); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | //look for customized template in departmentlist, overwrite template before |
| 68 | foreach ($calldisplay->organisation['departments'] as $departmentData) { |
| 69 | $entity = new Department($departmentData); |
| 70 | if ($this->getExistingTemplate($entity)) { |
| 71 | $template = $this->getExistingTemplate($entity); |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $template; |
| 77 | } |
| 78 | |
| 79 | protected function getExistingTemplate(Entity $entity) |
| 80 | { |
| 81 | $path = $this->subPath . '/calldisplay_' . $entity->getEntityName() . '_' . $entity->getId() . '.twig'; |
| 82 | if ($entity->hasId() && $this->isTemplateReadable($path)) { |
| 83 | return $path; |
| 84 | } |
| 85 | |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | protected function isTemplateReadable($path) |
| 90 | { |
| 91 | return is_readable($this->getTemplatePath() . $path); |
| 92 | } |
| 93 | |
| 94 | public function getTemplatePath() |
| 95 | { |
| 96 | return realpath(__DIR__) . '/../../../templates'; |
| 97 | } |
| 98 | } |