| Total Complexity | 77 |
| Total Lines | 543 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Notification 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 Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Notification implements INotification { |
||
| 35 | |||
| 36 | /** @var IValidator */ |
||
| 37 | protected $richValidator; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $app; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | protected $user; |
||
| 44 | |||
| 45 | /** @var \DateTime */ |
||
| 46 | protected $dateTime; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | protected $objectType; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | protected $objectId; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | protected $subject; |
||
| 56 | |||
| 57 | /** @var array */ |
||
| 58 | protected $subjectParameters; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | protected $subjectParsed; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | protected $subjectRich; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | protected $subjectRichParameters; |
||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | protected $message; |
||
| 71 | |||
| 72 | /** @var array */ |
||
| 73 | protected $messageParameters; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | protected $messageParsed; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | protected $messageRich; |
||
| 80 | |||
| 81 | /** @var array */ |
||
| 82 | protected $messageRichParameters; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | protected $link; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | protected $icon; |
||
| 89 | |||
| 90 | /** @var array */ |
||
| 91 | protected $actions; |
||
| 92 | |||
| 93 | /** @var array */ |
||
| 94 | protected $actionsParsed; |
||
| 95 | |||
| 96 | /** @var bool */ |
||
| 97 | protected $hasPrimaryAction; |
||
| 98 | |||
| 99 | /** @var bool */ |
||
| 100 | protected $hasPrimaryParsedAction; |
||
| 101 | |||
| 102 | public function __construct(IValidator $richValidator) { |
||
| 103 | $this->richValidator = $richValidator; |
||
| 104 | $this->app = ''; |
||
| 105 | $this->user = ''; |
||
| 106 | $this->dateTime = new \DateTime(); |
||
| 107 | $this->dateTime->setTimestamp(0); |
||
| 108 | $this->objectType = ''; |
||
| 109 | $this->objectId = ''; |
||
| 110 | $this->subject = ''; |
||
| 111 | $this->subjectParameters = []; |
||
| 112 | $this->subjectParsed = ''; |
||
| 113 | $this->subjectRich = ''; |
||
| 114 | $this->subjectRichParameters = []; |
||
| 115 | $this->message = ''; |
||
| 116 | $this->messageParameters = []; |
||
| 117 | $this->messageParsed = ''; |
||
| 118 | $this->messageRich = ''; |
||
| 119 | $this->messageRichParameters = []; |
||
| 120 | $this->link = ''; |
||
| 121 | $this->icon = ''; |
||
| 122 | $this->actions = []; |
||
| 123 | $this->actionsParsed = []; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $app |
||
| 128 | * @return $this |
||
| 129 | * @throws \InvalidArgumentException if the app id is invalid |
||
| 130 | * @since 8.2.0 |
||
| 131 | */ |
||
| 132 | public function setApp(string $app): INotification { |
||
| 133 | if ($app === '' || isset($app[32])) { |
||
| 134 | throw new \InvalidArgumentException('The given app name is invalid'); |
||
| 135 | } |
||
| 136 | $this->app = $app; |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | * @since 8.2.0 |
||
| 143 | */ |
||
| 144 | public function getApp(): string { |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $user |
||
| 150 | * @return $this |
||
| 151 | * @throws \InvalidArgumentException if the user id is invalid |
||
| 152 | * @since 8.2.0 |
||
| 153 | */ |
||
| 154 | public function setUser(string $user): INotification { |
||
| 155 | if ($user === '' || isset($user[64])) { |
||
| 156 | throw new \InvalidArgumentException('The given user id is invalid'); |
||
| 157 | } |
||
| 158 | $this->user = $user; |
||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | * @since 8.2.0 |
||
| 165 | */ |
||
| 166 | public function getUser(): string { |
||
| 167 | return $this->user; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param \DateTime $dateTime |
||
| 172 | * @return $this |
||
| 173 | * @throws \InvalidArgumentException if the $dateTime is invalid |
||
| 174 | * @since 9.0.0 |
||
| 175 | */ |
||
| 176 | public function setDateTime(\DateTime $dateTime): INotification { |
||
| 177 | if ($dateTime->getTimestamp() === 0) { |
||
| 178 | throw new \InvalidArgumentException('The given date time is invalid'); |
||
| 179 | } |
||
| 180 | $this->dateTime = $dateTime; |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return \DateTime |
||
| 186 | * @since 9.0.0 |
||
| 187 | */ |
||
| 188 | public function getDateTime(): \DateTime { |
||
| 189 | return $this->dateTime; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $type |
||
| 194 | * @param string $id |
||
| 195 | * @return $this |
||
| 196 | * @throws \InvalidArgumentException if the object type or id is invalid |
||
| 197 | * @since 8.2.0 - 9.0.0: Type of $id changed to string |
||
| 198 | */ |
||
| 199 | public function setObject(string $type, string $id): INotification { |
||
| 200 | if ($type === '' || isset($type[64])) { |
||
| 201 | throw new \InvalidArgumentException('The given object type is invalid'); |
||
| 202 | } |
||
| 203 | $this->objectType = $type; |
||
| 204 | |||
| 205 | if ($id === '' || isset($id[64])) { |
||
| 206 | throw new \InvalidArgumentException('The given object id is invalid'); |
||
| 207 | } |
||
| 208 | $this->objectId = (string) $id; |
||
| 209 | return $this; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | * @since 8.2.0 |
||
| 215 | */ |
||
| 216 | public function getObjectType(): string { |
||
| 217 | return $this->objectType; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | * @since 8.2.0 - 9.0.0: Return type changed to string |
||
| 223 | */ |
||
| 224 | public function getObjectId(): string { |
||
| 225 | return $this->objectId; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $subject |
||
| 230 | * @param array $parameters |
||
| 231 | * @return $this |
||
| 232 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 233 | * @since 8.2.0 |
||
| 234 | */ |
||
| 235 | public function setSubject(string $subject, array $parameters = []): INotification { |
||
| 236 | if ($subject === '' || isset($subject[64])) { |
||
| 237 | throw new \InvalidArgumentException('The given subject is invalid'); |
||
| 238 | } |
||
| 239 | |||
| 240 | $this->subject = $subject; |
||
| 241 | $this->subjectParameters = $parameters; |
||
| 242 | |||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | * @since 8.2.0 |
||
| 249 | */ |
||
| 250 | public function getSubject(): string { |
||
| 251 | return $this->subject; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return array |
||
| 256 | * @since 8.2.0 |
||
| 257 | */ |
||
| 258 | public function getSubjectParameters(): array { |
||
| 259 | return $this->subjectParameters; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $subject |
||
| 264 | * @return $this |
||
| 265 | * @throws \InvalidArgumentException if the subject is invalid |
||
| 266 | * @since 8.2.0 |
||
| 267 | */ |
||
| 268 | public function setParsedSubject(string $subject): INotification { |
||
| 269 | if ($subject === '') { |
||
| 270 | throw new \InvalidArgumentException('The given parsed subject is invalid'); |
||
| 271 | } |
||
| 272 | $this->subjectParsed = $subject; |
||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | * @since 8.2.0 |
||
| 279 | */ |
||
| 280 | public function getParsedSubject(): string { |
||
| 281 | return $this->subjectParsed; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $subject |
||
| 286 | * @param array $parameters |
||
| 287 | * @return $this |
||
| 288 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 289 | * @since 11.0.0 |
||
| 290 | */ |
||
| 291 | public function setRichSubject(string $subject, array $parameters = []): INotification { |
||
| 292 | if ($subject === '') { |
||
| 293 | throw new \InvalidArgumentException('The given parsed subject is invalid'); |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->subjectRich = $subject; |
||
| 297 | $this->subjectRichParameters = $parameters; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return string |
||
| 304 | * @since 11.0.0 |
||
| 305 | */ |
||
| 306 | public function getRichSubject(): string { |
||
| 307 | return $this->subjectRich; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return array[] |
||
| 312 | * @since 11.0.0 |
||
| 313 | */ |
||
| 314 | public function getRichSubjectParameters(): array { |
||
| 315 | return $this->subjectRichParameters; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $message |
||
| 320 | * @param array $parameters |
||
| 321 | * @return $this |
||
| 322 | * @throws \InvalidArgumentException if the message or parameters are invalid |
||
| 323 | * @since 8.2.0 |
||
| 324 | */ |
||
| 325 | public function setMessage(string $message, array $parameters = []): INotification { |
||
| 326 | if ($message === '' || isset($message[64])) { |
||
| 327 | throw new \InvalidArgumentException('The given message is invalid'); |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->message = $message; |
||
| 331 | $this->messageParameters = $parameters; |
||
| 332 | |||
| 333 | return $this; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return string |
||
| 338 | * @since 8.2.0 |
||
| 339 | */ |
||
| 340 | public function getMessage(): string { |
||
| 341 | return $this->message; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return array |
||
| 346 | * @since 8.2.0 |
||
| 347 | */ |
||
| 348 | public function getMessageParameters(): array { |
||
| 349 | return $this->messageParameters; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $message |
||
| 354 | * @return $this |
||
| 355 | * @throws \InvalidArgumentException if the message is invalid |
||
| 356 | * @since 8.2.0 |
||
| 357 | */ |
||
| 358 | public function setParsedMessage(string $message): INotification { |
||
| 359 | if ($message === '') { |
||
| 360 | throw new \InvalidArgumentException('The given parsed message is invalid'); |
||
| 361 | } |
||
| 362 | $this->messageParsed = $message; |
||
| 363 | return $this; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return string |
||
| 368 | * @since 8.2.0 |
||
| 369 | */ |
||
| 370 | public function getParsedMessage(): string { |
||
| 371 | return $this->messageParsed; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $message |
||
| 376 | * @param array $parameters |
||
| 377 | * @return $this |
||
| 378 | * @throws \InvalidArgumentException if the message or parameters are invalid |
||
| 379 | * @since 11.0.0 |
||
| 380 | */ |
||
| 381 | public function setRichMessage(string $message, array $parameters = []): INotification { |
||
| 382 | if ($message === '') { |
||
| 383 | throw new \InvalidArgumentException('The given parsed message is invalid'); |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->messageRich = $message; |
||
| 387 | $this->messageRichParameters = $parameters; |
||
| 388 | |||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return string |
||
| 394 | * @since 11.0.0 |
||
| 395 | */ |
||
| 396 | public function getRichMessage(): string { |
||
| 397 | return $this->messageRich; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array[] |
||
| 402 | * @since 11.0.0 |
||
| 403 | */ |
||
| 404 | public function getRichMessageParameters(): array { |
||
| 405 | return $this->messageRichParameters; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $link |
||
| 410 | * @return $this |
||
| 411 | * @throws \InvalidArgumentException if the link is invalid |
||
| 412 | * @since 8.2.0 |
||
| 413 | */ |
||
| 414 | public function setLink(string $link): INotification { |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return string |
||
| 424 | * @since 8.2.0 |
||
| 425 | */ |
||
| 426 | public function getLink(): string { |
||
| 427 | return $this->link; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $icon |
||
| 432 | * @return $this |
||
| 433 | * @throws \InvalidArgumentException if the icon is invalid |
||
| 434 | * @since 11.0.0 |
||
| 435 | */ |
||
| 436 | public function setIcon(string $icon): INotification { |
||
| 437 | if ($icon === '' || isset($icon[4000])) { |
||
| 438 | throw new \InvalidArgumentException('The given icon is invalid'); |
||
| 439 | } |
||
| 440 | $this->icon = $icon; |
||
| 441 | return $this; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | * @since 11.0.0 |
||
| 447 | */ |
||
| 448 | public function getIcon(): string { |
||
| 449 | return $this->icon; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return IAction |
||
| 454 | * @since 8.2.0 |
||
| 455 | */ |
||
| 456 | public function createAction(): IAction { |
||
| 457 | return new Action(); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param IAction $action |
||
| 462 | * @return $this |
||
| 463 | * @throws \InvalidArgumentException if the action is invalid |
||
| 464 | * @since 8.2.0 |
||
| 465 | */ |
||
| 466 | public function addAction(IAction $action): INotification { |
||
| 467 | if (!$action->isValid()) { |
||
| 468 | throw new \InvalidArgumentException('The given action is invalid'); |
||
| 469 | } |
||
| 470 | |||
| 471 | if ($action->isPrimary()) { |
||
| 472 | if ($this->hasPrimaryAction) { |
||
| 473 | throw new \InvalidArgumentException('The notification already has a primary action'); |
||
| 474 | } |
||
| 475 | |||
| 476 | $this->hasPrimaryAction = true; |
||
| 477 | } |
||
| 478 | |||
| 479 | $this->actions[] = $action; |
||
| 480 | return $this; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return IAction[] |
||
| 485 | * @since 8.2.0 |
||
| 486 | */ |
||
| 487 | public function getActions(): array { |
||
| 488 | return $this->actions; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param IAction $action |
||
| 493 | * @return $this |
||
| 494 | * @throws \InvalidArgumentException if the action is invalid |
||
| 495 | * @since 8.2.0 |
||
| 496 | */ |
||
| 497 | public function addParsedAction(IAction $action): INotification { |
||
| 498 | if (!$action->isValidParsed()) { |
||
| 499 | throw new \InvalidArgumentException('The given parsed action is invalid'); |
||
| 500 | } |
||
| 501 | |||
| 502 | if ($action->isPrimary()) { |
||
| 503 | if ($this->hasPrimaryParsedAction) { |
||
| 504 | throw new \InvalidArgumentException('The notification already has a primary action'); |
||
| 505 | } |
||
| 506 | |||
| 507 | $this->hasPrimaryParsedAction = true; |
||
| 508 | |||
| 509 | // Make sure the primary action is always the first one |
||
| 510 | array_unshift($this->actionsParsed, $action); |
||
| 511 | } else { |
||
| 512 | $this->actionsParsed[] = $action; |
||
| 513 | } |
||
| 514 | |||
| 515 | return $this; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @return IAction[] |
||
| 520 | * @since 8.2.0 |
||
| 521 | */ |
||
| 522 | public function getParsedActions(): array { |
||
| 523 | return $this->actionsParsed; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return bool |
||
| 528 | * @since 8.2.0 |
||
| 529 | */ |
||
| 530 | public function isValid(): bool { |
||
| 531 | return |
||
| 532 | $this->isValidCommon() |
||
| 533 | && |
||
| 534 | $this->getSubject() !== '' |
||
| 535 | ; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return bool |
||
| 540 | * @since 8.2.0 |
||
| 541 | */ |
||
| 542 | public function isValidParsed(): bool { |
||
| 563 | ; |
||
| 564 | } |
||
| 565 | |||
| 566 | protected function isValidCommon(): bool { |
||
| 567 | return |
||
| 568 | $this->getApp() !== '' |
||
| 569 | && |
||
| 570 | $this->getUser() !== '' |
||
| 571 | && |
||
| 572 | $this->getDateTime()->getTimestamp() !== 0 |
||
| 573 | && |
||
| 574 | $this->getObjectType() !== '' |
||
| 575 | && |
||
| 576 | $this->getObjectId() !== '' |
||
| 577 | ; |
||
| 578 | } |
||
| 579 | } |
||
| 580 |