Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.26% covered (warning)
72.26%
99 / 137
60.00% covered (warning)
60.00%
12 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessFormValidation
72.26% covered (warning)
72.26%
99 / 137
60.00% covered (warning)
60.00%
12 / 20
112.94
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
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 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
 getMailConfirmationParameters
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 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
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 1
5.23
 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
 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::getMailConfirmationParameters($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::getMailConfirmationParameters($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 getMailConfirmationParameters($collection)
136    {
137        if (1 == Validator::param('sendMailConfirmation')->isNumber()->getValue()) {
138            $collection['sendMailConfirmation'] = Validator::param('sendMailConfirmation')->isNumber();
139        }
140        return $collection;
141    }
142
143    protected static function testName($collection, $withAppointment)
144    {
145        $length = strlen(Validator::param('familyName')->isString()->getValue() ?? '');
146        if ($length || $withAppointment) {
147            $collection['familyName'] = Validator::param('familyName')->isString()
148                ->isBiggerThan(2, "Es muss ein aussagekräftiger Name eingegeben werden")
149                ->isSmallerThan(50, "Der Name sollte 50 Zeichen nicht überschreiten");
150        }
151        return $collection;
152    }
153
154    protected static function testMail($collection, $scopePrefs, $withAppointment)
155    {
156        $length = strlen(Validator::param('email')->isString()->getValue() ?? '');
157        if (self::isMailRequired($scopePrefs) && $withAppointment) {
158            $collection['email'] = Validator::param('email')
159                ->isMail("Die E-Mail Adresse muss im Format max@mustermann.de eingeben werden.")
160                ->isBiggerThan(6, "Für den Standort muss eine gültige E-Mail Adresse eingetragen werden");
161        }
162        if (self::hasCheckedMail() && !$length && $withAppointment) {
163            $collection['email'] = Validator::param('email')
164                ->isString()
165                ->isBiggerThan(6, "Für den Email-Versand muss eine gültige E-Mail Adresse angegeben werden");
166        }
167
168        if ($length) {
169            $collection['email'] = Validator::param('email')
170                ->isMail("Die E-Mail Adresse muss im Format max@mustermann.de eingeben werden.")
171                ->hasDNS(
172                    "Zu der angegebenen E-Mail-Adresse können keine Mails verschickt werden. " .
173                    "Der Host zur Domain nach dem '@' ist nicht erreichbar. " .
174                    ""
175                );
176        }
177        return $collection;
178    }
179
180    protected static function testSurvey($collection)
181    {
182        $length = strlen(Validator::param('email')->isString()->getValue() ?? '');
183        if (self::hasCheckedSurvey() && !$length) {
184            $collection['surveyAccepted'] = Validator::param('surveyAccepted')->isNumber();
185            $collection['email'] = Validator::param('email')->isString()->isBiggerThan(
186                6,
187                "Für die Teilnahme an der Umfrage muss eine gültige E-Mail Adresse angegeben werden"
188            );
189        }
190        return $collection;
191    }
192
193    protected static function testTelephone($collection, $scopePrefs, $withAppointment)
194    {
195        $inputNumber = Validator::param('telephone')->isString()->getValue();
196        if (! $inputNumber) {
197            return $collection;
198        }
199        $length = strlen($inputNumber);
200        $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
201        $phoneNumberObject = $phoneNumberUtil->parse($inputNumber, 'DE');
202        $phoneNumber = '+' . $phoneNumberObject->getCountryCode() . $phoneNumberObject->getNationalNumber();
203
204        if (self::isPhoneRequired($scopePrefs) && $withAppointment) {
205            $collection['telephone'] = Validator::value($phoneNumber, 'telephone')
206                ->isString()
207                ->isBiggerThan(6, "Zu kurz: für den Standort muss eine gültige Telefonnummer eingetragen werden")
208                ->isSmallerThan(15, "Zu lang: für den Standort muss eine gültige Telefonnummer eingetragen werden");
209        }
210
211        if ($length) {
212            $collection['telephone'] = Validator::value($phoneNumber, 'telephone')
213                ->isString()
214                ->isBiggerThan(6, "Zu kurz: für den Standort muss eine gültige Telefonnummer eingetragen werden")
215                ->isSmallerThan(15, "Zu lang: für den Standort muss eine gültige Telefonnummer eingetragen werden")
216                ->isMatchOf("/^\+?[\d\s]*$/", "Die Telefonnummer muss im Format 01701234567 eingegeben werden");
217        }
218        return $collection;
219    }
220
221    protected static function getAdditionalParameters($collection)
222    {
223        // amendment
224        if (!Validator::param('amendment')->isDeclared()->hasFailed()) {
225            $collection['amendment'] = Validator::param('amendment')->isString()
226                ->isSmallerThan(300, "Die Anmerkung sollte 300 Zeichen nicht überschreiten");
227        }
228
229        // agb gelesen
230        $collection['agbgelesen'] = Validator::param('agbgelesen')
231            ->isDeclared("Bitte akzeptieren Sie die Nutzungsbedingungen um einen Termin zu vereinbaren!");
232        if (!Validator::param('agbgelesen')->isDeclared()->hasFailed()) {
233            $collection['agbgelesen'] = Validator::param('agbgelesen')->isNumber();
234        }
235        return $collection;
236    }
237
238    protected static function getAdditionalAdminParameters($collection, $withAppointment = false)
239    {
240        // amendment
241        if (!Validator::param('amendment')->isDeclared()->hasFailed()) {
242            $collection['amendment'] = Validator::param('amendment')->isString()
243                ->isSmallerThan(300, "Die Anmerkung sollte 300 Zeichen nicht überschreiten");
244        }
245
246        // requests
247        if ($withAppointment) {
248            $collection['requests'] = Validator::param('requests')
249                ->isArray("Es muss mindestens eine Dienstleistung ausgewählt werden!");
250        }
251        return $collection;
252    }
253
254    protected static function isMailRequired($scopePrefs)
255    {
256        return (
257            Property::__keyExists('emailRequired', $scopePrefs['client']) &&
258            Property::__keyExists('emailFrom', $scopePrefs['client']) &&
259            $scopePrefs['client']['emailRequired'] &&
260            $scopePrefs['client']['emailFrom']
261        );
262    }
263
264    protected static function isPhoneRequired($scopePrefs)
265    {
266        return (
267            Property::__keyExists('telephoneRequired', $scopePrefs['client']) &&
268            $scopePrefs['client']['telephoneRequired']
269        );
270    }
271
272    protected static function hasCheckedMail()
273    {
274        return (1 == Validator::param('sendMailConfirmation')->isNumber()->getValue());
275    }
276
277    protected static function hasCheckedSurvey()
278    {
279        return (1 == Validator::param('surveyAccepted')->isNumber()->getValue());
280    }
281}