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