]>
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; | |
bf6c0346 | 94 | const TYPE_SHARE = 3; |
e0f9010e TC |
95 | |
96 | public function __construct(User $user = null) | |
97 | { | |
98 | $this->logger = new NullLogger(); | |
99 | $this->timestamp = new \DateTime(); | |
100 | $this->actions = new ArrayCollection(); | |
101 | $this->parameters = []; | |
102 | $this->read = false; | |
103 | $this->user = $user; | |
104 | } | |
105 | ||
106 | /** | |
107 | * @param LoggerInterface $logger | |
108 | * | |
109 | * @return NotificationInterface | |
110 | */ | |
111 | public function setLogger(LoggerInterface $logger) | |
112 | { | |
113 | $this->logger = $logger; | |
114 | ||
115 | return $this; | |
116 | } | |
117 | ||
118 | /** | |
119 | * @return mixed | |
120 | */ | |
121 | public function getId() | |
122 | { | |
123 | return $this->id; | |
124 | } | |
125 | ||
126 | /** | |
127 | * @return mixed | |
128 | */ | |
129 | public function getType() | |
130 | { | |
131 | return $this->type; | |
132 | } | |
133 | ||
134 | /** | |
135 | * @param mixed $type | |
136 | * | |
137 | * @return NotificationInterface | |
138 | */ | |
139 | public function setType($type) | |
140 | { | |
141 | $this->type = $type; | |
142 | ||
143 | return $this; | |
144 | } | |
145 | ||
146 | /** | |
147 | * @return User | |
148 | */ | |
149 | public function getUser() | |
150 | { | |
151 | return $this->user; | |
152 | } | |
153 | ||
154 | /** | |
155 | * @param User $user | |
156 | * | |
157 | * @return NotificationInterface | |
158 | */ | |
159 | public function setUser(User $user) | |
160 | { | |
161 | $this->user = $user; | |
162 | ||
163 | return $this; | |
164 | } | |
165 | ||
166 | /** | |
167 | * @return \DateTime | |
168 | */ | |
169 | public function getTimestamp() | |
170 | { | |
171 | return $this->timestamp; | |
172 | } | |
173 | ||
174 | /** | |
175 | * @param \DateTime $timestamp | |
176 | * | |
177 | * @return NotificationInterface | |
178 | */ | |
179 | public function setTimestamp(\DateTime $timestamp) | |
180 | { | |
181 | $this->timestamp = $timestamp; | |
182 | ||
183 | return $this; | |
184 | } | |
185 | ||
186 | /** | |
187 | * @return string | |
188 | */ | |
189 | public function getTitle() | |
190 | { | |
191 | return $this->title; | |
192 | } | |
193 | ||
194 | /** | |
195 | * @param string $title | |
196 | * | |
197 | * @return NotificationInterface | |
198 | */ | |
199 | public function setTitle($title) | |
200 | { | |
201 | $this->title = $title; | |
202 | ||
203 | return $this; | |
204 | } | |
205 | ||
206 | /** | |
207 | * @return bool | |
208 | */ | |
209 | public function isRead() | |
210 | { | |
211 | return $this->read; | |
212 | } | |
213 | ||
214 | /** | |
215 | * @param bool $read | |
216 | * | |
217 | * @return NotificationInterface | |
218 | */ | |
219 | public function setRead($read) | |
220 | { | |
221 | $this->read = $read; | |
222 | ||
223 | return $this; | |
224 | } | |
225 | ||
226 | /** | |
227 | * @param ActionInterface $action | |
228 | * | |
229 | * @return NotificationInterface | |
e0f9010e TC |
230 | * @throws \InvalidArgumentException |
231 | */ | |
232 | public function addAction(ActionInterface $action) | |
233 | { | |
234 | if (isset($this->actionTypes[$action->getType()])) { | |
235 | throw new \InvalidArgumentException('The notification already has a primary action'); | |
236 | } | |
237 | $this->actionTypes[$action->getType()] = true; | |
238 | $this->actions->add($action); | |
e0f9010e TC |
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 | } |