| Total Complexity | 47 |
| Total Lines | 502 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Mail |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | * |
||
| 19 | * @ORM\Column(name="id", type="integer") |
||
| 20 | * @ORM\Id |
||
| 21 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 22 | */ |
||
| 23 | protected $id; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | * |
||
| 28 | * @ORM\Column(name="title", type="string", length=255) |
||
| 29 | */ |
||
| 30 | protected $title = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | * |
||
| 35 | * @ORM\Column(name="text", type="text") |
||
| 36 | */ |
||
| 37 | protected $text; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Event[] |
||
| 41 | * |
||
| 42 | * @ORM\ManyToMany(targetEntity="Event") |
||
| 43 | * @ORM\JoinColumn(name="event_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 44 | */ |
||
| 45 | protected $events; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var EventAudience[] |
||
| 49 | * |
||
| 50 | * @ORM\ManyToMany(targetEntity="Stfalcon\Bundle\EventBundle\Entity\EventAudience") |
||
| 51 | * @ORM\JoinColumn(name="audience_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 52 | */ |
||
| 53 | protected $audiences; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | * |
||
| 58 | * @ORM\Column(name="start", type="boolean") |
||
| 59 | */ |
||
| 60 | protected $start = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @todo refact. это костыльное и временное решение |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | * |
||
| 67 | * @ORM\Column(name="payment_status", type="string", nullable=true) |
||
| 68 | */ |
||
| 69 | protected $paymentStatus = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool |
||
| 73 | * |
||
| 74 | * @ORM\Column(name="wants_visit_event", type="boolean") |
||
| 75 | */ |
||
| 76 | protected $wantsVisitEvent = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int |
||
| 80 | * |
||
| 81 | * @ORM\Column(name="total_messages", type="integer") |
||
| 82 | */ |
||
| 83 | protected $totalMessages = 0; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int |
||
| 87 | * |
||
| 88 | * @ORM\Column(name="sent_messages", type="integer") |
||
| 89 | */ |
||
| 90 | protected $sentMessages = 0; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | * |
||
| 95 | * @ORM\Column(name="unsubscribe_messages_cnt", type="integer") |
||
| 96 | */ |
||
| 97 | protected $unsubscribeMessagesCount = 0; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var int |
||
| 101 | * |
||
| 102 | * @ORM\Column(name="open_messages_cnt", type="integer") |
||
| 103 | */ |
||
| 104 | protected $openMessagesCount = 0; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | * |
||
| 109 | * @ORM\OneToMany(targetEntity="MailQueue", mappedBy="mail", cascade={"remove", "persist"}, orphanRemoval=true) |
||
| 110 | * @ORM\JoinColumn(name="mail_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 111 | */ |
||
| 112 | protected $mailQueues; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Constructor. |
||
| 116 | */ |
||
| 117 | public function __construct() |
||
| 118 | { |
||
| 119 | $this->events = new ArrayCollection(); |
||
|
|
|||
| 120 | $this->audiences = new ArrayCollection(); |
||
| 121 | $this->mailQueues = new ArrayCollection(); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param int $openMessagesCount |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function setOpenMessagesCount($openMessagesCount) |
||
| 130 | { |
||
| 131 | $this->openMessagesCount = $openMessagesCount; |
||
| 132 | |||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param int $unsubscribeMessagesCount |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function setUnsubscribeMessagesCount($unsubscribeMessagesCount) |
||
| 142 | { |
||
| 143 | $this->unsubscribeMessagesCount = $unsubscribeMessagesCount; |
||
| 144 | |||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getOpenMessagesCount() |
||
| 152 | { |
||
| 153 | return $this->openMessagesCount; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function getUnsubscribeMessagesCount() |
||
| 160 | { |
||
| 161 | return $this->unsubscribeMessagesCount; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add open messages count. |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function addOpenMessagesCount() |
||
| 170 | { |
||
| 171 | ++$this->openMessagesCount; |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Add unsubscribe messages count. |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function addUnsubscribeMessagesCount() |
||
| 182 | { |
||
| 183 | ++$this->unsubscribeMessagesCount; |
||
| 184 | |||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function isWantsVisitEvent() |
||
| 192 | { |
||
| 193 | return $this->wantsVisitEvent; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param bool $wantsVisitEvent |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setWantsVisitEvent($wantsVisitEvent) |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function __toString() |
||
| 212 | { |
||
| 213 | return (string) $this->getTitle() ?: ''; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param int $sentMessages |
||
| 218 | */ |
||
| 219 | public function setSentMessages($sentMessages) |
||
| 220 | { |
||
| 221 | $this->sentMessages = $sentMessages; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | public function getSentMessages() |
||
| 228 | { |
||
| 229 | return $this->sentMessages; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param bool $checkStop |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function incSentMessage($checkStop = true) |
||
| 238 | { |
||
| 239 | ++$this->sentMessages; |
||
| 240 | |||
| 241 | if ($checkStop) { |
||
| 242 | $this->stopIfMailed(); |
||
| 243 | } |
||
| 244 | |||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param bool $checkStop |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function decSentMessage($checkStop = true) |
||
| 254 | { |
||
| 255 | --$this->sentMessages; |
||
| 256 | |||
| 257 | if ($checkStop) { |
||
| 258 | $this->stopIfMailed(); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Stop Mail if all mailed. |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function stopIfMailed() |
||
| 270 | { |
||
| 271 | if ($this->start) { |
||
| 272 | $this->start = $this->totalMessages > $this->sentMessages; |
||
| 273 | } |
||
| 274 | |||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param int $totalMessages |
||
| 280 | */ |
||
| 281 | public function setTotalMessages($totalMessages) |
||
| 282 | { |
||
| 283 | $this->totalMessages = $totalMessages; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | public function getTotalMessages() |
||
| 290 | { |
||
| 291 | return $this->totalMessages; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param bool $checkStop |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function decTotalMessages($checkStop = true) |
||
| 300 | { |
||
| 301 | --$this->totalMessages; |
||
| 302 | |||
| 303 | if ($checkStop) { |
||
| 304 | $this->stopIfMailed(); |
||
| 305 | } |
||
| 306 | |||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param bool $checkStop |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | */ |
||
| 315 | public function incTotalMessages($checkStop = true) |
||
| 316 | { |
||
| 317 | ++$this->totalMessages; |
||
| 318 | |||
| 319 | if ($checkStop) { |
||
| 320 | $this->stopIfMailed(); |
||
| 321 | } |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get id. |
||
| 328 | * |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | public function getId() |
||
| 332 | { |
||
| 333 | return $this->id; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getTitle() |
||
| 340 | { |
||
| 341 | return $this->title; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $title |
||
| 346 | */ |
||
| 347 | public function setTitle($title) |
||
| 348 | { |
||
| 349 | $this->title = $title; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getText() |
||
| 356 | { |
||
| 357 | return $this->text; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $text |
||
| 362 | */ |
||
| 363 | public function setText($text) |
||
| 364 | { |
||
| 365 | $this->text = $text; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return Event[]|ArrayCollection |
||
| 370 | */ |
||
| 371 | public function getEvents() |
||
| 372 | { |
||
| 373 | return $this->events; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param ArrayCollection $events |
||
| 378 | */ |
||
| 379 | public function setEvents($events) |
||
| 380 | { |
||
| 381 | $this->events = $events; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function getStart() |
||
| 388 | { |
||
| 389 | return $this->start; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param bool $start |
||
| 394 | */ |
||
| 395 | public function setStart($start) |
||
| 396 | { |
||
| 397 | $this->start = $start; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param array $data |
||
| 402 | * |
||
| 403 | * @return mixed|string |
||
| 404 | */ |
||
| 405 | public function replace($data) |
||
| 406 | { |
||
| 407 | $text = $this->getText(); |
||
| 408 | foreach ($data as $key => $value) { |
||
| 409 | $text = str_replace($key, $value, $text); |
||
| 410 | } |
||
| 411 | |||
| 412 | return $text; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return null|string |
||
| 417 | */ |
||
| 418 | public function getPaymentStatus() |
||
| 419 | { |
||
| 420 | return $this->paymentStatus; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param string $paymentStatus |
||
| 425 | */ |
||
| 426 | public function setPaymentStatus($paymentStatus) |
||
| 427 | { |
||
| 428 | $this->paymentStatus = $paymentStatus; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function getStatistic() |
||
| 435 | { |
||
| 436 | return |
||
| 437 | $this->totalMessages.'/'.$this->sentMessages.'/'.$this->openMessagesCount.'/'.$this->unsubscribeMessagesCount.(($this->sentMessages === $this->totalMessages) ? ' - complete' : ''); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Add event. |
||
| 442 | * |
||
| 443 | * @param Event $event |
||
| 444 | * |
||
| 445 | * @return Mail |
||
| 446 | */ |
||
| 447 | public function addEvent(Event $event) |
||
| 448 | { |
||
| 449 | $this->events->add($event); |
||
| 450 | |||
| 451 | return $this; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Remove events. |
||
| 456 | * |
||
| 457 | * @param Event $event |
||
| 458 | */ |
||
| 459 | public function removeEvent(Event $event) |
||
| 460 | { |
||
| 461 | $this->events->removeElement($event); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Add mailQueue. |
||
| 466 | * |
||
| 467 | * @param \Stfalcon\Bundle\EventBundle\Entity\MailQueue $mailQueue |
||
| 468 | * |
||
| 469 | * @return Mail |
||
| 470 | */ |
||
| 471 | public function addMailQueue(MailQueue $mailQueue) |
||
| 472 | { |
||
| 473 | $this->mailQueues->add($mailQueue); |
||
| 474 | |||
| 475 | return $this; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Remove mailQueue. |
||
| 480 | * |
||
| 481 | * @param \Stfalcon\Bundle\EventBundle\Entity\MailQueue $mailQueue |
||
| 482 | */ |
||
| 483 | public function removeMailQueue(MailQueue $mailQueue) |
||
| 484 | { |
||
| 485 | $this->mailQueues->removeElement($mailQueue); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get mailQueues. |
||
| 490 | * |
||
| 491 | * @return \Doctrine\Common\Collections\Collection |
||
| 492 | */ |
||
| 493 | public function getMailQueues() |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return EventAudience[]|ArrayCollection |
||
| 500 | */ |
||
| 501 | public function getAudiences() |
||
| 502 | { |
||
| 503 | return $this->audiences; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param EventAudience[]|ArrayCollection $audiences |
||
| 508 | * |
||
| 509 | * @return $this |
||
| 510 | */ |
||
| 511 | public function setAudiences($audiences) |
||
| 516 | } |
||
| 517 | } |
||
| 518 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..