| Total Complexity | 43 |
| Total Lines | 434 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 32 | #[ORM\Table(name: 'message')] |
||
| 33 | #[ORM\Index(columns: ['user_sender_id'], name: 'idx_message_user_sender')] |
||
| 34 | #[ORM\Index(columns: ['group_id'], name: 'idx_message_group')] |
||
| 35 | #[ORM\Index(columns: ['msg_type'], name: 'idx_message_type')] |
||
| 36 | #[ORM\Entity(repositoryClass: MessageRepository::class)] |
||
| 37 | #[ApiResource( |
||
| 38 | operations: [ |
||
| 39 | new Get(security: "is_granted('VIEW', object)"), |
||
| 40 | new Put(security: "is_granted('EDIT', object)"), |
||
| 41 | new Delete(security: "is_granted('DELETE', object)"), |
||
| 42 | new GetCollection( |
||
| 43 | uriTemplate: '/messages', |
||
| 44 | security: "is_granted('ROLE_USER')", |
||
| 45 | name: 'get_all_messages' |
||
| 46 | ), |
||
| 47 | new GetCollection( |
||
| 48 | uriTemplate: '/messages/by-group/list', |
||
| 49 | security: "is_granted('ROLE_USER')", |
||
| 50 | name: 'get_messages_by_social_group', |
||
| 51 | provider: MessageByGroupStateProvider::class |
||
| 52 | ), |
||
| 53 | new Post(securityPostDenormalize: "is_granted('CREATE', object)"), |
||
| 54 | ], |
||
| 55 | normalizationContext: [ |
||
| 56 | 'groups' => ['message:read'], |
||
| 57 | ], |
||
| 58 | denormalizationContext: [ |
||
| 59 | 'groups' => ['message:write'], |
||
| 60 | ], |
||
| 61 | security: "is_granted('ROLE_USER')", |
||
| 62 | processor: MessageProcessor::class, |
||
| 63 | )] |
||
| 64 | #[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] |
||
| 65 | #[ApiFilter( |
||
| 66 | filterClass: SearchFilter::class, |
||
| 67 | properties: [ |
||
| 68 | 'msgType' => 'exact', |
||
| 69 | 'status' => 'exact', |
||
| 70 | 'sender' => 'exact', |
||
| 71 | 'receivers.receiver' => 'exact', |
||
| 72 | 'receivers.tags.tag' => 'exact', |
||
| 73 | 'parent' => 'exact', |
||
| 74 | ] |
||
| 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 | #[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageFeedback::class, orphanRemoval: true)] |
||
| 173 | protected Collection $likes; |
||
| 174 | |||
| 175 | public function __construct() |
||
| 176 | { |
||
| 177 | $this->sendDate = new DateTime('now'); |
||
| 178 | $this->updateDate = $this->sendDate; |
||
| 179 | $this->msgType = self::MESSAGE_TYPE_INBOX; |
||
| 180 | $this->content = ''; |
||
| 181 | $this->attachments = new ArrayCollection(); |
||
| 182 | $this->children = new ArrayCollection(); |
||
| 183 | $this->receivers = new ArrayCollection(); |
||
| 184 | $this->likes = new ArrayCollection(); |
||
| 185 | $this->votes = 0; |
||
| 186 | $this->status = 0; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return Collection<int, MessageRelUser> |
||
| 191 | */ |
||
| 192 | public function getReceivers(): Collection |
||
| 193 | { |
||
| 194 | return $this->receivers; |
||
| 195 | } |
||
| 196 | |||
| 197 | #[Groups(['message:read'])] |
||
| 198 | public function getReceiversTo(): array |
||
| 199 | { |
||
| 200 | return $this->receivers |
||
| 201 | ->filter( |
||
| 202 | fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_TO === $messageRelUser->getReceiverType() |
||
| 203 | )->getValues() |
||
| 204 | ; |
||
| 205 | } |
||
| 206 | |||
| 207 | #[Groups(['message:read'])] |
||
| 208 | public function getReceiversCc(): array |
||
| 209 | { |
||
| 210 | return $this->receivers |
||
| 211 | ->filter( |
||
| 212 | fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_CC === $messageRelUser->getReceiverType() |
||
| 213 | ) |
||
| 214 | ->getValues() |
||
| 215 | ; |
||
| 216 | } |
||
| 217 | |||
| 218 | #[Groups(['message:read'])] |
||
| 219 | public function getFirstReceiver(): ?MessageRelUser |
||
| 220 | { |
||
| 221 | if ($this->receivers->count() > 0) { |
||
| 222 | return $this->receivers->first(); |
||
| 223 | } |
||
| 224 | |||
| 225 | return null; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function hasUserReceiver(User $receiver): bool |
||
| 229 | { |
||
| 230 | if ($this->receivers->count()) { |
||
| 231 | $criteria = Criteria::create() |
||
| 232 | ->where( |
||
| 233 | Criteria::expr()->eq('receiver', $receiver) |
||
| 234 | ) |
||
| 235 | ->andWhere( |
||
| 236 | Criteria::expr()->eq('message', $this) |
||
| 237 | ) |
||
| 238 | ; |
||
| 239 | |||
| 240 | return $this->receivers->matching($criteria)->count() > 0; |
||
| 241 | } |
||
| 242 | |||
| 243 | return false; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function addReceiverTo(User $receiver): self |
||
| 247 | { |
||
| 248 | $messageRelUser = (new MessageRelUser()) |
||
| 249 | ->setReceiver($receiver) |
||
| 250 | ->setReceiverType(MessageRelUser::TYPE_TO) |
||
| 251 | ; |
||
| 252 | |||
| 253 | $this->addReceiver($messageRelUser); |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function addReceiver(MessageRelUser $messageRelUser): self |
||
| 259 | { |
||
| 260 | if (!$this->receivers->contains($messageRelUser)) { |
||
| 261 | $this->receivers->add($messageRelUser); |
||
| 262 | |||
| 263 | $messageRelUser->setMessage($this); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $this; |
||
| 267 | } |
||
| 268 | |||
| 269 | public function addReceiverCc(User $receiver): self |
||
| 270 | { |
||
| 271 | $messageRelUser = (new MessageRelUser()) |
||
| 272 | ->setReceiver($receiver) |
||
| 273 | ->setReceiverType(MessageRelUser::TYPE_CC) |
||
| 274 | ; |
||
| 275 | |||
| 276 | $this->addReceiver($messageRelUser); |
||
| 277 | |||
| 278 | return $this; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function removeReceiver(MessageRelUser $messageRelUser): self |
||
| 282 | { |
||
| 283 | $this->receivers->removeElement($messageRelUser); |
||
| 284 | |||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function getSender(): ?User |
||
| 289 | { |
||
| 290 | return $this->sender; |
||
| 291 | } |
||
| 292 | |||
| 293 | public function setSender(?User $sender): self |
||
| 294 | { |
||
| 295 | $this->sender = $sender; |
||
| 296 | |||
| 297 | return $this; |
||
| 298 | } |
||
| 299 | |||
| 300 | public function getMsgType(): int |
||
| 301 | { |
||
| 302 | return $this->msgType; |
||
| 303 | } |
||
| 304 | |||
| 305 | public function setMsgType(int $msgType): self |
||
| 306 | { |
||
| 307 | $this->msgType = $msgType; |
||
| 308 | |||
| 309 | return $this; |
||
| 310 | } |
||
| 311 | |||
| 312 | public function getSendDate(): DateTime |
||
| 313 | { |
||
| 314 | return $this->sendDate; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function setSendDate(DateTime $sendDate): self |
||
| 318 | { |
||
| 319 | $this->sendDate = $sendDate; |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | public function getTitle(): string |
||
| 325 | { |
||
| 326 | return $this->title; |
||
| 327 | } |
||
| 328 | |||
| 329 | public function setTitle(string $title): self |
||
| 330 | { |
||
| 331 | $this->title = $title; |
||
| 332 | |||
| 333 | return $this; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function getContent(): string |
||
| 337 | { |
||
| 338 | return $this->content; |
||
| 339 | } |
||
| 340 | |||
| 341 | public function setContent(string $content): self |
||
| 342 | { |
||
| 343 | $this->content = $content; |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getUpdateDate(): ?DateTime |
||
| 349 | { |
||
| 350 | return $this->updateDate; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function setUpdateDate(DateTime $updateDate): self |
||
| 354 | { |
||
| 355 | $this->updateDate = $updateDate; |
||
| 356 | |||
| 357 | return $this; |
||
| 358 | } |
||
| 359 | |||
| 360 | public function getId(): ?int |
||
| 361 | { |
||
| 362 | return $this->id; |
||
| 363 | } |
||
| 364 | |||
| 365 | public function getVotes(): int |
||
| 368 | } |
||
| 369 | |||
| 370 | public function setVotes(int $votes): self |
||
| 371 | { |
||
| 372 | $this->votes = $votes; |
||
| 373 | |||
| 374 | return $this; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return Collection<int, MessageAttachment> |
||
| 379 | */ |
||
| 380 | public function getAttachments(): Collection |
||
| 381 | { |
||
| 382 | return $this->attachments; |
||
| 383 | } |
||
| 384 | |||
| 385 | public function addAttachment(MessageAttachment $attachment): static |
||
| 386 | { |
||
| 387 | if (!$this->attachments->contains($attachment)) { |
||
| 388 | $this->attachments->add($attachment); |
||
| 389 | $attachment |
||
| 390 | ->setMessage($this) |
||
| 391 | ->setParent($this->sender) |
||
| 392 | ->setCreator($this->sender) |
||
| 393 | ; |
||
| 394 | } |
||
| 395 | |||
| 396 | return $this; |
||
| 397 | } |
||
| 398 | |||
| 399 | public function removeAttachment(MessageAttachment $attachment): static |
||
| 400 | { |
||
| 401 | if ($this->attachments->removeElement($attachment)) { |
||
| 402 | if ($attachment->getMessage() === $this) { |
||
| 403 | $attachment->setMessage(null); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | return $this; |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getParent(): ?self |
||
| 411 | { |
||
| 412 | return $this->parent; |
||
| 413 | } |
||
| 414 | |||
| 415 | public function setParent(?self $parent): self |
||
| 416 | { |
||
| 417 | $this->parent = $parent; |
||
| 418 | |||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return Collection<int, Message> |
||
| 424 | */ |
||
| 425 | public function getChildren(): Collection |
||
| 426 | { |
||
| 427 | return $this->children; |
||
| 428 | } |
||
| 429 | |||
| 430 | public function addChild(self $child): self |
||
| 431 | { |
||
| 432 | $this->children[] = $child; |
||
| 433 | $child->setParent($this); |
||
| 434 | |||
| 435 | return $this; |
||
| 436 | } |
||
| 437 | |||
| 438 | public function getGroup(): ?Usergroup |
||
| 439 | { |
||
| 440 | return $this->group; |
||
| 441 | } |
||
| 442 | |||
| 443 | public function setGroup(?Usergroup $group): self |
||
| 444 | { |
||
| 445 | // $this->msgType = self::MESSAGE_TYPE_GROUP; |
||
| 446 | $this->group = $group; |
||
| 447 | |||
| 448 | return $this; |
||
| 449 | } |
||
| 450 | |||
| 451 | public function getStatus(): int |
||
| 452 | { |
||
| 453 | return $this->status; |
||
| 454 | } |
||
| 455 | |||
| 456 | public function setStatus(int $status): self |
||
| 457 | { |
||
| 458 | $this->status = $status; |
||
| 459 | |||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | public function getLikes(): Collection |
||
| 466 | } |
||
| 467 | } |
||
| 468 |