Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.78% covered (warning)
70.78%
109 / 154
66.67% covered (warning)
66.67%
14 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessFormValidation
70.78% covered (warning)
70.78%
109 / 154
66.67% covered (warning)
66.67%
14 / 21
145.85
0.00% covered (danger)
0.00%
0 / 1
 fromParameters
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromAdminParameters
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 fromManageProcess
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 fromParametersToProcess
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 setFormStatus
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 addClient
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 addReminderTimestamp
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addAmendment
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getPersonalParameters
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getNotificationParameters
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 testName
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 testMail
66.67% covered (warning)
66.67%
12 / 18
0.00% covered (danger)
0.00%
0 / 1
8.81
 testSurvey
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
5.20
 testTelephone
59.26% covered (warning)
59.26%
16 / 27
0.00% covered (danger)
0.00%
0 / 1
10.31
 getAdditionalParameters
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getAdditionalAdminParameters
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 isMailRequired
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 isPhoneRequired
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 hasCheckedSms
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 hasCheckedMail
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasCheckedSurvey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 *
5 * @package Zmsentities
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsentities\Helper;
11
12use BO\Mellon\Validator;
13use BO\Zmsentities\Helper\Property;
14
15/**
16 *
17 * @SuppressWarnings(Complexity)
18 * @todo Deprecated, only for d115 mandant in use, delete after changed to Validator\ProcessValidator
19 *
20 */
21class ProcessFormValidation
22{
23    /**
24     * form data for reuse in multiple controllers
25     */
26    public static function fromParameters($scopePrefs = array(), $withAppointment = false)
27    {
28        $collection = array();
29        $collection = self::getPersonalParameters($collection, $scopePrefs, $withAppointment);
30        $collection = self::getAdditionalParameters($collection);
31        $collection = self::getNotificationParameters($collection);
32
33        // return validated collection
34        $collection = Validator::collection($collection);
35        return $collection;
36    }
37
38    /**
39     * form data for reuse in multiple controllers
40     */
41    public static function fromAdminParameters($scopePrefs = array(), $withAppointment = false)
42    {
43        $collection = array();
44        $collection = self::getPersonalParameters($collection, $scopePrefs, $withAppointment);
45        $collection = self::getAdditionalAdminParameters($collection, $withAppointment);
46        $collection = self::getNotificationParameters($collection);
47
48        // return validated collection
49        $collection = Validator::collection($collection);
50        return $collection;
51    }
52
53    public static function fromManageProcess()
54    {
55        // processId
56        $collection['process'] = Validator::param('process')
57            ->isNumber("Es muss eine valide Vorgangsnummer eingegeben werden");
58        // authKey
59        $collection['authKey'] = Validator::param('authKey')
60            ->isString()
61            ->isBiggerThan(4, "Der Absagecode ist nicht korrekt")
62            ->isSmallerThan(50, "Der Absagecode ist nicht korrekt");
63        // return validated collection
64        $collection = Validator::collection($collection);
65        return $collection;
66    }
67
68    public static function fromParametersToProcess($process, $withAppointment = true)
69    {
70        $form = self::fromParameters($process->scope['preferences'], $withAppointment);
71        $formData = self::setFormStatus($form);
72        if (isset($formData['failed']) && ! $formData['failed']) {
73            $process = self::addClient($process, $formData);
74            $process = self::addReminderTimestamp($process, $formData);
75            $process = self::addAmendment($process, $formData);
76        }
77        return array(
78            'process' => $process,
79            'formdata' => $formData
80        );
81    }
82
83    protected static function setFormStatus($form)
84    {
85        $validate = Validator::param('form_validate')->isBool()->getValue();
86        $formData = ($validate) ? $form->getStatus(null, true) : null;
87        if (!$form->hasFailed()) {
88            $formData['failed'] = false;
89        } elseif (null !== $formData) {
90            $formData['failed'] = true;
91        }
92        return $formData;
93    }
94
95    protected static function addClient($process, $formData)
96    {
97        $client = new \BO\Zmsentities\Client();
98        foreach ($formData as $key => $item) {
99            if ($client->offsetExists($key) && null !== $item['value']) {
100                $client[$key] = $item['value'];
101            }
102        }
103        $process->clients = [$client];
104
105        return $process;
106    }
107
108    protected static function addReminderTimestamp($process, $formData)
109    {
110        $process->reminderTimestamp = 0;
111        if (isset($formData['headsUpTime'])) {
112            $process->reminderTimestamp = $formData['headsUpTime']['value'];
113        }
114        return $process;
115    }
116
117    protected static function addAmendment($process, $formData)
118    {
119        $process->amendment = '';
120        if (isset($formData['amendment'])) {
121            $process->amendment = $formData['amendment']['value'];
122        }
123        return $process;
124    }
125
126    protected static function getPersonalParameters($collection, $scopePrefs, $withAppointment = false)
127    {
128        $collection = static::testName($collection, $withAppointment);
129        $collection = static::testMail($collection, $scopePrefs, $withAppointment);
130        $collection = static::testTelephone($collection, $scopePrefs, $withAppointment);
131        $collection = static::testSurvey($collection);
132        return $collection;
133    }
134
135    protected static function getNotificationParameters($collection)
136    {
137        // confirmation notification
138        if (1 == Validator::param('sendConfirmation')->isNumber()->getValue()) {
139            $collection['sendConfirmation'] = Validator::param('sendConfirmation')->isNumber();
140        }
141
142        // confirmation mail
143        if (1 == Validator::param('sendMailConfirmation')->isNumber()->getValue()) {
144            $collection['sendMailConfirmation'] = Validator::param('sendMailConfirmation')->isNumber();
145        }
146
147        // reminder notification
148        if (1 == Validator::param('sendReminder')->isNumber()->getValue()) {
149            $collection['sendReminder'] = Validator::param('sendReminder')->isNumber();
150            $collection['headsUpTime'] = Validator::param('headsUpTime')->isNumber();
151        }
152        return $collection;
153    }
154
155    protected static function testName($collection, $withAppointment)
156    {
157        $length = strlen(Validator::param('familyName')->isString()->getValue());
158        if ($length || $withAppointment) {
159            $collection['familyName'] = Validator::param('familyName')->isString()
160                ->isBiggerThan(2, "Es muss ein aussagekräftiger Name eingegeben werden")
161                ->isSmallerThan(50, "Der Name sollte 50 Zeichen nicht überschreiten");
162        }
163        return $collection;
164    }
165
166    protected static function testMail($collection, $scopePrefs, $withAppointment)
167    {
168        $length = strlen(Validator::param('email')->isString()->getValue());
169        if (self::isMailRequired($scopePrefs) && $withAppointment) {
170            $collection['email'] = Validator::param('email')
171                ->isMail("Die E-Mail Adresse muss im Format max@mustermann.de eingeben werden.")
172                ->isBiggerThan(6, "Für den Standort muss eine gültige E-Mail Adresse eingetragen werden");
173        }
174        if (self::hasCheckedMail() && !$length && $withAppointment) {
175            $collection['email'] = Validator::param('email')
176                ->isString()
177                ->isBiggerThan(6, "Für den Email-Versand muss eine gültige E-Mail Adresse angegeben werden");
178        }
179
180        if ($length) {
181            $collection['email'] = Validator::param('email')
182                ->isMail("Die E-Mail Adresse muss im Format max@mustermann.de eingeben werden.")
183                ->hasDNS(
184                    "Zu der angegebenen E-Mail-Adresse können keine Mails verschickt werden. " .
185                    "Der Host zur Domain nach dem '@' ist nicht erreichbar. " .
186                    ""
187                );
188        }
189        return $collection;
190    }
191
192    protected static function testSurvey($collection)
193    {
194        $length = strlen(Validator::param('email')->isString()->getValue());
195        if (self::hasCheckedSurvey() && !$length) {
196            $collection['surveyAccepted'] = Validator::param('surveyAccepted')->isNumber();
197            $collection['email'] = Validator::param('email')->isString()->isBiggerThan(
198                6,
199                "Für die Teilnahme an der Umfrage muss eine gültige E-Mail Adresse angegeben werden"
200            );
201        }
202        return $collection;
203    }
204
205    protected static function testTelephone($collection, $scopePrefs, $withAppointment)
206    {
207        $inputNumber = Validator::param('telephone')->isString()->getValue();
208        if (! $inputNumber) {
209            return $collection;
210        }
211        $length = strlen($inputNumber);
212        $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
213        $phoneNumberObject = $phoneNumberUtil->parse($inputNumber, 'DE');
214        $phoneNumber = '+' . $phoneNumberObject->getCountryCode() . $phoneNumberObject->getNationalNumber();
215
216        if (self::isPhoneRequired($scopePrefs) && $withAppointment) {
217            $collection['telephone'] = Validator::value($phoneNumber, 'telephone')
218                ->isString()
219                ->isBiggerThan(6, "Zu kurz: für den Standort muss eine gültige Telefonnummer eingetragen werden")
220                ->isSmallerThan(15, "Zu lang: für den Standort muss eine gültige Telefonnummer eingetragen werden");
221        }
222
223        if (self::hasCheckedSms() && !$length) {
224            $collection['telephone'] = Validator::value($phoneNumber, 'telephone')
225                ->isString()
226                ->isBiggerThan(10, "Zu kurz: für den SMS-Versand muss eine gültige Mobilfunknummer angegeben werden")
227                ->isSmallerThan(
228                    15,
229                    "Zu lang: für den SMS-Versand muss eine gültige Mobilfunknummer angegeben werden"
230                );
231        }
232
233        if ($length) {
234            $collection['telephone'] = Validator::value($phoneNumber, 'telephone')
235                ->isString()
236                ->isBiggerThan(6, "Zu kurz: für den Standort muss eine gültige Telefonnummer eingetragen werden")
237                ->isSmallerThan(15, "Zu lang: für den Standort muss eine gültige Telefonnummer eingetragen werden")
238                ->isMatchOf("/^\+?[\d\s]*$/", "Die Telefonnummer muss im Format 01701234567 eingegeben werden");
239        }
240        return $collection;
241    }
242
243    protected static function getAdditionalParameters($collection)
244    {
245        // amendment
246        if (!Validator::param('amendment')->isDeclared()->hasFailed()) {
247            $collection['amendment'] = Validator::param('amendment')->isString()
248                ->isSmallerThan(300, "Die Anmerkung sollte 300 Zeichen nicht überschreiten");
249        }
250
251        // agb gelesen
252        $collection['agbgelesen'] = Validator::param('agbgelesen')
253            ->isDeclared("Bitte akzeptieren Sie die Nutzungsbedingungen um einen Termin zu vereinbaren!");
254        if (!Validator::param('agbgelesen')->isDeclared()->hasFailed()) {
255            $collection['agbgelesen'] = Validator::param('agbgelesen')->isNumber();
256        }
257        return $collection;
258    }
259
260    protected static function getAdditionalAdminParameters($collection, $withAppointment = false)
261    {
262        // amendment
263        if (!Validator::param('amendment')->isDeclared()->hasFailed()) {
264            $collection['amendment'] = Validator::param('amendment')->isString()
265                ->isSmallerThan(300, "Die Anmerkung sollte 300 Zeichen nicht überschreiten");
266        }
267
268        // requests
269        if ($withAppointment) {
270            $collection['requests'] = Validator::param('requests')
271                ->isArray("Es muss mindestens eine Dienstleistung ausgewählt werden!");
272        }
273        return $collection;
274    }
275
276    protected static function isMailRequired($scopePrefs)
277    {
278        return (
279            Property::__keyExists('emailRequired', $scopePrefs['client']) &&
280            Property::__keyExists('emailFrom', $scopePrefs['client']) &&
281            $scopePrefs['client']['emailRequired'] &&
282            $scopePrefs['client']['emailFrom']
283        );
284    }
285
286    protected static function isPhoneRequired($scopePrefs)
287    {
288        return (
289            Property::__keyExists('telephoneRequired', $scopePrefs['client']) &&
290            $scopePrefs['client']['telephoneRequired']
291        );
292    }
293
294    protected static function hasCheckedSms()
295    {
296        return (
297            1 == Validator::param('sendConfirmation')->isNumber()->getValue() ||
298            1 == Validator::param('sendReminder')->isNumber()->getValue()
299        );
300    }
301
302    protected static function hasCheckedMail()
303    {
304        return (1 == Validator::param('sendMailConfirmation')->isNumber()->getValue());
305    }
306
307    protected static function hasCheckedSurvey()
308    {
309        return (1 == Validator::param('surveyAccepted')->isNumber()->getValue());
310    }
311}