Completed
Branch FET/reg-form-builder/extract-a... (6e8a58)
by
unknown
35:36 queued 25:38
created

RegistrantData   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 354
rs 9.36
c 0
b 0
f 0
wmc 38
lcom 2
cbo 2

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initializeRegistrantData() 0 8 1
A attendeeCount() 0 4 1
A copyPrimary() 0 4 1
A setCopyPrimary() 0 4 1
A getRegistrant() 0 4 1
A getRegistrantData() 0 4 1
A addRegistrantDataValue() 0 4 1
B ensureCriticalRegistrantDataIsSet() 0 34 6
A setRegistrantData() 0 4 1
A getRequiredQuestions() 0 4 1
A addRequiredQuestion() 0 4 1
A registrantAnswers() 0 4 1
A addRegistrantAnswer() 0 4 1
A getRegistrantAnswer() 0 4 1
A registrantAnswerIsObject() 0 5 1
A primaryRegistrantData() 0 4 1
A addPrimaryRegistrantDataValue() 0 4 1
A getPrimaryRegistrantDataValue() 0 4 1
A setPrimaryRegistrantData() 0 4 1
A primaryRegistrant() 0 4 1
A primaryRegistrantIsValid() 0 4 1
A setPrimaryRegistrant() 0 4 1
A currentRegistrantIsPrimary() 0 9 4
A currentRegistrantIsNotPrimary() 0 4 1
A saveOrCopyPrimaryRegistrantData() 0 13 5
1
<?php
2
3
namespace EventEspresso\core\domain\services\registration\form\v1;
4
5
use EE_Answer;
6
use EE_Attendee;
7
use EE_Error;
8
use EE_Registration;
9
use ReflectionException;
10
11
/**
12
 * Class RegistrantData
13
 * DTO for tracking and sharing data during the registration process
14
 *
15
 * @author  Brent Christensen
16
 * @package EventEspresso\core\domain\services\registration\form\v1
17
 * @since   $VID:$
18
 */
