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