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