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