19
class RegistrantData
20
{
21
22
    /**
23
     * @var int
24
     */
25
    private $attendee_counter = 0;
26
27
    /**
28
     * @var array
29
     */
30
    private $registrant_data = [];
31
32
    /**
33
     * @var bool
34
     */
35
    private $copy_primary = false;
36
37
    /**
38
     * @var array
39
     */
40
    private $required_questions = [];
41
42
    /**
43
     * @var EE_Registration[]
44
     */
45
    private $registrations = [];
46
47
    /**
48
     * @var EE_Answer[][]
49
     */
50
    private $registrant_answers = [];
51
52
    /**
53
     * array for tracking reg form data for the primary registrant
54
     *
55
     * @var array
56
     */
57
    private $primary_registrant_data;
58
59
    /**
60
     * the attendee object created for the primary registrant
61
     *
62
     * @var EE_Attendee
63
     */
64
    private $primary_registrant;
65
66
67
    /**
68
     * RegistrantData constructor.
69
     */
70
    public function __construct()
71
    {
72
        $this->primary_registrant_data = ['line_item_id' => null,];
73
    }
74
75
76
    /**
77
     * @param EE_Registration $registration
78
     * @throws EE_Error
79
     */
80
    public function initializeRegistrantData(EE_Registration $registration): void
81
    {
82
        $reg_url_link = $registration->reg_url_link();
83
        $this->registrations[ $reg_url_link ] = $registration;
84
        $this->registrant_answers[ $reg_url_link ] = $registration->answers();
85
        $this->registrant_data[ $reg_url_link ] = [];
86
        $this->attendee_counter++;
87
    }
88
89
90
    /**
91
     * @return int
92
     */
93
    public function attendeeCount(): int
94
    {
95
        return $this->attendee_counter;
96
    }
97
98
99
    /**
100
     * @return bool
101
     */
102
    public function copyPrimary(): bool
103
    {
104
        return $this->copy_primary;
105
    }
106
107
108
    /**
109
     * @param bool $copy_primary
110
     */
111
    public function setCopyPrimary(bool $copy_primary): void
112
    {
113
        $this->copy_primary = filter_var($copy_primary, FILTER_VALIDATE_BOOLEAN);
114
    }
115
116
117
    /**
118
     * @param string $reg_url_link
119
     * @return array|null
120
     */
121
    public function getRegistrant(string $reg_url_link): ?EE_Registration
122
    {
123
        return $this->registrations[ $reg_url_link ] ?? null;
124
    }
125
126
127
    /**
128
     * @param string $reg_url_link
129
     * @return array|null
130
     */
131
    public function getRegistrantData(string $reg_url_link): ?array
132
    {
133
        return $this->registrant_data[ $reg_url_link ] ?? null;
134
    }
135
136
137
    /**
138
     * @param string $reg_url_link
139
     * @param string $key
140
     * @param mixed $value
141
     */
142
    public function addRegistrantDataValue(string $reg_url_link, string $key, $value): void
143
    {
144
        $this->registrant_data[ $reg_url_link ][ $key ] = $value;
145
    }
146
147
148
    /**
149
     * ensures that all attendees at least have data for first name, last name, and email address
150
     *
151
     * @param string $reg_url_link
152
     * @throws EE_Error
153
     * @throws ReflectionException
154
     */
155
    public function ensureCriticalRegistrantDataIsSet(string $reg_url_link): void {
156
        if ($this->currentRegistrantIsPrimary()) {
157
            return;
158
        }
159
        // bare minimum critical details include first name, last name, email address
160
        $critical_attendee_details = ['ATT_fname', 'ATT_lname', 'ATT_email'];
161
        // add address info to critical details?
162
        if (
163
        apply_filters(
164
            'FHEE__EE_SPCO_Reg_Step_Attendee_Information__merge_address_details_with_critical_attendee_details',
165
            false
166
        )
167
        ) {
168
            $critical_attendee_details += [
169
                'ATT_address',
170
                'ATT_address2',
171
                'ATT_city',
172
                'STA_ID',
173
                'CNT_ISO',
174
                'ATT_zip',
175
                'ATT_phone',
176
            ];
177
        }
178
        foreach ($critical_attendee_details as $critical_attendee_detail) {
179
            if (
180
                ! isset($this->registrant_data[ $reg_url_link ][ $critical_attendee_detail ])
181
                || empty($this->registrant_data[ $reg_url_link ][ $critical_attendee_detail ])
182
            ) {
183
                $this->registrant_data[ $reg_url_link ][ $critical_attendee_detail ] = $this->primary_registrant->get(
184
                    $critical_attendee_detail
185
                );
186
            }
187
        }
188
    }
189
190
191
    /**
192
     * @param string $reg_url_link
193
     * @param array $registrant_data
194
     */
195
    public function setRegistrantData(string $reg_url_link, array $registrant_data): void
196
    {
197
        $this->registrant_data[ $reg_url_link ] = $registrant_data;
198
    }
199
200
201
    /**
202
     * @return array
203
     */
204
    public function getRequiredQuestions(): array
205
    {
206
        return $this->required_questions;
207
    }
208
209
210
    /**
211
     * @param string $identifier
212
     * @param string $required_question
213
     */
214
    public function addRequiredQuestion(string $identifier, string $required_question): void
215
    {
216
        $this->required_questions[ $identifier ] = $required_question;
217
    }
218
219
220
    /**
221
     * @return EE_Answer[]
222
     */
223
    public function registrantAnswers(string $reg_url_link): array
224
    {
225
        return $this->registrant_answers[ $reg_url_link ] ?? [];
226
    }
227
228
229
    /**
230
     * @param string $reg_url_link
231
     * @param string $identifier  the answer cache ID
232
     * @param EE_Answer $answer
233
     */
234
    public function addRegistrantAnswer(string $reg_url_link, string $identifier, EE_Answer $answer): void
235
    {
236
        $this->registrant_answers[ $reg_url_link ][ $identifier ] = $answer;
237
    }
238
239
240
    /**
241
     * @param string $reg_url_link
242
     * @param string $identifier
243
     * @return EE_Answer|null
244
     */
245
    public function getRegistrantAnswer(string $reg_url_link, string $identifier): ?EE_Answer
246
    {
247
        return $this->registrant_answers[ $reg_url_link ][ $identifier ] ?? null;
248
    }
249
250
251
252
    /**
253
     * @param string $reg_url_link
254
     * @param string $identifier
255
     * @return bool
256
     */
257
    public function registrantAnswerIsObject(string $reg_url_link, string $identifier): bool
258
    {
259
        $registrant_answer = $this->getRegistrantAnswer($reg_url_link, $identifier);
260
        return $registrant_answer instanceof EE_Answer;
261
    }
262
263
264
    /**
265
     * @return array
266
     */
267
    public function primaryRegistrantData(): array
268
    {
269
        return $this->primary_registrant_data;
270
    }
271
272
273
    /**
274
     * @param string $key
275
     * @param mixed  $value
276
     */
277
    public function addPrimaryRegistrantDataValue(string $key, $value): void
278
    {
279
        $this->primary_registrant_data[ $key ] = $value;
280
    }
281
282
283
    /**
284
     * @param string $key
285
     * @return mixed
286
     */
287
    public function getPrimaryRegistrantDataValue(string $key)
288
    {
289
        return $this->primary_registrant_data[ $key ] ?? null;
290
    }
291
292
293
    /**
294
     * @param array $primary_registrant_data
295
     */
296
    public function setPrimaryRegistrantData(array $primary_registrant_data): void
297
    {
298
        $this->primary_registrant_data = $primary_registrant_data;
299
    }
300
301
302
    /**
303
     * @return EE_Attendee
304
     */
305
    public function primaryRegistrant(): EE_Attendee
306
    {
307
        return $this->primary_registrant;
308
    }
309
310
311
    /**
312
     * @return bool
313
     */
314
    public function primaryRegistrantIsValid(): bool
315
    {
316
        return $this->primary_registrant instanceof EE_Attendee;
317
    }
318
319
320
    /**
321
     * @param EE_Attendee $primary_registrant
322
     */
323
    public function setPrimaryRegistrant(EE_Attendee $primary_registrant): void
324
    {
325
        $this->primary_registrant = $primary_registrant;
326
    }
327
328
329
    /**
330
     * @param string $reg_url_link
331
     * @return bool
332
     */
333
    public function currentRegistrantIsPrimary(string $reg_url_link = ''): bool
334
    {
335
        return $this->attendeeCount() === 1
336
            || (
337
                $this->attendeeCount() === 1
338
                && $reg_url_link !== ''
339
                && $this->getPrimaryRegistrantDataValue('reg_url_link') === $reg_url_link
340
           );
341
    }
342
343
344
    /**
345
     * @return bool
346
     */
347
    public function currentRegistrantIsNotPrimary(): bool
348
    {
349
        return $this->attendeeCount() > 1;
350
    }
351
352
353
    /**
354
     * @param string $reg_url_link
355
     * @param string $form_input
356
     * @param mixed  $input_value
357
     * @return mixed|null
358
     */
359
    public function saveOrCopyPrimaryRegistrantData(string $reg_url_link, string $form_input, $input_value)
360
    {
361
        // store a bit of data about the primary attendee
362
        if (! empty($input_value) && $this->currentRegistrantIsPrimary($reg_url_link)) {
363
            $this->primary_registrant_data[ $form_input ] = $input_value;
364
            return $input_value;
365
        }
366
        // or copy value from primary if incoming value is not set
367
        if ($input_value === null && $this->copyPrimary()) {
368
            $input_value = $this->getPrimaryRegistrantDataValue($form_input);
369
        }
370
        return $input_value;
371
    }
372
}
373