Completed
Push — master ( d82d84...28a6d6 )
by Yann
03:27
created

Notification::addNotificationAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Yokai\MessengerBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Util\ClassUtils;
8
use Symfony\Component\HttpFoundation\File\File;
9
use Yokai\MessengerBundle\Recipient\DoctrineRecipientInterface;
10
11
/**
12
 * Notification Doctrine ORM entity
13
 */
14
class Notification
15
{
16
    /**
17
     * @var int
18
     */
19
    private $id;
20
21
    /**
22
     * @var string
23
     */
24
    private $subject;
25
26
    /**
27
     * @var string
28
     */
29
    private $body;
30
31
    /**
32
     * @var NotificationAttachment|Collection
33
     */
34
    private $attachments;
35
36
    /**
37
     * @var \DateTime
38
     */
39
    private $recordedAt;
40
41
    /**
42
     * @var string
43
     */
44
    private $recipientClass;
45
46
    /**
47
     * @var int
48
     */
49
    private $recipientId;
50
51
    /**
52
     * @var \DateTime|null
53 3
     */
54
    private $deliveredAt;
55 3
56 3
    /**
57 3
     * @param string                     $subject
58 3
     * @param string                     $body
59 3
     * @param DoctrineRecipientInterface $recipient
60 3
     */
61
    public function __construct($subject, $body, DoctrineRecipientInterface $recipient)
62
    {
63
        $this->subject = $subject;
64
        $this->body = $body;
65 1
        $this->recipientClass = ClassUtils::getClass($recipient);
66
        $this->recipientId = $recipient->getId();
67 1
        $this->recordedAt = new \DateTime('now');
68
69
        $this->attachments = new ArrayCollection();
70
    }
71
72
    /**
73 2
     * @return integer
74
     */
75 2
    public function getId()
76
    {
77
        return $this->id;
78
    }
79
80
    /**
81 2
     * @return string
82
     */
83 2
    public function getSubject()
84
    {
85
        return $this->subject;
86
    }
87
88
    /**
89 2
     * @return string
90
     */
91 2
    public function getBody()
92
    {
93
        return $this->body;
94
    }
95
96
    /**
97 2
     * @return NotificationAttachment[]
98
     */
99 2
    public function getAttachments()
100
    {
101
        return $this->attachments->toArray();
0 ignored issues
show
Bug introduced by
The method toArray does only exist in Doctrine\Common\Collections\Collection, but not in Yokai\MessengerBundle\En...\NotificationAttachment.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
    }
103
104
    /**
105 1
     * @return string
106
     */
107 1
    public function getRecipientClass()
108
    {
109
        return $this->recipientClass;
110
    }
111
112
    /**
113 1
     * @return int
114
     */
115 1
    public function getRecipientId()
116
    {
117
        return $this->recipientId;
118
    }
119
120
    /**
121 2
     * @return \DateTime
122
     */
123 2
    public function getRecordedAt()
124
    {
125
        return $this->recordedAt;
126
    }
127
128 2
    /**
129
     * @return bool
130 2
     */
131 1
    public function isDelivered()
132
    {
133
        return null !== $this->deliveredAt;
134 2
    }
135 2
136
    /**
137
     * @return \DateTime|null
138
     */
139
    public function getDeliveredAt()
140
    {
141
        return $this->deliveredAt;
142
    }
143
144
    /**
145
     */
146
    public function setDelivered()
147
    {
148
        if (null !== $this->deliveredAt) {
149
            return; //immutable
150
        }
151
152
        $this->deliveredAt = new \DateTime('now');
153
    }
154
155
    /**
156
     * @param NotificationAttachment $attachment
157
     */
158
    public function addNotificationAttachment(NotificationAttachment $attachment)
159
    {
160
        $this->attachments->add($attachment);
0 ignored issues
show
Bug introduced by
The method add does only exist in Doctrine\Common\Collections\Collection, but not in Yokai\MessengerBundle\En...\NotificationAttachment.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
161
    }
162
}
163