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    public function setCustomizedTemplate(\BO\Zmsentities\Ticketprinter $ticketprinter, \BO\Zmsentities\Organisation $organisation): static
44    {
45        $template = null;
46        if ($this->defaultTemplate == 'default') {
47            $template = $this->getTemplateBySettings($ticketprinter, $organisation);
48        }
49        $this->template = ($template) ? $template : $this->template;
50        return $this;
51    }
52
53    public function getButtonTemplateType($ticketprinter): string
54    {
55        if (1 == count($ticketprinter->buttons)) {
56            $buttonDisplay = 'button_single';
57        } elseif (2 == count($ticketprinter->buttons)) {
58            $buttonDisplay = 'button_multi_deep';
59        } else {
60            $buttonDisplay = 'button_multi';
61        }
62        return $buttonDisplay;
63    }
64
65    protected function getTemplateBySettings($ticketprinter, $organisation)
66    {
67        $template = null;
68        //look for customized templates by single scope or single cluster
69        if (1 == count($ticketprinter->buttons)) {
70            $entity = null;
71            if ($ticketprinter->getScopeList()->getFirst()) {
72                $entity = $ticketprinter->getScopeList()->getFirst();
73            } elseif ($ticketprinter->getClusterList()->getFirst()) {
74                $entity = $ticketprinter->getClusterList()->getFirst();
75            }
76            $template = $this->getExistingTemplate($entity);
77        }
78        // look for customized template in clusterlist, overwrite template before
79        // cluster not supported anymore
80        /*
81        foreach ($ticketprinter->getClusterList() as $entity) {
82            if ($this->getExistingTemplate($entity)) {
83                $template = $this->getExistingTemplate($entity);
84                break;
85            }
86        }
87        */
88        //look for customized template in departmentlist, overwrite template before
89        foreach ($organisation->departments as $departmentData) {
90            $entity = new Department($departmentData);
91            if ($this->getExistingTemplate($entity)) {
92                $template = $this->getExistingTemplate($entity);
93                break;
94            }
95        }
96        return $template;
97    }
98
99    protected function getExistingTemplate(Entity $entity): string|null
100    {
101        $path = $this->subPath . '/buttonDisplay_' . $entity->getEntityName() . '_' . $entity->id . '.twig';
102        if ($entity->hasId() && $this->isTemplateReadable($path)) {
103            return $path;
104        }
105        return null;
106    }
107
108    protected function isTemplateReadable(string $path): bool
109    {
110        return is_readable($this->getTemplatePath() . $path);
111    }
112
113    /**
114     * @todo check against ISO definition
115     */
116    protected function getTemplatePath(): string
117    {
118        return realpath(__DIR__) . '/../../../templates';
119    }
120}