| Total Complexity | 48 |
| Total Lines | 405 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WhereQuery 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 WhereQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class WhereQuery extends Query { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array $available_criteria |
||
| 57 | */ |
||
| 58 | protected $available_criteria = [ |
||
| 59 | 'OR', 'AND', |
||
| 60 | 'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
||
| 61 | 'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
||
| 62 | 'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT" |
||
| 67 | * @param string $name |
||
| 68 | * @param array|null $arguments |
||
| 69 | * |
||
| 70 | * @return mixed |
||
| 71 | * @throws InvalidWhereQueryCriteriaException |
||
| 72 | * @throws MethodNotFoundException |
||
| 73 | */ |
||
| 74 | public function __call($name, $arguments) { |
||
| 75 | $that = $this; |
||
| 76 | |||
| 77 | $name = Str::camel($name); |
||
| 78 | |||
| 79 | if(strtolower(substr($name, 0, 3)) === 'not') { |
||
| 80 | $that = $that->whereNot(); |
||
| 81 | $name = substr($name, 3); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (strpos(strtolower($name), "where") === false){ |
||
| 85 | $method = 'where'.ucfirst($name); |
||
| 86 | }else{ |
||
| 87 | $method = lcfirst($name); |
||
| 88 | } |
||
| 89 | |||
| 90 | if(method_exists($this, $method) === true){ |
||
| 91 | return call_user_func_array([$that, $method], $arguments); |
||
|
|
|||
| 92 | } |
||
| 93 | |||
| 94 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Validate a given criteria |
||
| 99 | * @param $criteria |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | * @throws InvalidWhereQueryCriteriaException |
||
| 103 | */ |
||
| 104 | protected function validate_criteria($criteria) { |
||
| 105 | $criteria = strtoupper($criteria); |
||
| 106 | if (substr($criteria, 0, 7) === "CUSTOM ") { |
||
| 107 | return substr($criteria, 7); |
||
| 108 | } |
||
| 109 | if(in_array($criteria, $this->available_criteria) === false) { |
||
| 110 | throw new InvalidWhereQueryCriteriaException(); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $criteria; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param mixed $criteria |
||
| 118 | * @param null $value |
||
| 119 | * |
||
| 120 | * @return $this |
||
| 121 | * @throws InvalidWhereQueryCriteriaException |
||
| 122 | */ |
||
| 123 | public function where($criteria, $value = null) { |
||
| 124 | if(is_array($criteria)){ |
||
| 125 | foreach($criteria as $key => $value){ |
||
| 126 | if(is_numeric($key)){ |
||
| 127 | return $this->where($value); |
||
| 128 | } |
||
| 129 | return $this->where($key, $value); |
||
| 130 | } |
||
| 131 | }else{ |
||
| 132 | $criteria = $this->validate_criteria($criteria); |
||
| 133 | $value = $this->parse_value($value); |
||
| 134 | |||
| 135 | if($value === null || $value === ''){ |
||
| 136 | $this->query->push([$criteria]); |
||
| 137 | }else{ |
||
| 138 | $this->query->push([$criteria, $value]); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param \Closure $closure |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function orWhere(\Closure $closure = null) { |
||
| 151 | $this->query->push(['OR']); |
||
| 152 | if($closure !== null) $closure($this); |
||
| 153 | |||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param \Closure $closure |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function andWhere(\Closure $closure = null) { |
||
| 163 | $this->query->push(['AND']); |
||
| 164 | if($closure !== null) $closure($this); |
||
| 165 | |||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return WhereQuery |
||
| 171 | * @throws InvalidWhereQueryCriteriaException |
||
| 172 | */ |
||
| 173 | public function whereAll() { |
||
| 174 | return $this->where('ALL'); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return WhereQuery |
||
| 179 | * @throws InvalidWhereQueryCriteriaException |
||
| 180 | */ |
||
| 181 | public function whereAnswered() { |
||
| 182 | return $this->where('ANSWERED'); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $value |
||
| 187 | * |
||
| 188 | * @return WhereQuery |
||
| 189 | * @throws InvalidWhereQueryCriteriaException |
||
| 190 | */ |
||
| 191 | public function whereBcc($value) { |
||
| 192 | return $this->where('BCC', $value); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param mixed $value |
||
| 197 | * @return WhereQuery |
||
| 198 | * @throws InvalidWhereQueryCriteriaException |
||
| 199 | * @throws MessageSearchValidationException |
||
| 200 | */ |
||
| 201 | public function whereBefore($value) { |
||
| 202 | $date = $this->parse_date($value); |
||
| 203 | return $this->where('BEFORE', $date); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $value |
||
| 208 | * |
||
| 209 | * @return WhereQuery |
||
| 210 | * @throws InvalidWhereQueryCriteriaException |
||
| 211 | */ |
||
| 212 | public function whereBody($value) { |
||
| 213 | return $this->where('BODY', $value); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $value |
||
| 218 | * |
||
| 219 | * @return WhereQuery |
||
| 220 | * @throws InvalidWhereQueryCriteriaException |
||
| 221 | */ |
||
| 222 | public function whereCc($value) { |
||
| 223 | return $this->where('CC', $value); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return WhereQuery |
||
| 228 | * @throws InvalidWhereQueryCriteriaException |
||
| 229 | */ |
||
| 230 | public function whereDeleted() { |
||
| 231 | return $this->where('DELETED'); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $value |
||
| 236 | * |
||
| 237 | * @return WhereQuery |
||
| 238 | * @throws InvalidWhereQueryCriteriaException |
||
| 239 | */ |
||
| 240 | public function whereFlagged($value) { |
||
| 241 | return $this->where('FLAGGED', $value); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $value |
||
| 246 | * |
||
| 247 | * @return WhereQuery |
||
| 248 | * @throws InvalidWhereQueryCriteriaException |
||
| 249 | */ |
||
| 250 | public function whereFrom($value) { |
||
| 251 | return $this->where('FROM', $value); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $value |
||
| 256 | * |
||
| 257 | * @return WhereQuery |
||
| 258 | * @throws InvalidWhereQueryCriteriaException |
||
| 259 | */ |
||
| 260 | public function whereKeyword($value) { |
||
| 261 | return $this->where('KEYWORD', $value); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return WhereQuery |
||
| 266 | * @throws InvalidWhereQueryCriteriaException |
||
| 267 | */ |
||
| 268 | public function whereNew() { |
||
| 269 | return $this->where('NEW'); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return WhereQuery |
||
| 274 | * @throws InvalidWhereQueryCriteriaException |
||
| 275 | */ |
||
| 276 | public function whereNot() { |
||
| 277 | return $this->where('NOT'); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return WhereQuery |
||
| 282 | * @throws InvalidWhereQueryCriteriaException |
||
| 283 | */ |
||
| 284 | public function whereOld() { |
||
| 285 | return $this->where('OLD'); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param mixed $value |
||
| 290 | * |
||
| 291 | * @return WhereQuery |
||
| 292 | * @throws MessageSearchValidationException |
||
| 293 | * @throws InvalidWhereQueryCriteriaException |
||
| 294 | */ |
||
| 295 | public function whereOn($value) { |
||
| 296 | $date = $this->parse_date($value); |
||
| 297 | return $this->where('ON', $date); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return WhereQuery |
||
| 302 | * @throws InvalidWhereQueryCriteriaException |
||
| 303 | */ |
||
| 304 | public function whereRecent() { |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return WhereQuery |
||
| 310 | * @throws InvalidWhereQueryCriteriaException |
||
| 311 | */ |
||
| 312 | public function whereSeen() { |
||
| 313 | return $this->where('SEEN'); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param mixed $value |
||
| 318 | * |
||
| 319 | * @return WhereQuery |
||
| 320 | * @throws MessageSearchValidationException |
||
| 321 | * @throws InvalidWhereQueryCriteriaException |
||
| 322 | */ |
||
| 323 | public function whereSince($value) { |
||
| 324 | $date = $this->parse_date($value); |
||
| 325 | return $this->where('SINCE', $date); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $value |
||
| 330 | * |
||
| 331 | * @return WhereQuery |
||
| 332 | * @throws InvalidWhereQueryCriteriaException |
||
| 333 | */ |
||
| 334 | public function whereSubject($value) { |
||
| 335 | return $this->where('SUBJECT', $value); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $value |
||
| 340 | * |
||
| 341 | * @return WhereQuery |
||
| 342 | * @throws InvalidWhereQueryCriteriaException |
||
| 343 | */ |
||
| 344 | public function whereText($value) { |
||
| 345 | return $this->where('TEXT', $value); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $value |
||
| 350 | * |
||
| 351 | * @return WhereQuery |
||
| 352 | * @throws InvalidWhereQueryCriteriaException |
||
| 353 | */ |
||
| 354 | public function whereTo($value) { |
||
| 355 | return $this->where('TO', $value); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $value |
||
| 360 | * |
||
| 361 | * @return WhereQuery |
||
| 362 | * @throws InvalidWhereQueryCriteriaException |
||
| 363 | */ |
||
| 364 | public function whereUnkeyword($value) { |
||
| 365 | return $this->where('UNKEYWORD', $value); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return WhereQuery |
||
| 370 | * @throws InvalidWhereQueryCriteriaException |
||
| 371 | */ |
||
| 372 | public function whereUnanswered() { |
||
| 373 | return $this->where('UNANSWERED'); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return WhereQuery |
||
| 378 | * @throws InvalidWhereQueryCriteriaException |
||
| 379 | */ |
||
| 380 | public function whereUndeleted() { |
||
| 381 | return $this->where('UNDELETED'); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return WhereQuery |
||
| 386 | * @throws InvalidWhereQueryCriteriaException |
||
| 387 | */ |
||
| 388 | public function whereUnflagged() { |
||
| 389 | return $this->where('UNFLAGGED'); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return WhereQuery |
||
| 394 | * @throws InvalidWhereQueryCriteriaException |
||
| 395 | */ |
||
| 396 | public function whereUnseen() { |
||
| 397 | return $this->where('UNSEEN'); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return WhereQuery |
||
| 402 | * @throws InvalidWhereQueryCriteriaException |
||
| 403 | */ |
||
| 404 | public function whereNoXSpam(){ |
||
| 405 | return $this->where("CUSTOM X-Spam-Flag NO"); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return WhereQuery |
||
| 410 | * @throws InvalidWhereQueryCriteriaException |
||
| 411 | */ |
||
| 412 | public function whereIsXSpam(){ |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Search for a specific header value |
||
| 418 | * @param $header |
||
| 419 | * @param $value |
||
| 420 | * |
||
| 421 | * @return WhereQuery |
||
| 422 | * @throws InvalidWhereQueryCriteriaException |
||
| 423 | */ |
||
| 424 | public function whereHeader($header, $value){ |
||
| 425 | return $this->where("CUSTOM HEADER $header $value"); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Search for a specific message id |
||
| 430 | * @param $messageId |
||
| 431 | * |
||
| 432 | * @return WhereQuery |
||
| 433 | * @throws InvalidWhereQueryCriteriaException |
||
| 434 | */ |
||
| 435 | public function whereMessageId($messageId){ |
||
| 436 | return $this->whereHeader("Message-ID", $messageId); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Search for a specific message id |
||
| 441 | * @param $messageId |
||
| 442 | * |
||
| 443 | * @return WhereQuery |
||
| 444 | * @throws InvalidWhereQueryCriteriaException |
||
| 445 | */ |
||
| 446 | public function whereInReplyTo($messageId){ |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param $country_code |
||
| 452 | * |
||
| 453 | * @return WhereQuery |
||
| 454 | * @throws InvalidWhereQueryCriteriaException |
||
| 455 | */ |
||
| 456 | public function whereLanguage($country_code){ |
||
| 458 | } |
||
| 459 | } |