| Total Complexity | 44 |
| Total Lines | 442 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | #[ORM\Table(name: 'message')] |
||
| 34 | #[ORM\Index(columns: ['user_sender_id'], name: 'idx_message_user_sender')] |
||
| 35 | #[ORM\Index(columns: ['group_id'], name: 'idx_message_group')] |
||
| 36 | #[ORM\Index(columns: ['msg_type'], name: 'idx_message_type')] |
||
| 37 | #[ORM\Entity(repositoryClass: MessageRepository::class)] |
||
| 38 | #[ORM\EntityListeners([MessageListener::class])] |
||
| 39 | #[ApiResource( |
||
| 40 | operations: [ |
||
| 41 | new Get(security: "is_granted('VIEW', object)"), |
||
| 42 | new Put(security: "is_granted('EDIT', object)"), |
||
| 43 | new Delete(security: "is_granted('DELETE', object)"), |
||
| 44 | new GetCollection( |
||
| 45 | uriTemplate: '/messages', |
||
| 46 | security: "is_granted('ROLE_USER')", |
||
| 47 | name: 'get_all_messages' |
||
| 48 | ), |
||
| 49 | new GetCollection( |
||
| 50 | uriTemplate: '/messages/by-group/list', |
||
| 51 | security: "is_granted('ROLE_USER')", |
||
| 52 | name: 'get_messages_by_social_group', |
||
| 53 | provider: MessageByGroupStateProvider::class |
||
| 54 | ), |
||
| 55 | new Post(securityPostDenormalize: "is_granted('CREATE', object)"), |
||
| 56 | ], |
||
| 57 | normalizationContext: [ |
||
| 58 | 'groups' => ['message:read'], |
||
| 59 | ], |
||
| 60 | denormalizationContext: [ |
||
| 61 | 'groups' => ['message:write'], |
||
| 62 | ], |
||
| 63 | security: "is_granted('ROLE_USER')", |
||
| 64 | processor: MessageProcessor::class, |
||
| 65 | )] |
||
| 66 | #[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] |
||
| 67 | #[ApiFilter(SearchFilter::class, properties: [ |
||
| 68 | 'msgType' => 'exact', |
||
| 69 | 'status' => 'exact', |
||
| 70 | 'sender' => 'exact', |
||
| 71 | 'receivers.receiver' => 'exact', |
||
| 72 | 'receivers.receiverType' => 'exact', |
||
| 73 | 'receivers.tags.tag' => 'exact', |
||
| 74 | 'parent' => 'exact', |
||
| 75 | ])] |
||
| 76 | #[ApiFilter( |
||
| 77 | BooleanFilter::class, |
||
| 78 | properties: ['receivers.read'] |
||
| 79 | )] |
||
| 80 | #[ApiFilter(SearchOrFilter::class, properties: ['title', 'content'])] |
||
| 81 | class Message |
||
| 82 | { |
||
| 83 | public const MESSAGE_TYPE_INBOX = 1; |
||
| 84 | public const MESSAGE_TYPE_GROUP = 5; |
||
| 85 | public const MESSAGE_TYPE_INVITATION = 6; |
||
| 86 | public const MESSAGE_TYPE_CONVERSATION = 7; |
||
| 87 | // status |
||
| 88 | public const MESSAGE_STATUS_DELETED = 3; |
||
| 89 | public const MESSAGE_STATUS_DRAFT = 4; |
||
| 90 | public const MESSAGE_STATUS_INVITATION_PENDING = 5; |
||
| 91 | public const MESSAGE_STATUS_INVITATION_ACCEPTED = 6; |
||
| 92 | public const MESSAGE_STATUS_INVITATION_DENIED = 7; |
||
| 93 | |||
| 94 | #[ORM\Column(name: 'id', type: 'integer')] |
||
| 95 | #[ORM\Id] |
||
| 96 | #[ORM\GeneratedValue] |
||
| 97 | #[Groups(['message:read'])] |
||
| 98 | protected ?int $id = null; |
||
| 99 | |||
| 100 | #[Assert\NotBlank] |
||
| 101 | #[Groups(['message:read', 'message:write'])] |
||
| 102 | #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'sentMessages')] |
||
| 103 | #[ORM\JoinColumn(name: 'user_sender_id', referencedColumnName: 'id', onDelete: 'SET NULL')] |
||
| 104 | protected ?User $sender = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var Collection<int, MessageRelUser> |
||
| 108 | */ |
||
| 109 | #[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageRelUser::class, cascade: ['persist', 'remove'])] |
||
| 110 | #[Groups(['message:write'])] |
||
| 111 | protected Collection $receivers; |
||
| 112 | |||
| 113 | #[Assert\NotBlank] |
||
| 114 | #[Groups(['message:read', 'message:write'])] |
||
| 115 | #[ORM\Column(name: 'msg_type', type: 'smallint', nullable: false)] |
||
| 116 | protected int $msgType; |
||
| 117 | |||
| 118 | #[Assert\NotBlank] |
||
| 119 | #[Groups(['message:read', 'message:write'])] |
||
| 120 | #[ORM\Column(name: 'status', type: 'smallint', nullable: false)] |
||
| 121 | protected int $status; |
||
| 122 | |||
| 123 | #[Groups(['message:read'])] |
||
| 124 | #[ORM\Column(name: 'send_date', type: 'datetime', nullable: false)] |
||
| 125 | protected DateTime $sendDate; |
||
| 126 | |||
| 127 | #[Assert\NotBlank] |
||
| 128 | #[Groups(['message:read', 'message:write'])] |
||
| 129 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
| 130 | protected string $title; |
||
| 131 | |||
| 132 | #[Assert\NotBlank] |
||
| 133 | #[Groups(['message:read', 'message:write'])] |
||
| 134 | #[ORM\Column(name: 'content', type: 'text', nullable: false)] |
||
| 135 | protected string $content; |
||
| 136 | |||
| 137 | #[Groups(['message:read', 'message:write'])] |
||
| 138 | #[ORM\ManyToOne(targetEntity: Usergroup::class)] |
||
| 139 | #[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
| 140 | protected ?Usergroup $group = null; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Collection<int, Message> |
||
| 144 | */ |
||
| 145 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] |
||
| 146 | protected Collection $children; |
||
| 147 | |||
| 148 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
| 149 | #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id')] |
||
| 150 | protected ?Message $parent = null; |
||
| 151 | |||
| 152 | #[Gedmo\Timestampable(on: 'update')] |
||
| 153 | #[ORM\Column(name: 'update_date', type: 'datetime', nullable: true)] |
||
| 154 | protected ?DateTime $updateDate; |
||
| 155 | |||
| 156 | #[ORM\Column(name: 'votes', type: 'integer', nullable: true)] |
||
| 157 | protected ?int $votes; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var Collection<int, MessageAttachment> |
||
| 161 | */ |
||
| 162 | #[Assert\Valid] |
||
| 163 | #[Groups(['message:read', 'message:write'])] |
||
| 164 | #[ORM\OneToMany( |
||
| 165 | mappedBy: 'message', |
||
| 166 | targetEntity: MessageAttachment::class, |
||
| 167 | cascade: ['persist'], |
||
| 168 | orphanRemoval: true, |
||
| 169 | )] |
||
| 170 | protected Collection $attachments; |
||
| 171 | |||
| 172 | public function __construct() |
||
| 173 | { |
||
| 174 | $this->sendDate = new DateTime('now'); |
||
| 175 | $this->updateDate = $this->sendDate; |
||
| 176 | $this->msgType = self::MESSAGE_TYPE_INBOX; |
||
| 177 | $this->content = ''; |
||
| 178 | $this->attachments = new ArrayCollection(); |
||
| 179 | $this->children = new ArrayCollection(); |
||
| 180 | $this->receivers = new ArrayCollection(); |
||
| 181 | $this->votes = 0; |
||
| 182 | $this->status = 0; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return Collection<int, MessageRelUser> |
||
| 187 | */ |
||
| 188 | public function getReceivers(): Collection |
||
| 189 | { |
||
| 190 | return $this->receivers; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function setReceiversFromArray(array $receivers): self |
||
| 194 | { |
||
| 195 | $this->receivers = new ArrayCollection($receivers); |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | #[Groups(['message:read'])] |
||
| 201 | public function getReceiversTo(): array |
||
| 202 | { |
||
| 203 | return $this->receivers |
||
| 204 | ->filter( |
||
| 205 | fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_TO === $messageRelUser->getReceiverType() |
||
| 206 | )->getValues() |
||
| 207 | ; |
||
| 208 | } |
||
| 209 | |||
| 210 | #[Groups(['message:read'])] |
||
| 211 | public function getReceiversCc(): array |
||
| 212 | { |
||
| 213 | return $this->receivers |
||
| 214 | ->filter( |
||
| 215 | fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_CC === $messageRelUser->getReceiverType() |
||
| 216 | ) |
||
| 217 | ->getValues() |
||
| 218 | ; |
||
| 219 | } |
||
| 220 | |||
| 221 | #[Groups(['message:read'])] |
||
| 222 | public function getReceiversSender(): array |
||
| 223 | { |
||
| 224 | return $this->receivers |
||
| 225 | ->filter( |
||
| 226 | fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_SENDER === $messageRelUser->getReceiverType() |
||
| 227 | ) |
||
| 228 | ->getValues() |
||
| 229 | ; |
||
| 230 | } |
||
| 231 | |||
| 232 | #[Groups(['message:read'])] |
||
| 233 | public function getFirstReceiver(): ?MessageRelUser |
||
| 234 | { |
||
| 235 | if ($this->receivers->count() > 0) { |
||
| 236 | return $this->receivers->first(); |
||
| 237 | } |
||
| 238 | |||
| 239 | return null; |
||
| 240 | } |
||
| 241 | |||
| 242 | public function hasUserReceiver(User $receiver): bool |
||
| 243 | { |
||
| 244 | if ($this->receivers->count()) { |
||
| 245 | $criteria = Criteria::create() |
||
| 246 | ->where( |
||
| 247 | Criteria::expr()->eq('receiver', $receiver) |
||
| 248 | ) |
||
| 249 | ->andWhere( |
||
| 250 | Criteria::expr()->eq('message', $this) |
||
| 251 | ) |
||
| 252 | ; |
||
| 253 | |||
| 254 | return $this->receivers->matching($criteria)->count() > 0; |
||
| 255 | } |
||
| 256 | |||
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function addReceiverTo(User $receiver): self |
||
| 261 | { |
||
| 262 | $messageRelUser = (new MessageRelUser()) |
||
| 263 | ->setReceiver($receiver) |
||
| 264 | ->setReceiverType(MessageRelUser::TYPE_TO) |
||
| 265 | ; |
||
| 266 | |||
| 267 | $this->addReceiver($messageRelUser); |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function addReceiver(MessageRelUser $messageRelUser): self |
||
| 273 | { |
||
| 274 | if (!$this->receivers->contains($messageRelUser)) { |
||
| 275 | $this->receivers->add($messageRelUser); |
||
| 276 | |||
| 277 | $messageRelUser->setMessage($this); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function addReceiverCc(User $receiver): self |
||
| 284 | { |
||
| 285 | $messageRelUser = (new MessageRelUser()) |
||
| 286 | ->setReceiver($receiver) |
||
| 287 | ->setReceiverType(MessageRelUser::TYPE_CC) |
||
| 288 | ; |
||
| 289 | |||
| 290 | $this->addReceiver($messageRelUser); |
||
| 291 | |||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function removeReceiver(MessageRelUser $messageRelUser): self |
||
| 296 | { |
||
| 297 | $this->receivers->removeElement($messageRelUser); |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | public function getSender(): ?User |
||
| 303 | { |
||
| 304 | return $this->sender; |
||
| 305 | } |
||
| 306 | |||
| 307 | public function setSender(?User $sender): self |
||
| 308 | { |
||
| 309 | $this->sender = $sender; |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | public function getMsgType(): int |
||
| 315 | { |
||
| 316 | return $this->msgType; |
||
| 317 | } |
||
| 318 | |||
| 319 | public function setMsgType(int $msgType): self |
||
| 320 | { |
||
| 321 | $this->msgType = $msgType; |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function getSendDate(): DateTime |
||
| 327 | { |
||
| 328 | return $this->sendDate; |
||
| 329 | } |
||
| 330 | |||
| 331 | public function setSendDate(DateTime $sendDate): self |
||
| 332 | { |
||
| 333 | $this->sendDate = $sendDate; |
||
| 334 | |||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function getTitle(): string |
||
| 339 | { |
||
| 340 | return $this->title; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function setTitle(string $title): self |
||
| 344 | { |
||
| 345 | $this->title = $title; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | public function getContent(): string |
||
| 351 | { |
||
| 352 | return $this->content; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function setContent(string $content): self |
||
| 356 | { |
||
| 357 | $this->content = $content; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function getUpdateDate(): ?DateTime |
||
| 363 | { |
||
| 364 | return $this->updateDate; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function setUpdateDate(DateTime $updateDate): self |
||
| 368 | { |
||
| 369 | $this->updateDate = $updateDate; |
||
| 370 | |||
| 371 | return $this; |
||
| 372 | } |
||
| 373 | |||
| 374 | public function getId(): ?int |
||
| 375 | { |
||
| 376 | return $this->id; |
||
| 377 | } |
||
| 378 | |||
| 379 | public function getVotes(): int |
||
| 380 | { |
||
| 381 | return $this->votes; |
||
|
|
|||
| 382 | } |
||
| 383 | |||
| 384 | public function setVotes(int $votes): self |
||
| 385 | { |
||
| 386 | $this->votes = $votes; |
||
| 387 | |||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return Collection<int, MessageAttachment> |
||
| 393 | */ |
||
| 394 | public function getAttachments(): Collection |
||
| 395 | { |
||
| 396 | return $this->attachments; |
||
| 397 | } |
||
| 398 | |||
| 399 | public function addAttachment(MessageAttachment $attachment): static |
||
| 400 | { |
||
| 401 | if (!$this->attachments->contains($attachment)) { |
||
| 402 | $this->attachments->add($attachment); |
||
| 403 | $attachment |
||
| 404 | ->setMessage($this) |
||
| 405 | ->setParent($this->sender) |
||
| 406 | ->setCreator($this->sender) |
||
| 407 | ; |
||
| 408 | } |
||
| 409 | |||
| 410 | return $this; |
||
| 411 | } |
||
| 412 | |||
| 413 | public function removeAttachment(MessageAttachment $attachment): static |
||
| 414 | { |
||
| 415 | if ($this->attachments->removeElement($attachment)) { |
||
| 416 | if ($attachment->getMessage() === $this) { |
||
| 417 | $attachment->setMessage(null); |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | public function getParent(): ?self |
||
| 425 | { |
||
| 426 | return $this->parent; |
||
| 427 | } |
||
| 428 | |||
| 429 | public function setParent(?self $parent): self |
||
| 430 | { |
||
| 431 | $this->parent = $parent; |
||
| 432 | |||
| 433 | return $this; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return Collection<int, Message> |
||
| 438 | */ |
||
| 439 | public function getChildren(): Collection |
||
| 440 | { |
||
| 441 | return $this->children; |
||
| 442 | } |
||
| 443 | |||
| 444 | public function addChild(self $child): self |
||
| 450 | } |
||
| 451 | |||
| 452 | public function getGroup(): ?Usergroup |
||
| 453 | { |
||
| 454 | return $this->group; |
||
| 455 | } |
||
| 456 | |||
| 457 | public function setGroup(?Usergroup $group): self |
||
| 458 | { |
||
| 459 | // $this->msgType = self::MESSAGE_TYPE_GROUP; |
||
| 460 | $this->group = $group; |
||
| 461 | |||
| 462 | return $this; |
||
| 463 | } |
||
| 464 | |||
| 465 | public function getStatus(): int |
||
| 466 | { |
||
| 467 | return $this->status; |
||
| 468 | } |
||
| 469 | |||
| 470 | public function setStatus(int $status): self |
||
| 475 | } |
||
| 476 | } |
||
| 477 |