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