Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
22.16% covered (danger)
22.16%
39 / 176
22.22% covered (danger)
22.22%
6 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
TwigExtension
22.16% covered (danger)
22.16%
39 / 176
22.22% covered (danger)
22.22%
6 / 27
1995.90
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
 dump
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 currentRoute
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 getD115Enabeld
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getD115OpeningTimes
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getD115Text
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getBobbiChatButtonEnabeld
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getOSMAccessToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOSMOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatPhoneNumber
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 convertOpeningTimes
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 dateToTS
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 tsToDate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 formatDateTime
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
 kindOfPayment
50.00% covered (danger)
50.00%
6 / 12
0.00% covered (danger)
0.00%
0 / 1
10.50
 azPrefixList
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 sortFirstChar
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 sortByName
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 toSortableString
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 csvProperty
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 csvAppointmentLocations
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 getAppointmentForLocationFromServiceAppointmentLocations
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getAppointmentForService
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 getLocationHintByServiceId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 dayIsBookable
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
5 **/
6
7namespace BO\Zmsdldb;
8
9use Error;
10
11/**
12 * Extension for Twig
13 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
14 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
15 * @SuppressWarnings(PHPMD.TooManyMethods)
16 */
17class TwigExtension extends \Twig\Extension\AbstractExtension
18{
19    private $container;
20
21    public function __construct($container = null)
22    {
23        $this->container = $container;
24    }
25
26    public function getName()
27    {
28        return 'dldb';
29    }
30
31    #[\Override]
32    public function getFunctions()
33    {
34        return array(
35            new \Twig\TwigFunction('convertOpeningTimes', array($this, 'convertOpeningTimes')),
36            new \Twig\TwigFunction('csvProperty', array($this, 'csvProperty')),
37            new \Twig\TwigFunction('csvAppointmentLocations', array($this, 'csvAppointmentLocations')),
38            new \Twig\TwigFunction('getAppointmentForService', array($this, 'getAppointmentForService')),
39            new \Twig\TwigFunction('getLocationHintByServiceId', array($this, 'getLocationHintByServiceId')),
40            new \Twig\TwigFunction('isAppointmentBookable', array($this, 'isAppointmentBookable')),
41            new \Twig\TwigFunction('kindOfPayment', array($this, 'kindOfPayment')),
42            new \Twig\TwigFunction('formatDateTime', array($this, 'formatDateTime')),
43            new \Twig\TwigFunction('dateToTS', array($this, 'dateToTS')),
44            new \Twig\TwigFunction('tsToDate', array($this, 'tsToDate')),
45            new \Twig\TwigFunction('dayIsBookable', array($this, 'dayIsBookable')),
46            new \Twig\TwigFunction('azPrefixList', array($this, 'azPrefixList')),
47            new \Twig\TwigFunction('formatPhoneNumber', array($this, 'formatPhoneNumber')),
48            new \Twig\TwigFunction('getOSMOptions', array($this, 'getOSMOptions')),
49            new \Twig\TwigFunction('getOSMAccessToken', array($this, 'getOSMAccessToken')),
50            new \Twig\TwigFunction('getD115Enabeld', array($this, 'getD115Enabeld')),
51            new \Twig\TwigFunction('getD115OpeningTimes', array($this, 'getD115OpeningTimes')),
52            new \Twig\TwigFunction('getD115Text', array($this, 'getD115Text')),
53            new \Twig\TwigFunction('getBobbiChatButtonEnabeld', array($this, 'getBobbiChatButtonEnabeld')),
54            new \Twig\TwigFunction('currentRoute', array($this, 'currentRoute')),
55            new \Twig\TwigFunction('dump', array($this, 'dump')),
56            new \Twig\TwigFunction(
57                'getAppointmentForLocationFromServiceAppointmentLocations',
58                array($this, 'getAppointmentForLocationFromServiceAppointmentLocations')
59            ),
60        );
61    }
62
63    public function dump($item)
64    {
65        return '<pre>' . print_r($item, 1) . '</pre>';
66    }
67
68    public function currentRoute($lang = null)
69    {
70        if ($this->container->has('currentRoute')) {
71            $routeParams = $this->container->get('currentRouteParams');
72
73            if (null !== $lang && 'de' == $lang) {
74                unset($routeParams['lang']);
75            } else {
76                $routeParams['lang'] = ($lang !== null) ? $lang : \App::$language->getCurrentLanguage();
77            }
78
79
80            $routeName = $this->container->get('currentRoute');
81            $route = array(
82                'name' => $routeName,
83                'params' => $routeParams
84            );
85        } else {
86            $route = array(
87                'name' => 'noroute',
88                'params' => []
89            );
90        }
91        return $route;
92    }
93
94
95    public function getD115Enabeld()
96    {
97        $settingsRepository = \App::$repository->fromSetting();
98        $active = (bool)($settingsRepository->fetchName('d115.active') ?? true);
99
100
101        return $active;
102    }
103
104    public function getD115OpeningTimes()
105    {
106        $settingsRepository = \App::$repository->fromSetting();
107        $openinTimes = $settingsRepository->fetchName('d115.openingTime') ?? \APP::D115_DEFAULT_OPENINGTIME;
108
109        return $openinTimes;
110    }
111
112    public function getD115Text()
113    {
114        $settingsRepository = \App::$repository->fromSetting();
115        $text = $settingsRepository->fetchName('d115.messageHtml') ?? \APP::D115_DEFAULT_TEXT;
116
117        return $text;
118    }
119
120    public function getBobbiChatButtonEnabeld()
121    {
122        $settingsRepository = \App::$repository->fromSetting();
123        $buttonEnabled = (bool)($settingsRepository->fetchName('frontend.bobbi.chatbutton.enabled') ?? false);
124
125        return $buttonEnabled;
126    }
127
128    public function getOSMAccessToken()
129    {
130        return \APP::OSM_ACCESS_TOKEN;
131    }
132
133    public function getOSMOptions()
134    {
135        return 'gestureHandling: ' . \APP::OSM_GESTURE_HANDLING;
136    }
137
138    public function formatPhoneNumber($phoneNumber)
139    {
140        preg_match_all('/(^\+)?[\d]+/', $phoneNumber, $matches);
141        $number = implode($matches[0]);
142        $phonenumber = preg_replace('/^030/', '+4930', $number);
143        return $phonenumber;
144    }
145
146    public function convertOpeningTimes($name)
147    {
148        $days = array(
149            'monday' => 'Montag',
150            'tuesday' => 'Dienstag',
151            'wednesday' => 'Mittwoch',
152            'thursday' => 'Donnerstag',
153            'friday' => 'Freitag',
154            'saturday' => 'Samstag',
155            'sunday' => 'Sonntag',
156            'special' => ''
157        );
158        return $days[$name];
159    }
160
161    public function dateToTS($datetime)
162    {
163        $timestamp = ($datetime === (int)$datetime) ? $datetime : strtotime($datetime);
164        return $timestamp;
165    }
166
167    public function tsToDate($timestamp)
168    {
169        $date =  date('Y-m-d', $timestamp);
170        return $date;
171    }
172
173    public function formatDateTime($dateString)
174    {
175        $dateTime = new \DateTimeImmutable($dateString, new \DateTimezone('Europe/Berlin'));
176        $formatDate['date']     = Helper\DateTime::getFormatedDates($dateTime, "EE, dd. MMMM yyyy");
177        $formatDate['fulldate'] = Helper\DateTime::getFormatedDates($dateTime, "EEEE, 'den' dd. MMMM yyyy");
178        $formatDate['weekday']  = ($dateTime->format('N') == 0) ?
179            $dateTime->format('N') + 6 :
180            $dateTime->format('N') - 1;
181        $formatDate['weekdayfull'] = Helper\DateTime::getFormatedDates($dateTime, "EEEE");
182
183        $time = $dateTime->format('H:i');
184        $formatDate['time']     = ($time != '00:00') ?
185            Helper\DateTime::getFormatedDates($dateTime, "HH:mm 'Uhr'") :
186            false;
187        $formatDate['timeId']   = ($time != '00:00') ? $time : false;
188        $formatDate['ts']       = $dateTime->getTimestamp();
189        $formatDate['dateId']   = $dateTime->format('Y-m-d');
190        $formatDate['ym']      = $dateTime->format('Y-m');
191
192        return $formatDate;
193    }
194
195    public function kindOfPayment($code)
196    {
197        $result = '';
198        if ($code == 0) {
199            $result = 'eccash';
200        } elseif ($code == 1) {
201            $result = 'nocash';
202        } elseif ($code == 2) {
203            $result = 'ec';
204        } elseif ($code == 3) {
205            $result = 'cash';
206        } elseif ($code == 4) {
207            $result = 'subscribecash';
208        }
209        return $result;
210    }
211
212    public function azPrefixList($list, $property)
213    {
214        $azList = array();
215        foreach ((array)$list as $item) {
216            if (!is_scalar($item) && Entity\Base::hasValidOffset($item, $property)) {
217                $currentPrefix = self::sortFirstChar($item[$property]);
218                if (!array_key_exists($currentPrefix, $azList)) {
219                    $azList[$currentPrefix] = array(
220                        'prefix' => $currentPrefix,
221                        'sublist' => array(),
222                    );
223                }
224                $azList[$currentPrefix]['sublist'][] = $item;
225                uasort($azList[$currentPrefix]['sublist'], array($this,'sortByName'));
226                ksort($azList);
227            }
228        }
229        return $azList;
230    }
231
232    protected static function sortFirstChar($string)
233    {
234        $firstChar = mb_substr($string, 0, 1);
235        $firstChar = mb_strtoupper($firstChar);
236        $firstChar = strtr($firstChar, array('Ä' => 'A', 'Ö' => 'O', 'Ü' => 'U'));
237        return $firstChar;
238    }
239
240    protected static function sortByName($left, $right)
241    {
242        return strcmp(
243            self::toSortableString(strtolower($left['name'])),
244            strtolower(self::toSortableString($right['name']))
245        );
246    }
247
248    protected static function toSortableString($string)
249    {
250        $string = strtr($string, array(
251            'Ä' => 'Ae',
252            'Ö' => 'Oe',
253            'Ü' => 'Ue',
254            'ä' => 'ae',
255            'ö' => 'oe',
256            'ü' => 'ue',
257            'ß' => 'ss',
258            '€' => 'E',
259        ));
260        return $string;
261    }
262
263    public function csvProperty($list, $property)
264    {
265        $propertylist = array();
266        foreach ($list as $item) {
267            if (!is_scalar($item) && Entity\Base::hasValidOffset($item, $property)) {
268                $propertylist[] = $item[$property];
269            }
270        }
271        return implode(',', array_unique($propertylist));
272    }
273
274    public function csvAppointmentLocations($list, $service_id = '')
275    {
276        $propertylist = array();
277        foreach ($list as $item) {
278            if (!is_scalar($item) && Entity\Base::hasValidOffset($item, 'services')) {
279                $appointment = $this->getAppointmentForService($item, $service_id);
280                if ($appointment === false) {
281                    continue;
282                }
283                if (false === $appointment['external'] && true === $appointment['allowed']) {
284                    $propertylist[] = $item['id'];
285                }
286            }
287        }
288        return implode(',', array_unique($propertylist));
289    }
290
291    /**
292     * @SuppressWarnings(PHPMD.LongVariable)
293     */
294    public function getAppointmentForLocationFromServiceAppointmentLocations(
295        array $serviceAppointmentLocationList,
296        $locationId
297    ) {
298        if (isset($serviceAppointmentLocationList[$locationId])) {
299            $appointment = $serviceAppointmentLocationList[$locationId]['appointment'];
300            $appointment['responsibility_hint'] = $serviceAppointmentLocationList[$locationId]['responsibility_hint'];
301            $appointment['responsibility'] = $serviceAppointmentLocationList[$locationId]['responsibility'];
302            return $appointment;
303        }
304        return false;
305    }
306
307    public function getAppointmentForService($location, $service_id)
308    {
309        $servicecompare = explode(',', $service_id);
310        foreach ($location['services'] as $service) {
311            if (in_array($service['service'], $servicecompare)) {
312                $appointment = $service['appointment'];
313                if ($appointment['allowed'] === true || $appointment['external'] === true) {
314                    return $appointment;
315                }
316            }
317        }
318        return false;
319    }
320
321    public function getLocationHintByServiceId($location, $service_id)
322    {
323        if (isset($location['services'])) {
324            foreach ($location['services'] as $service) {
325                if ($service['service'] == $service_id) {
326                    return $service['hint'];
327                }
328            }
329        }
330        return false;
331    }
332
333    public function dayIsBookable($dateList, $day)
334    {
335        $result = false;
336        if (count($dateList)) {
337            foreach ($dateList as $date) {
338                if ($date['datum'] == $day) {
339                    $result = $date;
340                }
341            }
342        }
343        return $result;
344    }
345}