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