Completed
Push — master ( c3d3e1...3f9344 )
by
unknown
15:40
created

CaseEntity   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 646
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 0
Metric Value
wmc 50
lcom 3
cbo 6
dl 0
loc 646
rs 8.3909
c 0
b 0
f 0

40 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getId() 0 4 1
A setSubject() 0 6 1
A getSubject() 0 4 1
A setDescription() 0 6 1
A getDescription() 0 4 1
A setResolution() 0 6 1
A getResolution() 0 4 1
A setSource() 0 6 1
A getSource() 0 4 1
A setStatus() 0 7 1
A updateClosedAt() 0 11 4
A getStatus() 0 4 1
A setPriority() 0 6 1
A getPriority() 0 4 1
A setRelatedContact() 0 6 1
A getRelatedContact() 0 4 1
A setRelatedAccount() 0 6 1
A getRelatedAccount() 0 4 1
A setAssignedTo() 0 6 1
A getAssignedTo() 0 4 1
A setOwner() 0 6 1
A getOwner() 0 4 1
A getComments() 0 4 1
A addComment() 0 7 1
A removeComment() 0 6 1
A setCreatedAt() 0 6 1
A getCreatedAt() 0 4 1
A setUpdatedAt() 0 6 1
A getUpdatedAt() 0 4 1
A setReportedAt() 0 6 1
A getReportedAt() 0 4 1
A setClosedAt() 0 10 2
A getClosedAt() 0 4 1
A getEmail() 0 9 2
B prePersist() 0 8 5
A preUpdate() 0 7 2
A __toString() 0 4 1
A setOrganization() 0 6 1
A getOrganization() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like CaseEntity 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 CaseEntity, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace OroCRM\Bundle\CaseBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
9
use Oro\Bundle\EmailBundle\Model\EmailHolderInterface;
10
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
11
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;
12
use Oro\Bundle\DataAuditBundle\Metadata\Annotation as Oro;
13
14
use Oro\Bundle\OrganizationBundle\Entity\Organization;
15
use Oro\Bundle\UserBundle\Entity\User;
16
use OroCRM\Bundle\CaseBundle\Model\ExtendCaseEntity;
17
use OroCRM\Bundle\ContactBundle\Entity\Contact;
18
use OroCRM\Bundle\AccountBundle\Entity\Account;
19
20
/**
21
 * @ORM\Entity
22
 * @ORM\Table(
23
 *      name="orocrm_case"
24
 * )
25
 * @ORM\HasLifecycleCallbacks()
26
 * @Oro\Loggable
27
 * @Config(
28
 *      routeName="orocrm_case_index",
29
 *      routeView="orocrm_case_view",
30
 *      defaultValues={
31
 *          "dataaudit"={
32
 *              "auditable"=true
33
 *          },
34
 *          "entity"={
35
 *              "icon"="icon-list-alt"
36
 *          },
37
 *          "ownership"={
38
 *              "owner_type"="USER",
39
 *              "owner_field_name"="owner",
40
 *              "owner_column_name"="owner_id",
41
 *              "organization_field_name"="organization",
42
 *              "organization_column_name"="organization_id"
43
 *          },
44
 *          "security"={
45
 *              "type"="ACL",
46
 *              "category"="account_management"
47
 *          },
48
 *          "grid"={
49
 *              "default"="cases-grid",
50
 *              "context"="cases-for-context-grid"
51
 *          },
52
 *          "tag"={
53
 *              "enabled"=true,
54
 *              "immutable"=true
55
 *          }
56
 *      }
57
 * )
58
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
59
 */
