diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Entity/Notification.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Entity/Notification.php | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Entity/Notification.php b/src/Wallabag/CoreBundle/Entity/Notification.php new file mode 100644 index 00000000..e1c1b449 --- /dev/null +++ b/src/Wallabag/CoreBundle/Entity/Notification.php | |||
@@ -0,0 +1,220 @@ | |||
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; | ||
80 | |||
81 | public function __construct(User $user = null) | ||
82 | { | ||
83 | $this->logger = new NullLogger(); | ||
84 | $this->timestamp = new \DateTime(); | ||
85 | $this->actions = new ArrayCollection(); | ||
86 | $this->read = false; | ||
87 | $this->user = $user; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * @param LoggerInterface $logger | ||
92 | * @return NotificationInterface | ||
93 | */ | ||
94 | public function setLogger(LoggerInterface $logger) | ||
95 | { | ||
96 | $this->logger = $logger; | ||
97 | return $this; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * @return mixed | ||
102 | */ | ||
103 | public function getId() | ||
104 | { | ||
105 | return $this->id; | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * @return mixed | ||
110 | */ | ||
111 | public function getType() | ||
112 | { | ||
113 | return $this->type; | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * @param mixed $type | ||
118 | * @return NotificationInterface | ||
119 | */ | ||
120 | public function setType($type) | ||
121 | { | ||
122 | $this->type = $type; | ||
123 | return $this; | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * @return User | ||
128 | */ | ||
129 | public function getUser() | ||
130 | { | ||
131 | return $this->user; | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * @param User $user | ||
136 | * @return NotificationInterface | ||
137 | */ | ||
138 | public function setUser(User $user) | ||
139 | { | ||
140 | $this->user = $user; | ||
141 | return $this; | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * @return \DateTime | ||
146 | */ | ||
147 | public function getTimestamp() | ||
148 | { | ||
149 | return $this->timestamp; | ||
150 | } | ||
151 | |||
152 | /** | ||
153 | * @param \DateTime $timestamp | ||
154 | * @return NotificationInterface | ||
155 | */ | ||
156 | public function setTimestamp(\DateTime $timestamp) | ||
157 | { | ||
158 | $this->timestamp = $timestamp; | ||
159 | return $this; | ||
160 | } | ||
161 | |||
162 | /** | ||
163 | * @return string | ||
164 | */ | ||
165 | public function getTitle() | ||
166 | { | ||
167 | return $this->title; | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * @param string $title | ||
172 | * @return NotificationInterface | ||
173 | */ | ||
174 | public function setTitle($title) | ||
175 | { | ||
176 | $this->title = $title; | ||
177 | return $this; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * @return bool | ||
182 | */ | ||
183 | public function isRead() | ||
184 | { | ||
185 | return $this->read; | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * @param bool $read | ||
190 | * @return NotificationInterface | ||
191 | */ | ||
192 | public function setRead($read) | ||
193 | { | ||
194 | $this->read = $read; | ||
195 | return $this; | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * @param ActionInterface $action | ||
200 | * @return NotificationInterface | ||
201 | * @throws \InvalidArgumentException | ||
202 | */ | ||
203 | public function addAction(ActionInterface $action) | ||
204 | { | ||
205 | if (isset($this->actionTypes[$action->getType()])) { | ||
206 | throw new \InvalidArgumentException('The notification already has a primary action'); | ||
207 | } | ||
208 | $this->actionTypes[$action->getType()] = true; | ||
209 | $this->actions->add($action); | ||
210 | return $this; | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * @return ArrayCollection<ActionInterface> | ||
215 | */ | ||
216 | public function getActions() | ||
217 | { | ||
218 | return $this->actions; | ||
219 | } | ||
220 | } | ||