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