Total Complexity | 55 |
Total Lines | 342 |
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 | /** |
||
69 | * @param string $attribute |
||
70 | * @param string $address |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function validateEmail($attribute,$address = null){ |
||
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) |
||
109 | { |
||
110 | if(!$address or empty($address)){ |
||
111 | return false; |
||
112 | } |
||
113 | $isSame = ($attribute=='email_address' ? false : $address == $this->email_address); |
||
114 | if ($isSame) { |
||
115 | $this->addError($attribute, |
||
116 | Yii::t('app', |
||
117 | 'Invalid email address "{0}"',[$address] |
||
118 | ).' '.Yii::t('app','Reason: {0}',[Yii::t('app',$attribute. ' duplicates main address')]) |
||
119 | ); |
||
120 | } |
||
121 | return $isSame; |
||
122 | } |
||
123 | |||
124 | |||
125 | public function validateMultipleEmails($attribute){ |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | |||
161 | public function validatePhoneNumber($attribute, $phone_number = null){ |
||
162 | $this->validateSameAsMainNumber($attribute, $phone_number); |
||
163 | // TODO |
||
164 | $isValidFormat = true; |
||
|
|||
165 | $this->validatePhoneSurveyDuplicate($attribute, $phone_number); |
||
166 | } |
||
167 | |||
168 | public function validateMultiplePhoneNumbers($attribute){ |
||
169 | if($this->alternative_phone_numbers){ |
||
170 | $cleanItems = []; |
||
171 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
||
172 | |||
173 | if(!empty($items)){ |
||
174 | $i=0; |
||
175 | foreach ($items as $key=> $item){ |
||
176 | $item = strtolower(trim($item)); |
||
177 | if($item <> ''){ |
||
178 | $i++; |
||
179 | // check the alternative numbers of that model for duplicates |
||
180 | $checkItems = $items; |
||
181 | unset($checkItems[$key]); |
||
182 | if(in_array($item,$checkItems)){ |
||
183 | $this->addError($attribute,Yii::t('app','Duplicate number in alternative phone numbers')); |
||
184 | } |
||
185 | |||
186 | |||
187 | if($i>=static::MAX_ALTERNATIVE_CONTACTS){ |
||
188 | $this->addError($attribute,Yii::t('app','Maximum alternative phone numbers limit ({0}) reached for {1}',[static::MAX_ALTERNATIVE_CONTACTS,$this->phone_number])); |
||
189 | } |
||
190 | if($this->validatePhoneNumber($attribute,$item)){ |
||
191 | $cleanItems[]=$item; |
||
192 | } |
||
193 | |||
194 | } |
||
195 | } |
||
196 | if(!empty($cleanItems)){ |
||
197 | $this->alternative_phone_numbers = yii\helpers\Json::encode($cleanItems); |
||
198 | } else { |
||
199 | $this->alternative_phone_numbers = null; |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * @param string $attribute |
||
208 | * @param string $number |
||
209 | * @return null |
||
210 | */ |
||
211 | private function validateSameAsMainNumber($attribute, $number = null) |
||
212 | { |
||
213 | if (!$number or empty($number)) { |
||
214 | return null; |
||
215 | } |
||
216 | |||
217 | $isSame = ($attribute=='phone_number' ? false : $number == $this->phone_number); |
||
218 | |||
219 | if ($isSame) { |
||
220 | $this->addError($attribute, |
||
221 | Yii::t('app', |
||
222 | 'Invalid phone number "{0}"',[$number] |
||
223 | ).' '.Yii::t('app','Reason: {0}',[Yii::t('app',$attribute. ' duplicates main phone number')]) |
||
224 | ); |
||
225 | } |
||
226 | return null; |
||
227 | } |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @param string $attribute |
||
232 | * @param string $phone_number Phone number to check duplicates for |
||
233 | */ |
||
234 | private function validatePhoneSurveyDuplicate($attribute, $phone_number){ |
||
235 | $query = static::find(); |
||
236 | // check only this survey |
||
237 | $query->andWhere(['survey_id'=>$this->survey_id]); |
||
238 | |||
239 | if($this->respondent_id){ |
||
240 | // not itself |
||
241 | $query->andWhere(['!=','respondent_id',$this->respondent_id]); |
||
242 | } |
||
243 | |||
244 | $condition = ['or', |
||
245 | '`phone_number`=:phone_number', |
||
246 | '`alternative_phone_numbers` LIKE :phone_number2', |
||
247 | ]; |
||
248 | $query->andWhere($condition,[':phone_number'=>$phone_number,':phone_number2'=>'%\"'.$phone_number.'\"%']); |
||
249 | |||
250 | if($query->count() > 0) { |
||
251 | $this->addError($attribute, |
||
252 | Yii::t('app', |
||
253 | 'Invalid phone number "{0}"',[$phone_number] |
||
254 | ).' '.Yii::t('app','Reason: {0}',[Yii::t('app','Duplicate phone number')]) |
||
255 | ); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @param string $email_address Email address to check duplicates for |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function isEmailSurveyDuplicate($attribute, $email_address){ |
||
265 | $query = static::find(); |
||
266 | // check only this survey |
||
267 | $query->andWhere(['survey_id'=>$this->survey_id]); |
||
268 | // not itself |
||
269 | if($this->respondent_id){ |
||
270 | // not itself |
||
271 | $query->andWhere(['!=','respondent_id',$this->respondent_id]); |
||
272 | } |
||
273 | |||
274 | $email_condition = ['or', |
||
275 | '`email_address`=:email_address', |
||
276 | '`alternative_email_addresses` LIKE :email_address2', |
||
277 | ]; |
||
278 | $query->andWhere($email_condition,[':email_address'=>$email_address,':email_address2'=>'%\"'.$email_address.'\"%']); |
||
279 | if($query->count() > 0){ |
||
280 | return true; |
||
281 | } |
||
282 | |||
283 | $this->addError($attribute, |
||
284 | Yii::t('app', |
||
285 | 'Invalid email address "{0}"',[$email_address] |
||
286 | ).' '.Yii::t('app','Reason: {0}',[Yii::t('app','Duplicates some other address')]) |
||
287 | ); |
||
288 | |||
289 | return false; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function attributeLabels() |
||
299 | ]; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $token Respondents token |
||
304 | * @return static |
||
305 | */ |
||
306 | public static function findByToken($token = null){ |
||
307 | if($token){ |
||
308 | $models = self::findByTokens([$token]); |
||
309 | if (!empty($models)){ |
||
310 | return $models[0]; |
||
311 | } |
||
312 | } |
||
313 | return null; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param string[] $tokens Respondents tokens |
||
318 | * @return static[] |
||
319 | */ |
||
320 | public static function findByTokens($tokens){ |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Check whether respondent has rejected this specific survey or has a hard bounce on e_mail address |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function getIsRejected(){ |
||
333 | if(Rejection::rejectedByCode($this->token)){ |
||
334 | return true; |
||
335 | } |
||
336 | if(Rejection::bouncedByEmailAddress($this->email_address)){ |
||
337 | return true; |
||
338 | } |
||
339 | |||
340 | return false; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get the last respondent based on Email-address |
||
345 | * @param string $email_address |
||
346 | * @return static |
||
347 | */ |
||
348 | public static function getLatestByEmail($email_address){ |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getShortToken(){ |
||
367 | } |
||
368 | |||
369 | |||
370 | } |