]>
Commit | Line | Data |
---|---|---|
22a4b20e TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Entity; | |
4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | |
6 | use Doctrine\ORM\Mapping as ORM; | |
7 | use Psr\Log\LoggerInterface; | |
8 | use Psr\Log\NullLogger; | |
9 | use Wallabag\CoreBundle\Notifications\ActionInterface; | |
10 | use Wallabag\CoreBundle\Notifications\NotificationInterface; | |
11 | use Wallabag\UserBundle\Entity\User; | |
12 | ||
13 | /** | |
14 | * Class Notification | |
15 | * | |
16 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\NotificationRepository") | |
17 | * @ORM\Table(name="`notification`") | |
18 | */ | |
19 | class Notification implements NotificationInterface { | |
20 | ||
21 | /** | |
22 | * @var int | |
23 | * | |
24 | * @ORM\Column(name="id", type="integer") | |
25 | * @ORM\Id | |
26 | * @ORM\GeneratedValue(strategy="AUTO") | |
27 | * | |
28 | */ | |
29 | protected $id; | |
30 | ||
31 | /** | |
32 | * @var int $type | |
33 | * | |
34 | * @ORM\Column(name="type", type="integer") | |
35 | */ | |
36 | protected $type; | |
37 | ||
38 | /** | |
39 | * @var User $user | |
40 | * | |
41 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="notifications") | |
42 | */ | |
43 | protected $user; | |
44 | ||
45 | /** | |
46 | * @var \DateTime $timestamp | |
47 | * | |
48 | * @ORM\Column(name="timestamp", type="datetime") | |
49 | */ | |
50 | protected $timestamp; | |
51 | ||
52 | /** | |
53 | * @var string $title | |
54 | * | |
55 | * @ORM\Column(name="title", type="string") | |
56 | */ | |
57 | protected $title; | |
58 | ||
59 | /** | |
60 | * @var boolean $read | |
61 | * | |
62 | * @ORM\Column(name="read", type="boolean") | |
63 | */ | |
64 | protected $read; | |
65 | ||
66 | protected $logger; | |
67 | ||
68 | /** | |
69 | * @var ArrayCollection<ActionInterface> | |
70 | * | |
71 | * @ORM\Column(name="actions", type="array", nullable=true) | |
72 | */ | |
73 | protected $actions; | |
74 | ||
75 | protected $actionTypes = []; | |
76 | ||
77 | const TYPE_ADMIN = 0; | |
78 | const TYPE_USER = 1; | |
79 | const TYPE_RELEASE = 2; | |
7f699602 | 80 | const TYPE_SHARE = 3; |
22a4b20e TC |
81 | |
82 | public function __construct(User $user = null) | |
83 | { | |
84 | $this->logger = new NullLogger(); | |
85 | $this->timestamp = new \DateTime(); | |
86 | $this->actions = new ArrayCollection(); | |
87 | $this->read = false; | |
88 | $this->user = $user; | |
89 | } | |
90 | ||
91 | /** | |
92 | * @param LoggerInterface $logger | |
93 | * @return NotificationInterface | |
94 | */ | |
95 | public function setLogger(LoggerInterface $logger) | |
96 | { | |
97 | $this->logger = $logger; | |
98 | return $this; | |
99 | } | |
100 | ||
101 | /** | |
102 | * @return mixed | |
103 | */ | |
104 | public function getId() | |
105 | { | |
106 | return $this->id; | |
107 | } | |
108 | ||
109 | /** | |
110 | * @return mixed | |
111 | */ | |
112 | public function getType() | |
113 | { | |
114 | return $this->type; | |
115 | } | |
116 | ||
117 | /** | |
118 | * @param mixed $type | |
119 | * @return NotificationInterface | |
120 | */ | |
121 | public function setType($type) | |
122 | { | |
123 | $this->type = $type; | |
124 | return $this; | |
125 | } | |
126 | ||
127 | /** | |
128 | * @return User | |
129 | */ | |
130 | public function getUser() | |
131 | { | |
132 | return $this->user; | |
133 | } | |
134 | ||
135 | /** | |
136 | * @param User $user | |
137 | * @return NotificationInterface | |
138 | */ | |
139 | public function setUser(User $user) | |
140 | { | |
141 | $this->user = $user; | |
142 | return $this; | |
143 | } | |
144 | ||
145 | /** | |
146 | * @return \DateTime | |
147 | */ | |
148 | public function getTimestamp() | |
149 | { | |
150 | return $this->timestamp; | |
151 | } | |
152 | ||
153 | /** | |
154 | * @param \DateTime $timestamp | |
155 | * @return NotificationInterface | |
156 | */ | |
157 | public function setTimestamp(\DateTime $timestamp) | |
158 | { | |
159 | $this->timestamp = $timestamp; | |
160 | return $this; | |
161 | } | |
162 | ||
163 | /** | |
164 | * @return string | |
165 | */ | |
166 | public function getTitle() | |
167 | { | |
168 | return $this->title; | |
169 | } | |
170 | ||
171 | /** | |
172 | * @param string $title | |
173 | * @return NotificationInterface | |
174 | */ | |
175 | public function setTitle($title) | |
176 | { | |
177 | $this->title = $title; | |
178 | return $this; | |
179 | } | |
180 | ||
181 | /** | |
182 | * @return bool | |
183 | */ | |
184 | public function isRead() | |
185 | { | |
186 | return $this->read; | |
187 | } | |
188 | ||
189 | /** | |
190 | * @param bool $read | |
191 | * @return NotificationInterface | |
192 | */ | |
193 | public function setRead($read) | |
194 | { | |
195 | $this->read = $read; | |
196 | return $this; | |
197 | } | |
198 | ||
199 | /** | |
200 | * @param ActionInterface $action | |
201 | * @return NotificationInterface | |
202 | * @throws \InvalidArgumentException | |
203 | */ | |
204 | public function addAction(ActionInterface $action) | |
205 | { | |
206 | if (isset($this->actionTypes[$action->getType()])) { | |
207 | throw new \InvalidArgumentException('The notification already has a primary action'); | |
208 | } | |
209 | $this->actionTypes[$action->getType()] = true; | |
210 | $this->actions->add($action); | |
211 | return $this; | |
212 | } | |
213 | ||
214 | /** | |
215 | * @return ArrayCollection<ActionInterface> | |
216 | */ | |
217 | public function getActions() | |
218 | { | |
219 | return $this->actions; | |
220 | } | |
221 | } |