60
class CaseEntity extends ExtendCaseEntity implements EmailHolderInterface
61
{
62
    /**
63
     * @var integer
64
     *
65
     * @ORM\Column(type="integer")
66
     * @ORM\Id
67
     * @ORM\GeneratedValue(strategy="AUTO")
68
     * @ConfigField(
69
     *      defaultValues={
70
     *          "dataaudit"={
71
     *              "auditable"=true
72
     *          }
73
     *      }
74
     * )
75
     */
76
    protected $id;
77
78
    /**
79
     * @var string
80
     *
81
     * @ORM\Column(name="subject", type="string", length=255)
82
     * @Oro\Versioned
83
     * @ConfigField(
84
     *      defaultValues={
85
     *          "dataaudit"={
86
     *              "auditable"=true
87
     *          }
88
     *      }
89
     * )
90
     */
91
    protected $subject;
92
93
    /**
94
     * @var string
95
     *
96
     * @ORM\Column(name="description", type="text", nullable=true)
97
     * @Oro\Versioned
98
     * @ConfigField(
99
     *      defaultValues={
100
     *          "dataaudit"={
101
     *              "auditable"=true
102
     *          }
103
     *      }
104
     * )
105
     */
106
    protected $description;
107
108
    /**
109
     * @var string
110
     *
111
     * @ORM\Column(name="resolution", type="text", nullable=true)
112
     * @Oro\Versioned
113
     * @ConfigField(
114
     *      defaultValues={
115
     *          "dataaudit"={
116
     *              "auditable"=true
117
     *          }
118
     *      }
119
     * )
120
     */
121
    protected $resolution;
122
123
    /**
124
     * @var CaseSource
125
     *
126
     * @ORM\ManyToOne(targetEntity="CaseSource")
127
     * @ORM\JoinColumn(name="source_name", referencedColumnName="name", onDelete="SET NULL")
128
     * @Oro\Versioned
129
     * @ConfigField(
130
     *      defaultValues={
131
     *          "dataaudit"={
132
     *              "auditable"=true
133
     *          }
134
     *      }
135
     * )
136
     */
137
    protected $source;
138
139
    /**
140
     * @var CaseStatus
141
     *
142
     * @ORM\ManyToOne(targetEntity="CaseStatus")
143
     * @ORM\JoinColumn(name="status_name", referencedColumnName="name", onDelete="SET NULL")
144
     * @Oro\Versioned
145
     * @ConfigField(
146
     *      defaultValues={
147
     *          "dataaudit"={
148
     *              "auditable"=true
149
     *          }
150
     *      }
151
     * )
152
     */
153
    protected $status;
154
155
    /**
156
     * @var CasePriority
157
     *
158
     * @ORM\ManyToOne(targetEntity="CasePriority")
159
     * @ORM\JoinColumn(name="priority_name", referencedColumnName="name", onDelete="SET NULL")
160
     * @Oro\Versioned
161
     * @ConfigField(
162
     *      defaultValues={
163
     *          "dataaudit"={
164
     *              "auditable"=true
165
     *          }
166
     *      }
167
     * )
168
     */
169
    protected $priority;
170
171
    /**
172
     * @var Contact
173
     *
174
     * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact")
175
     * @ORM\JoinColumn(name="related_contact_id", referencedColumnName="id", onDelete="SET NULL")
176
     * @ConfigField(
177
     *      defaultValues={
178
     *          "dataaudit"={
179
     *              "auditable"=true
180
     *          }
181
     *      }
182
     * )
183
     */
184
    protected $relatedContact;
185
186
    /**
187
     * @var Account
188
     *
189
     * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\AccountBundle\Entity\Account")
190
     * @ORM\JoinColumn(name="related_account_id", referencedColumnName="id", onDelete="SET NULL")
191
     * @ConfigField(
192
     *      defaultValues={
193
     *          "dataaudit"={
194
     *              "auditable"=true
195
     *          }
196
     *      }
197
     * )
198
     */
199
    protected $relatedAccount;
200
201
    /**
202
     * @var User
203
     *
204
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User")
205
     * @ORM\JoinColumn(name="assigned_to_id", referencedColumnName="id", onDelete="SET NULL")
206
     * @ConfigField(
207
     *      defaultValues={
208
     *          "dataaudit"={
209
     *              "auditable"=true
210
     *          }
211
     *      }
212
     * )
213
     * @Oro\Versioned
214
     */
215
    protected $assignedTo;
216
217
    /**
218
     * @var User
219
     *
220
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User")
221
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id", onDelete="SET NULL")
222
     * @Oro\Versioned
223
     * @ConfigField(
224
     *      defaultValues={
225
     *          "dataaudit"={
226
     *              "auditable"=true
227
     *          }
228
     *      }
229
     * )
230
     */
231
    protected $owner;
232
233
    /**
234
     * @var Collection
235
     *
236
     * @ORM\OneToMany(
237
     *     targetEntity="CaseComment",
238
     *     mappedBy="case",
239
     *     cascade={"ALL"},
240
     *     orphanRemoval=true
241
     * )
242
     * @ORM\OrderBy({"createdAt"="DESC"})
243
     */
244
    protected $comments;
245
246
    /**
247
     * @var \DateTime
248
     *
249
     * @ORM\Column(type="datetime")
250
     * @ConfigField(
251
     *      defaultValues={
252
     *          "entity"={
253
     *              "label"="oro.ui.created_at"
254
     *          }
255
     *      }
256
     * )
257
     */
258
    protected $createdAt;
259
260
    /**
261
     * @var \DateTime
262
     *
263
     * @ORM\Column(type="datetime", nullable=true)
264
     * @ConfigField(
265
     *      defaultValues={
266
     *          "entity"={
267
     *              "label"="oro.ui.updated_at"
268
     *          }
269
     *      }
270
     * )
271
     */
272
    protected $updatedAt;
273
274
    /**
275
     * @var \DateTime
276
     *
277
     * @ORM\Column(type="datetime")
278
     */
279
    protected $reportedAt;
280
281
    /**
282
     * @var \DateTime
283
     *
284
     * @ORM\Column(type="datetime", nullable=true)
285
     */
286
    protected $closedAt;
287
288
    /**
289
     * Flag to update closedAt field when status is set to closed.
290
     * Use null instead of false because of behaviour of BeSimpleSoapBundle.
291
     *
292
     * @var bool
293
     */
294
    private $updateClosedAt = null;
295
296
    /**
297
     * @var Organization
298
     *
299
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization")
300
     * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL")
301
     */
302
    protected $organization;
303
304
    public function __construct()
305
    {
306
        parent::__construct();
307
308
        $this->comments = new ArrayCollection();
309
    }
310
311
    /**
312
     * @return integer
313
     */
314
    public function getId()
315
    {
316
        return $this->id;
317
    }
318
319
    /**
320
     * @param string $subject
321
     * @return CaseEntity
322
     */
323
    public function setSubject($subject)
324
    {
325
        $this->subject = $subject;
326
327
        return $this;
328
    }
329
330
    /**
331
     * @return string
332
     */
333
    public function getSubject()
334
    {
335
        return $this->subject;
336
    }
337
338
    /**
339
     * @param string $description
340
     * @return CaseEntity
341
     */
342
    public function setDescription($description)
343
    {
344
        $this->description = $description;
345
346
        return $this;
347
    }
348
349
    /**
350
     * @return string
351
     */
352
    public function getDescription()
353
    {
354
        return $this->description;
355
    }
356
357
    /**
358
     * @param string $resolution
359
     * @return CaseEntity
360
     */
361
    public function setResolution($resolution)
362
    {
363
        $this->resolution = $resolution;
364
365
        return $this;
366
    }
367
368
    /**
369
     * @return string
370
     */
371
    public function getResolution()
372
    {
373
        return $this->resolution;
374
    }
375
376
    /**
377
     * @param CaseSource|null $source
378
     * @return CaseEntity
379
     */
380
    public function setSource($source)
381
    {
382
        $this->source = $source;
383
384
        return $this;
385
    }
386
387
    /**
388
     * @return CaseSource
389
     */
390
    public function getSource()
391
    {
392
        return $this->source;
393
    }
394
395
    /**
396
     * @param CaseStatus|null $status
397
     * @return CaseEntity
398
     */
399
    public function setStatus($status)
400
    {
401
        $this->updateClosedAt($status, $this->status);
402
        $this->status = $status;
403
404
        return $this;
405
    }
406
407
    /**
408
     * @param mixed $newStatus
409
     * @param mixed $oldStatus
410
     */
411
    protected function updateClosedAt($newStatus, $oldStatus)
412
    {
413
        if ($newStatus instanceof CaseStatus &&
414
            $newStatus->getName() == CaseStatus::STATUS_CLOSED &&
415
            !$newStatus->isEqualTo($oldStatus)
416
        ) {
417
            $this->updateClosedAt = true;
418
        } else {
419
            $this->updateClosedAt = null;
420
        }
421
    }
422
423
    /**
424
     * @return CaseStatus
425
     */
426
    public function getStatus()
427
    {
428
        return $this->status;
429
    }
430
431
    /**
432
     * @param CasePriority|null $priority
433
     * @return CaseEntity
434
     */
435
    public function setPriority($priority)
436
    {
437
        $this->priority = $priority;
438
439
        return $this;
440
    }
441
442
    /**
443
     * @return CasePriority
444
     */
445
    public function getPriority()
446
    {
447
        return $this->priority;
448
    }
449
450
    /**
451
     * @param Contact|null $relatedContact
452
     * @return CaseEntity
453
     */
454
    public function setRelatedContact($relatedContact = null)
455
    {
456
        $this->relatedContact = $relatedContact;
457
458
        return $this;
459
    }
460
461
    /**
462
     * @return Contact
463
     */
464
    public function getRelatedContact()
465
    {
466
        return $this->relatedContact;
467
    }
468
469
    /**
470
     * @param Account|null $relatedAccount
471
     * @return CaseEntity
472
     */
473
    public function setRelatedAccount($relatedAccount = null)
474
    {
475
        $this->relatedAccount = $relatedAccount;
476
477
        return $this;
478
    }
479
480
    /**
481
     * @return Account
482
     */
483
    public function getRelatedAccount()
484
    {
485
        return $this->relatedAccount;
486
    }
487
488
    /**
489
     * @param User $assignee
490
     * @return CaseEntity
491
     */
492
    public function setAssignedTo($assignee)
493
    {
494
        $this->assignedTo = $assignee;
495
496
        return $this;
497
    }
498
499
    /**
500
     * @return User|null
501
     */
502
    public function getAssignedTo()
503
    {
504
        return $this->assignedTo;
505
    }
506
507
    /**
508
     * @param User $owner
509
     * @return CaseEntity
510
     */
511
    public function setOwner($owner)
512
    {
513
        $this->owner = $owner;
514
515
        return $this;
516
    }
517
518
    /**
519
     * @return User
520
     */
521
    public function getOwner()
522
    {
523
        return $this->owner;
524
    }
525
526
    /**
527
     * @return Collection
528
     */
529
    public function getComments()
530
    {
531
        return $this->comments;
532
    }
533
534
    /**
535
     * @param CaseComment $comment
536
     * @return CaseEntity
537
     */
538
    public function addComment(CaseComment $comment)
539
    {
540
        $this->comments->add($comment);
541
        $comment->setCase($this);
542
543
        return $this;
544
    }
545
546
    /**
547
     * @param CaseComment $comment
548
     * @return CaseEntity
549
     */
550
    public function removeComment(CaseComment $comment)
551
    {
552
        $this->comments->removeElement($comment);
553
554
        return $this;
555
    }
556
557
    /**
558
     * @param \DateTime|null $createdAt
559
     * @return CaseEntity
560
     */
561
    public function setCreatedAt(\DateTime $createdAt = null)
562
    {
563
        $this->createdAt = $createdAt;
564
565
        return $this;
566
    }
567
568
    /**
569
     * @return \DateTime
570
     */
571
    public function getCreatedAt()
572
    {
573
        return $this->createdAt;
574
    }
575
576
    /**
577
     * @param \DateTime|null $updatedAt
578
     * @return CaseEntity
579
     */
580
    public function setUpdatedAt(\DateTime $updatedAt = null)
581
    {
582
        $this->updatedAt = $updatedAt;
583
584
        return $this;
585
    }
586
587
    /**
588
     * @return \DateTime
589
     */
590
    public function getUpdatedAt()
591
    {
592
        return $this->updatedAt;
593
    }
594
595
    /**
596
     * @param \DateTime $reportedAt
597
     * @return CaseEntity
598
     */
599
    public function setReportedAt(\DateTime $reportedAt = null)
600
    {
601
        $this->reportedAt = $reportedAt;
602
603
        return $this;
604
    }
605
606
    /**
607
     * @return \DateTime
608
     */
609
    public function getReportedAt()
610
    {
611
        return $this->reportedAt;
612
    }
613
614
    /**
615
     * @param \DateTime $closedAt
616
     * @return CaseEntity
617
     */
618
    public function setClosedAt(\DateTime $closedAt = null)
619
    {
620
        $this->closedAt = $closedAt;
621
622
        if ($this->closedAt) {
623
            unset($this->updateClosedAt);
624
        }
625
626
        return $this;
627
    }
628
629
    /**
630
     * @return \DateTime
631
     */
632
    public function getClosedAt()
633
    {
634
        return $this->closedAt;
635
    }
636
637
    /**
638
     * Get the primary email address of the related contact
639
     *
640
     * @return string
641
     */
642
    public function getEmail()
643
    {
644
        $contact = $this->getRelatedContact();
645
        if (!$contact) {
646
            return null;
647
        }
648
649
        return $contact->getEmail();
650
    }
651
652
    /**
653
     * @ORM\PrePersist
654
     */
655
    public function prePersist()
656
    {
657
        $this->createdAt  = $this->createdAt ? $this->createdAt : new \DateTime('now', new \DateTimeZone('UTC'));
658
        $this->reportedAt = $this->reportedAt? $this->reportedAt : new \DateTime('now', new \DateTimeZone('UTC'));
659
        if ($this->updateClosedAt && !$this->closedAt) {
660
            $this->setClosedAt(new \DateTime('now', new \DateTimeZone('UTC')));
661
        }
662
    }
663
664
    /**
665
     * @ORM\PreUpdate
666
     */
667
    public function preUpdate()
668
    {
669
        $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
670
        if ($this->updateClosedAt) {
671
            $this->setClosedAt(new \DateTime('now', new \DateTimeZone('UTC')));
672
        }
673
    }
674
675
    /**
676
     * @return string
677
     */
678
    public function __toString()
679
    {
680
        return (string)$this->subject;
681
    }
682
683
    /**
684
     * Set organization
685
     *
686
     * @param Organization $organization
687
     * @return CaseEntity
688
     */
689
    public function setOrganization(Organization $organization = null)
690
    {
691
        $this->organization = $organization;
692
693
        return $this;
694
    }
695
696
    /**
697
     * Get organization
698
     *
699
     * @return Organization
700
     */
701
    public function getOrganization()
702
    {
703
        return $this->organization;
704
    }
705
}
706