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