Total Complexity | 48 |
Total Lines | 337 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Respondent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Respondent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Respondent extends MyActiveRecord |
||
26 | { |
||
27 | const MAX_ALTERNATIVE_CONTACTS = 20; |
||
28 | /** @var bool $checkDSNForEmails whether email validation also used DSN records to check if domain exists */ |
||
29 | public static $checkDSNForEmails = true; |
||
30 | |||
31 | /** |
||
32 | * @var array $surveyIdentifyingColumns names of the columns that identify a respondent as unique inside a survey |
||
33 | */ |
||
34 | public static $surveyIdentifyingColumns = ['phone_number', 'email_address']; |
||
35 | |||
36 | public function init() |
||
39 | } |
||
40 | |||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function rules() |
||
64 | } |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param string $attribute |
||
69 | * @param string $address |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function validateEmail($attribute, $address = null) |
||
73 | { |
||
74 | |||
75 | if ($this->validateEmailFormat($attribute, $address) |
||
76 | && !$this->isSameAsMainAddress($attribute, $address) |
||
77 | && !$this->isEmailSurveyDuplicate($attribute, $address)) { |
||
78 | return true; |
||
79 | } |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $attribute |
||
85 | * @param string $address |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function validateEmailFormat($attribute, $address = null) |
||
89 | { |
||
90 | $validator = new yii\validators\EmailValidator(); |
||
91 | $validator->checkDNS = static::$checkDSNForEmails; |
||
92 | if (!$validator->validate($address)) { |
||
93 | $this->addError($attribute, |
||
94 | Yii::t('app', |
||
95 | 'Invalid email address "{0}"', [$address] |
||
96 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Invalid email format')]) |
||
97 | ); |
||
98 | return false; |
||
99 | } |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $attribute |
||
105 | * @param string $address |
||
106 | * @return bool |
||
107 | */ |
||
108 | private function isSameAsMainAddress($attribute, $address = null) |
||
122 | } |
||
123 | |||
124 | |||
125 | public function validateMultipleEmails($attribute) |
||
126 | { |
||
127 | $cleanAddresses = []; |
||
128 | $addresses = yii\helpers\Json::decode($this->alternative_email_addresses); |
||
129 | if (!empty($addresses)) { |
||
130 | $i = 0; |
||
131 | foreach ($addresses as $key => $address) { |
||
132 | $i++; |
||
133 | // check the alternative numbers of that model for duplicates |
||
134 | $checkItems = $addresses; |
||
135 | unset($checkItems[$key]); |
||
136 | if (in_array($address, $checkItems)) { |
||
137 | $this->addError($attribute, Yii::t('app', 'Duplicate email in alternative email addresses')); |
||
138 | } |
||
139 | |||
140 | if ($i >= static::MAX_ALTERNATIVE_CONTACTS) { |
||
141 | $this->addError($attribute, Yii::t('app', 'Maximum alternative addresses limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->email_address])); |
||
142 | } |
||
143 | $address = strtolower(trim($address)); |
||
144 | if ($this->validateEmail($attribute, $address)) { |
||
145 | $cleanAddresses[] = $address; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | $this->alternative_email_addresses = yii\helpers\Json::encode($cleanAddresses); |
||
150 | } |
||
151 | |||
152 | |||
153 | public function validatePhoneNumber($attribute, $phone_number = null) |
||
154 | { |
||
155 | $this->validateSameAsMainNumber($attribute, $phone_number); |
||
156 | // TODO |
||
157 | $isValidFormat = true; |
||
|
|||
158 | $this->validatePhoneSurveyDuplicate($attribute, $phone_number); |
||
159 | } |
||
160 | |||
161 | public function validateMultiplePhoneNumbers($attribute) |
||
162 | { |
||
163 | $cleanItems = []; |
||
164 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
||
165 | if (!empty($items)) { |
||
166 | $i = 0; |
||
167 | foreach ($items as $key => $item) { |
||
168 | $i++; |
||
169 | $item = strtolower(trim($item)); |
||
170 | $this->validateAlternativePhoneNumberInternalDuplicates($attribute, $item, $key); |
||
171 | |||
172 | if ($i >= static::MAX_ALTERNATIVE_CONTACTS) { |
||
173 | $this->addError($attribute, Yii::t('app', 'Maximum alternative phone numbers limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->phone_number])); |
||
174 | } |
||
175 | $this->validatePhoneNumber($attribute, $item); |
||
176 | } |
||
177 | } |
||
178 | $this->alternative_phone_numbers = yii\helpers\Json::encode($cleanItems); |
||
179 | } |
||
180 | |||
181 | private function validateAlternativePhoneNumberInternalDuplicates($attribute, $number, $key) |
||
182 | { |
||
183 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
||
184 | $checkItems = $items; |
||
185 | unset($checkItems[$key]); |
||
186 | if (in_array($number, $checkItems)) { |
||
187 | $this->addError($attribute, Yii::t('app', 'Duplicate number in alternative phone numbers')); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | |||
192 | /** |
||
193 | * @param string $attribute |
||
194 | * @param string $number |
||
195 | * @return null |
||
196 | */ |
||
197 | private function validateSameAsMainNumber($attribute, $number = null) |
||
198 | { |
||
199 | if (!$number or empty($number)) { |
||
200 | return null; |
||
201 | } |
||
202 | |||
203 | $isSame = ($attribute == 'phone_number' ? false : $number == $this->phone_number); |
||
204 | |||
205 | if ($isSame) { |
||
206 | $this->addError($attribute, |
||
207 | Yii::t('app', |
||
208 | 'Invalid phone number "{0}"', [$number] |
||
209 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute . ' duplicates main phone number')]) |
||
210 | ); |
||
211 | } |
||
212 | return null; |
||
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @param string $attribute |
||
218 | * @param string $phone_number Phone number to check duplicates for |
||
219 | */ |
||
220 | private function validatePhoneSurveyDuplicate($attribute, $phone_number) |
||
221 | { |
||
222 | $query = static::find(); |
||
223 | // check only this survey |
||
224 | $query->andWhere(['survey_id' => $this->survey_id]); |
||
225 | |||
226 | if ($this->respondent_id) { |
||
227 | // not itself |
||
228 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
||
229 | } |
||
230 | |||
231 | $condition = ['or', |
||
232 | '`phone_number`=:phone_number', |
||
233 | '`alternative_phone_numbers` LIKE :phone_number2', |
||
234 | ]; |
||
235 | |||
236 | $query->andWhere($condition, [':phone_number' => $phone_number, ':phone_number2' => '%\"' . $phone_number . '\"%']); |
||
237 | |||
238 | if ($query->count() > 0) { |
||
239 | $this->addError($attribute, |
||
240 | Yii::t('app', |
||
241 | 'Invalid phone number "{0}"', [$phone_number] |
||
242 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicate phone number')]) |
||
243 | ); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | |||
248 | /** |
||
249 | * @param string $email_address Email address to check duplicates for |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isEmailSurveyDuplicate($attribute, $email_address) |
||
253 | { |
||
254 | $query = static::find(); |
||
255 | // check only this survey |
||
256 | $query->andWhere(['survey_id' => $this->survey_id]); |
||
257 | // not itself |
||
258 | if ($this->respondent_id) { |
||
259 | // not itself |
||
260 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
||
261 | } |
||
262 | |||
263 | $email_condition = ['or', |
||
264 | '`email_address`=:email_address', |
||
265 | '`alternative_email_addresses` LIKE :email_address2', |
||
266 | ]; |
||
267 | $query->andWhere($email_condition, [':email_address' => $email_address, ':email_address2' => '%\"' . $email_address . '\"%']); |
||
268 | if ($query->count() > 0) { |
||
269 | return true; |
||
270 | } |
||
271 | |||
272 | $this->addError($attribute, |
||
273 | Yii::t('app', |
||
274 | 'Invalid email address "{0}"', [$email_address] |
||
275 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicates some other address')]) |
||
276 | ); |
||
277 | |||
278 | return false; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function attributeLabels() |
||
288 | ]; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param string $token Respondents token |
||
293 | * @return static |
||
294 | */ |
||
295 | public static function findByToken($token = null) |
||
296 | { |
||
297 | if ($token) { |
||
298 | $models = self::findByTokens([$token]); |
||
299 | if (!empty($models)) { |
||
300 | return $models[0]; |
||
301 | } |
||
302 | } |
||
303 | return null; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @param string[] $tokens Respondents tokens |
||
308 | * @return static[] |
||
309 | */ |
||
310 | public static function findByTokens($tokens) |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Check whether respondent has rejected this specific survey or has a hard bounce on e_mail address |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function getIsRejected() |
||
324 | { |
||
325 | if (Rejection::rejectedByCode($this->token)) { |
||
326 | return true; |
||
327 | } |
||
328 | |||
329 | if (Rejection::bouncedByEmailAddress($this->email_address)) { |
||
330 | return true; |
||
331 | } |
||
332 | |||
333 | return false; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Get the last respondent based on Email-address |
||
338 | * @param string $email_address |
||
339 | * @return static |
||
340 | */ |
||
341 | public static function getLatestByEmail($email_address) |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getShortToken() |
||
362 | } |
||
363 | |||
364 | |||
365 | } |