| Total Complexity | 52 | 
| Total Lines | 388 | 
| Duplicated Lines | 0 % | 
| Changes | 10 | ||
| Bugs | 2 | Features | 1 | 
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  | 
            ||
| 29 | class Respondent extends MyActiveRecord  | 
            ||
| 30 | { | 
            ||
| 31 | const MAX_ALTERNATIVE_CONTACTS = 20;  | 
            ||
| 32 | |||
| 33 | /** @var bool $checkDSNForEmails whether email validation also used DSN records to check if domain exists */  | 
            ||
| 34 | public static $checkDSNForEmails = false;  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @var array $surveyIdentifyingColumns names of the columns that identify a respondent as unique inside a survey  | 
            ||
| 38 | */  | 
            ||
| 39 | public static $surveyIdentifyingColumns = ['phone_number', 'email_address'];  | 
            ||
| 40 | |||
| 41 | public function init()  | 
            ||
| 42 |     { | 
            ||
| 43 | parent::init();  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | |||
| 47 | /**  | 
            ||
| 48 |      * {@inheritdoc} | 
            ||
| 49 | */  | 
            ||
| 50 | public function rules()  | 
            ||
| 51 |     { | 
            ||
| 52 | return array_merge([  | 
            ||
| 53 | [['survey_id', 'token'], 'required'],  | 
            ||
| 54 | [['survey_id'], 'integer'],  | 
            ||
| 55 | [['email_address'], 'validateEmail'],  | 
            ||
| 56 | [['email_address', 'phone_number'], 'filter', 'filter' => 'trim'],  | 
            ||
| 57 | [['email_address', 'phone_number'], 'filter', 'filter' => 'strtolower'],  | 
            ||
| 58 | [['alternative_email_addresses'], 'string'],  | 
            ||
| 59 | [['alternative_email_addresses'], 'validateMultipleEmails'],  | 
            ||
| 60 | [['phone_number'], 'string', 'length' => [4, 32]],  | 
            ||
| 61 | [['phone_number'], 'validatePhoneNumber'],  | 
            ||
| 62 | [['time_collector_registered'], 'string', 'max' => 32],  | 
            ||
| 63 | [['alternative_phone_numbers'], 'string'],  | 
            ||
| 64 | [['alternative_phone_numbers'], 'validateMultiplePhoneNumbers'],  | 
            ||
| 65 | [['token', 'key'], 'unique'],  | 
            ||
| 66 | [['key'], 'string', 'max' => 32],  | 
            ||
| 67 | ], parent::rules());  | 
            ||
| 68 | }  | 
            ||
| 69 | |||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * @param string $attribute  | 
            ||
| 73 | * @param string $address  | 
            ||
| 74 | * @return bool  | 
            ||
| 75 | */  | 
            ||
| 76 | public function validateEmail($attribute, $address)  | 
            ||
| 77 |     { | 
            ||
| 78 |         if (empty($address)) { | 
            ||
| 79 | $address = $this->email_address;  | 
            ||
| 80 | }  | 
            ||
| 81 |         if (!empty($address)) { | 
            ||
| 82 | if ($this->validateEmailFormat($attribute, $address)  | 
            ||
| 83 | && !$this->isSameAsMainAddress($attribute, $address)  | 
            ||
| 84 |                 && !$this->isEmailSurveyDuplicate($attribute, $address)) { | 
            ||
| 85 | return true;  | 
            ||
| 86 | }  | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 | return false;  | 
            ||
| 90 | }  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * @param string $attribute  | 
            ||
| 94 | * @param string $address  | 
            ||
| 95 | * @return bool  | 
            ||
| 96 | */  | 
            ||
| 97 | private function validateEmailFormat($attribute, $address)  | 
            ||
| 98 |     { | 
            ||
| 99 | $validator = new yii\validators\EmailValidator();  | 
            ||
| 100 | $validator->checkDNS = static::$checkDSNForEmails;  | 
            ||
| 101 |         if (!$validator->validate($address)) { | 
            ||
| 102 | $this->addError($attribute,  | 
            ||
| 103 |                 Yii::t('app', | 
            ||
| 104 |                     'Invalid email address "{0}"', [$address] | 
            ||
| 105 |                 ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Invalid email format')]) | 
            ||
| 106 | );  | 
            ||
| 107 | return false;  | 
            ||
| 108 | }  | 
            ||
| 109 | return true;  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * @param string $address  | 
            ||
| 114 | * @return bool  | 
            ||
| 115 | */  | 
            ||
| 116 | private function isSameAsMainAddress($attribute, $address)  | 
            ||
| 117 |     { | 
            ||
| 118 |         if (empty($address) | $attribute === "email_address") { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 119 | return false;  | 
            ||
| 120 | }  | 
            ||
| 121 |         if ($address === $this->email_address) { | 
            ||
| 122 | $this->addError($attribute,  | 
            ||
| 123 |                 Yii::t('app', | 
            ||
| 124 |                     'Email address same as main address "{0}"', [$address] | 
            ||
| 125 |                 ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute . ' duplicates main address')]) | 
            ||
| 126 | );  | 
            ||
| 127 | return true;  | 
            ||
| 128 | }  | 
            ||
| 129 | return false;  | 
            ||
| 130 | }  | 
            ||
| 131 | |||
| 132 | |||
| 133 | public function validateMultipleEmails($attribute)  | 
            ||
| 134 |     { | 
            ||
| 135 | $cleanAddresses = [];  | 
            ||
| 136 | $addresses = yii\helpers\Json::decode($this->alternative_email_addresses);  | 
            ||
| 137 |         if (!empty($addresses)) { | 
            ||
| 138 | $i = 0;  | 
            ||
| 139 |             foreach ($addresses as $key => $address) { | 
            ||
| 140 |                 if (!empty($address)) { | 
            ||
| 141 | $i++;  | 
            ||
| 142 | // check the alternative numbers of that model for duplicates  | 
            ||
| 143 | $checkItems = $addresses;  | 
            ||
| 144 | unset($checkItems[$key]);  | 
            ||
| 145 |                     if (in_array($address, $checkItems)) { | 
            ||
| 146 |                         $this->addError($attribute, Yii::t('app', 'Duplicate email in alternative email addresses')); | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 |                     if ($i > static::MAX_ALTERNATIVE_CONTACTS) { | 
            ||
| 150 |                         $this->addError($attribute, Yii::t('app', 'Maximum alternative addresses limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->email_address])); | 
            ||
| 151 | }  | 
            ||
| 152 | $address = strtolower(trim($address));  | 
            ||
| 153 |                     if ($this->validateEmail($attribute, $address)) { | 
            ||
| 154 | $cleanAddresses[] = $address;  | 
            ||
| 155 | }  | 
            ||
| 156 | }  | 
            ||
| 157 | }  | 
            ||
| 158 | }  | 
            ||
| 159 | $this->alternative_email_addresses = yii\helpers\Json::encode($cleanAddresses);  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | |||
| 163 | public function validatePhoneNumber($attribute, $phone_number)  | 
            ||
| 164 |     { | 
            ||
| 165 |         if (empty($phone_number)) { | 
            ||
| 166 | $phone_number = $this->phone_number;  | 
            ||
| 167 | }  | 
            ||
| 168 |         if (!empty($phone_number)) { | 
            ||
| 169 | $this->validateSameAsMainNumber($attribute, $phone_number);  | 
            ||
| 170 | // FIXME  | 
            ||
| 171 | $isValidFormat = true;  | 
            ||
| 172 | $this->validatePhoneSurveyDuplicate($attribute, $phone_number);  | 
            ||
| 173 | |||
| 174 | }  | 
            ||
| 175 | |||
| 176 | }  | 
            ||
| 177 | |||
| 178 | |||
| 179 | public function validateMultiplePhoneNumbers($attribute)  | 
            ||
| 180 |     { | 
            ||
| 181 | $cleanItems = [];  | 
            ||
| 182 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers);  | 
            ||
| 183 |         if (!empty($items)) { | 
            ||
| 184 | $i = 0;  | 
            ||
| 185 |             foreach ($items as $key => $item) { | 
            ||
| 186 | $i++;  | 
            ||
| 187 | $item = strtolower(trim($item));  | 
            ||
| 188 | $this->validateAlternativePhoneNumberInternalDuplicates($attribute, $item, $key);  | 
            ||
| 189 | |||
| 190 |                 if ($i > static::MAX_ALTERNATIVE_CONTACTS) { | 
            ||
| 191 |                     $this->addError($attribute, Yii::t('app', 'Maximum alternative phone numbers limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->phone_number])); | 
            ||
| 192 | }  | 
            ||
| 193 | $this->validatePhoneNumber($attribute, $item);  | 
            ||
| 194 | }  | 
            ||
| 195 | }  | 
            ||
| 196 | $this->alternative_phone_numbers = yii\helpers\Json::encode($cleanItems);  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | private function validateAlternativePhoneNumberInternalDuplicates($attribute, $number, $key)  | 
            ||
| 200 |     { | 
            ||
| 201 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers);  | 
            ||
| 202 | $checkItems = $items;  | 
            ||
| 203 | unset($checkItems[$key]);  | 
            ||
| 204 |         if (in_array($number, $checkItems)) { | 
            ||
| 205 |             $this->addError($attribute, Yii::t('app', 'Duplicate number in alternative phone numbers')); | 
            ||
| 206 | }  | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * @param string $attribute  | 
            ||
| 212 | * @param string $number  | 
            ||
| 213 | * @return null  | 
            ||
| 214 | */  | 
            ||
| 215 | private function validateSameAsMainNumber($attribute, $number)  | 
            ||
| 216 |     { | 
            ||
| 217 |         if (empty($number) | $attribute === 'phone_number') { | 
            ||
| 218 | return null;  | 
            ||
| 219 | }  | 
            ||
| 220 | |||
| 221 |         if ($number === $this->phone_number) { | 
            ||
| 222 | $this->addError($attribute,  | 
            ||
| 223 |                 Yii::t('app', | 
            ||
| 224 |                     'Invalid phone number "{0}"', [$number] | 
            ||
| 225 |                 ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute . ' duplicates main phone number')]) | 
            ||
| 226 | );  | 
            ||
| 227 | }  | 
            ||
| 228 | return null;  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * @param string $attribute  | 
            ||
| 234 | * @param string $phone_number Phone number to check duplicates for  | 
            ||
| 235 | */  | 
            ||
| 236 | protected function validatePhoneSurveyDuplicate($attribute, $phone_number)  | 
            ||
| 237 |     { | 
            ||
| 238 | |||
| 239 | $query = static::find();  | 
            ||
| 240 | // check only this survey  | 
            ||
| 241 | $query->andWhere(['survey_id' => $this->survey_id]);  | 
            ||
| 242 | |||
| 243 |         if ($this->respondent_id) { | 
            ||
| 244 | // not itself  | 
            ||
| 245 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]);  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | $condition = ['or',  | 
            ||
| 249 | '`phone_number`=:phone_number',  | 
            ||
| 250 | '`alternative_phone_numbers` LIKE :phone_number2',  | 
            ||
| 251 | ];  | 
            ||
| 252 | |||
| 253 | $query->andWhere($condition, [':phone_number' => $phone_number, ':phone_number2' => '%\"' . $phone_number . '\"%']);  | 
            ||
| 254 | |||
| 255 |         if ($query->count() > 0) { | 
            ||
| 256 | $this->addError($attribute,  | 
            ||
| 257 |                 Yii::t('app', | 
            ||
| 258 |                     'Invalid phone number "{0}"', [$phone_number] | 
            ||
| 259 |                 ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicate phone number')]) | 
            ||
| 260 | );  | 
            ||
| 261 | }  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * @param string $email_address Email address to check duplicates for  | 
            ||
| 267 | * @return bool  | 
            ||
| 268 | */  | 
            ||
| 269 | public function isEmailSurveyDuplicate($attribute, $email_address)  | 
            ||
| 270 |     { | 
            ||
| 271 | $query = static::find();  | 
            ||
| 272 | // check only this survey  | 
            ||
| 273 | $query->andWhere(['survey_id' => $this->survey_id]);  | 
            ||
| 274 | |||
| 275 | // not itself  | 
            ||
| 276 |         if (!empty($this->respondent_id)) { | 
            ||
| 277 | // not itself  | 
            ||
| 278 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]);  | 
            ||
| 279 | }  | 
            ||
| 280 | |||
| 281 | $email_condition = ['or',  | 
            ||
| 282 | '`email_address`=:email_address',  | 
            ||
| 283 | '`alternative_email_addresses` LIKE :email_address2',  | 
            ||
| 284 | ];  | 
            ||
| 285 | |||
| 286 | $query->andWhere($email_condition, [':email_address' => $email_address, ':email_address2' => '%\"' . $email_address . '\"%']);  | 
            ||
| 287 | |||
| 288 |         if ($query->count() > 0) { | 
            ||
| 289 | $this->addError($attribute,  | 
            ||
| 290 |                 Yii::t('app', | 
            ||
| 291 |                     'Invalid email address "{0}"', [$email_address] | 
            ||
| 292 |                 ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicates some other address')]) | 
            ||
| 293 | );  | 
            ||
| 294 | return true;  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | |||
| 298 | return false;  | 
            ||
| 299 | }  | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 |      * {@inheritdoc} | 
            ||
| 303 | */  | 
            ||
| 304 | public function attributeLabels()  | 
            ||
| 308 | ];  | 
            ||
| 309 | }  | 
            ||
| 310 | |||
| 311 | /**  | 
            ||
| 312 | * @param string $token Respondents token  | 
            ||
| 313 | * @return static  | 
            ||
| 314 | */  | 
            ||
| 315 | public static function findByToken($token = null)  | 
            ||
| 316 |     { | 
            ||
| 317 |         if ($token) { | 
            ||
| 318 | $models = self::findByTokens([$token]);  | 
            ||
| 319 |             if (!empty($models)) { | 
            ||
| 320 | return $models[0];  | 
            ||
| 321 | }  | 
            ||
| 322 | }  | 
            ||
| 323 | return null;  | 
            ||
| 324 | }  | 
            ||
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * @param string[] $tokens Respondents tokens  | 
            ||
| 328 | * @return static[]  | 
            ||
| 329 | */  | 
            ||
| 330 | public static function findByTokens($tokens)  | 
            ||
| 337 | }  | 
            ||
| 338 | |||
| 339 | /**  | 
            ||
| 340 | * Check whether respondent has rejected this specific survey or has a hard bounce on e-mail address  | 
            ||
| 341 | * @return bool  | 
            ||
| 342 | */  | 
            ||
| 343 | public function getIsRejected()  | 
            ||
| 344 |     { | 
            ||
| 345 |         if ($this->isRejectedByClick) { | 
            ||
| 346 | return true;  | 
            ||
| 347 | }  | 
            ||
| 348 | |||
| 349 |         if (Rejection::bouncedByEmailAddress($this->email_address)) { | 
            ||
| 350 | return true;  | 
            ||
| 351 | }  | 
            ||
| 352 | |||
| 353 | return false;  | 
            ||
| 354 | }  | 
            ||
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * Check whether there are rejections for this respondent that have clicked on reject link. This check does not check bounced e-mails  | 
            ||
| 358 | * @return bool  | 
            ||
| 359 | */  | 
            ||
| 360 | public function getIsRejectedByClick()  | 
            ||
| 367 | }  | 
            ||
| 368 | |||
| 369 | /**  | 
            ||
| 370 | * Get the last respondent based on Email-address  | 
            ||
| 371 | * @param string $email_address  | 
            ||
| 372 | * @return static  | 
            ||
| 373 | */  | 
            ||
| 374 | public static function getLatestByEmail($email_address)  | 
            ||
| 375 |     { | 
            ||
| 376 | /** @var static $model */  | 
            ||
| 377 | $model = static::find()  | 
            ||
| 378 | ->andWhere(['email_address' => $email_address])  | 
            ||
| 379 | ->orderBy([(new static)->primaryKeySingle() => SORT_DESC])  | 
            ||
| 380 | ->one();  | 
            ||
| 381 | return $model;  | 
            ||
| 382 | }  | 
            ||
| 383 | |||
| 384 | /**  | 
            ||
| 385 | * @return string  | 
            ||
| 386 | */  | 
            ||
| 387 | public function getShortToken()  | 
            ||
| 388 |     { | 
            ||
| 389 |         if (Uuid::isValid($this->token)) { | 
            ||
| 390 | $uuid = Uuid::fromString($this->token);  | 
            ||
| 391 | $shotUuid = new ShortUuid();  | 
            ||
| 392 | return $shotUuid->encode($uuid);  | 
            ||
| 393 | }  | 
            ||
| 394 | return $this->token;  | 
            ||
| 395 | }  | 
            ||
| 396 | |||
| 397 | /**  | 
            ||
| 398 | * TODO move to some factory  | 
            ||
| 399 | * @return integer  | 
            ||
| 400 | */  | 
            ||
| 401 | public function setBulkRegistered($tokens, $field = 'time_collector_registered')  | 
            ||
| 410 | }  | 
            ||
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * @return array  | 
            ||
| 414 | */  | 
            ||
| 415 |     public function getParticipantData(){ | 
            ||
| 416 | return [];  | 
            ||
| 417 | }  | 
            ||
| 418 | |||
| 419 | |||
| 420 | }  |