]>
Commit | Line | Data |
---|---|---|
e0f9010e 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 | protected $id; | |
29 | ||
30 | /** | |
31 | * @var int | |
32 | * | |
33 | * @ORM\Column(name="type", type="integer") | |
34 | */ | |
35 | protected $type; | |
36 | ||
37 | /** | |
38 | * @var User | |
39 | * | |
40 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="notifications") | |
41 | */ | |
42 | protected $user; | |
43 | ||
44 | /** | |
45 | * @var \DateTime | |
46 | * | |
47 | * @ORM\Column(name="timestamp", type="datetime") | |
48 | */ | |
49 | protected $timestamp; | |
50 | ||
51 | /** | |
52 | * @var string | |
53 | * | |
54 | * @ORM\Column(name="title", type="string") | |
55 | */ | |
56 | protected $title; | |
57 | ||
58 | /** | |
59 | * @var string | |
60 | * | |
61 | * @ORM\Column(name="description", type="string", nullable=true) | |
62 | */ | |
63 | protected $description; | |
64 | ||
65 | /** | |
66 | * @var bool | |
67 | * | |
68 | * @ORM\Column(name="read", type="boolean") | |
69 | */ | |
70 | protected $read; | |
71 | ||
72 | /** | |
73 | * @var array | |
74 | * | |
75 | * @ORM\Column(name="parameters", type="array", nullable=true) | |
76 | */ | |
77 | protected $parameters; | |
78 | ||
79 | protected $logger; | |
80 | ||
81 | /** | |
82 | * @var ArrayCollection<ActionInterface> | |
83 | * | |
84 | * @ORM\Column(name="actions", type="array", nullable=true) | |
85 | */ | |
86 | protected $actions; | |
87 | ||
88 | protected $actionTypes = []; | |
89 | ||
90 | const TYPE_ADMIN = 0; | |
91 | const TYPE_USER = 1; | |
92 | const TYPE_RELEASE = 2; | |
93 | ||
94 | public function __construct(User $user = null) | |
95 | { | |
96 | $this->logger = new NullLogger(); | |
97 | $this->timestamp = new \DateTime(); | |
98 | $this->actions = new ArrayCollection(); | |
99 | $this->parameters = []; | |
100 | $this->read = false; | |
101 | $this->user = $user; | |
102 | } | |
103 | ||
104 | /** | |
105 | * @param LoggerInterface $logger | |
106 | * | |
107 | * @return NotificationInterface | |
108 | */ | |
109 | public function setLogger(LoggerInterface $logger) | |
110 | { | |
111 | $this->logger = $logger; | |
112 | ||
113 | return $this; | |
114 | } | |
115 | ||
116 | /** | |
117 | * @return mixed | |
118 | */ | |
119 | public function getId() | |
120 | { | |
121 | return $this->id; | |
122 | } | |
123 | ||
124 | /** | |
125 | * @return mixed | |
126 | */ | |
127 | public function getType() | |
128 | { | |
129 | return $this->type; | |
130 | } | |
131 | ||
132 | /** | |
133 | * @param mixed $type | |
134 | * | |
135 | * @return NotificationInterface | |
136 | */ | |
137 | public function setType($type) | |
138 | { | |
139 | $this->type = $type; | |
140 | ||
141 | return $this; | |
142 | } | |
143 | ||
144 | /** | |
145 | * @return User | |
146 | */ | |
147 | public function getUser() | |
148 | { | |
149 | return $this->user; | |
150 | } | |
151 | ||
152 | /** | |
153 | * @param User $user | |
154 | * | |
155 | * @return NotificationInterface | |
156 | */ | |
157 | public function setUser(User $user) | |
158 | { | |
159 | $this->user = $user; | |
160 | ||
161 | return $this; | |
162 | } | |
163 | ||
164 | /** | |
165 | * @return \DateTime | |
166 | */ | |
167 | public function getTimestamp() | |
168 | { | |
169 | return $this->timestamp; | |
170 | } | |
171 | ||
172 | /** | |
173 | * @param \DateTime $timestamp | |
174 | * | |
175 | * @return NotificationInterface | |
176 | */ | |
177 | public function setTimestamp(\DateTime $timestamp) | |
178 | { | |
179 | $this->timestamp = $timestamp; | |
180 | ||
181 | return $this; | |
182 | } | |
183 | ||
184 | /** | |
185 | * @return string | |
186 | */ | |
187 | public function getTitle() | |
188 | { | |
189 | return $this->title; | |
190 | } | |
191 | ||
192 | /** | |
193 | * @param string $title | |
194 | * | |
195 | * @return NotificationInterface | |
196 | */ | |
197 | public function setTitle($title) | |
198 | { | |
199 | $this->title = $title; | |
200 | ||
201 | return $this; | |
202 | } | |
203 | ||
204 | /** | |
205 | * @return bool | |
206 | */ | |
207 | public function isRead() | |
208 | { | |
209 | return $this->read; | |
210 | } | |
211 | ||
212 | /** | |
213 | * @param bool $read | |
214 | * | |
215 | * @return NotificationInterface | |
216 | */ | |
217 | public function setRead($read) | |
218 | { | |
219 | $this->read = $read; | |
220 | ||
221 | return $this; | |
222 | } | |
223 | ||
224 | /** | |
225 | * @param ActionInterface $action | |
226 | * | |
227 | * @return NotificationInterface | |
228 | * | |
229 | * @throws \InvalidArgumentException | |
230 | */ | |
231 | public function addAction(ActionInterface $action) | |
232 | { | |
233 | if (isset($this->actionTypes[$action->getType()])) { | |
234 | throw new \InvalidArgumentException('The notification already has a primary action'); | |
235 | } | |
236 | $this->actionTypes[$action->getType()] = true; | |
237 | $this->actions->add($action); | |
238 | ||
239 | return $this; | |
240 | } | |
241 | ||
242 | /** | |
243 | * @return ArrayCollection<ActionInterface> | |
244 | */ | |
245 | public function getActions() | |
246 | { | |
247 | return $this->actions; | |
248 | } | |
249 | ||
250 | /** | |
251 | * @return string | |
252 | */ | |
253 | public function getDescription() | |
254 | { | |
255 | return $this->description; | |
256 | } | |
257 | ||
258 | /** | |
259 | * @param string $description | |
260 | * | |
261 | * @return Notification | |
262 | */ | |
263 | public function setDescription($description) | |
264 | { | |
265 | $this->description = $description; | |
266 | ||
267 | return $this; | |
268 | } | |
269 | ||
270 | /** | |
271 | * @return array | |
272 | */ | |
273 | public function getParameters() | |
274 | { | |
275 | return $this->parameters; | |
276 | } | |
277 | ||
278 | /** | |
279 | * @param array $parameters | |
280 | * | |
281 | * @return Notification | |
282 | */ | |
283 | public function setParameters($parameters) | |
284 | { | |
285 | $this->parameters = $parameters; | |
286 | ||
287 | return $this; | |
288 | } | |
289 | ||
290 | /** | |
291 | * @param string $key | |
292 | * @param string $value | |
293 | * | |
294 | * @return Notification | |
295 | * | |
296 | * @throws \InvalidArgumentException | |
297 | */ | |
298 | public function addParameter($key, $value) | |
299 | { | |
300 | if (in_array($key, $this->parameters, true)) { | |
301 | throw new \InvalidArgumentException('This parameter already is set'); | |
302 | } | |
303 | ||
304 | $this->parameters[$key] = $value; | |
305 | ||
306 | return $this; | |
307 | } | |
308 | } |