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