Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace Fenos\Notifynder\Notifications; |
||
16 | class NotificationRepository implements NotificationDB |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var Notification | Builder | BuilderDB |
||
21 | */ |
||
22 | protected $notification; |
||
23 | |||
24 | /** |
||
25 | * @var $db DatabaseManager | Connection |
||
26 | */ |
||
27 | protected $db; |
||
28 | |||
29 | /** |
||
30 | * @param Notification $notification |
||
31 | * @param \Illuminate\Database\DatabaseManager $db |
||
32 | */ |
||
33 | public function __construct( |
||
34 | Notification $notification, |
||
35 | DatabaseManager $db |
||
36 | ) { |
||
37 | $this->notification = $notification; |
||
38 | $this->db = $db; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Find notification by id |
||
43 | * |
||
44 | * @param $notification_id |
||
45 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static |
||
46 | */ |
||
47 | public function find($notification_id) |
||
51 | |||
52 | /** |
||
53 | * Save a single notification sent |
||
54 | * |
||
55 | * @param array $info |
||
56 | * @return Notification |
||
57 | */ |
||
58 | public function storeSingle(array $info) |
||
62 | |||
63 | /** |
||
64 | * Save multiple notifications sent |
||
65 | * at once |
||
66 | * |
||
67 | * @param array $info |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function storeMultiple(array $info) |
||
71 | { |
||
72 | return $this->db->table( |
||
73 | $this->notification->getTable() |
||
74 | )->insert($info); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Make Read One Notification |
||
79 | * |
||
80 | * @param Notification $notification |
||
81 | * @return bool|Notification |
||
82 | */ |
||
83 | public function readOne(Notification $notification) |
||
84 | { |
||
85 | $notification->read = 1; |
||
86 | |||
87 | if ($notification->save()) { |
||
88 | return $notification; |
||
89 | } |
||
90 | |||
91 | return false; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Read notifications in base the number |
||
96 | * Given |
||
97 | * |
||
98 | * @param $to_id |
||
99 | * @param $entity |
||
100 | * @param $numbers |
||
101 | * @param $order |
||
102 | * @return int |
||
103 | */ |
||
104 | public function readLimit($to_id, $entity, $numbers, $order) |
||
105 | { |
||
106 | $notifications = $this->notification->withNotRead() |
||
107 | ->wherePolymorphic($to_id, $entity) |
||
108 | ->limit($numbers) |
||
109 | ->orderBy('id', $order) |
||
110 | ->lists('id'); |
||
111 | |||
112 | return $this->notification->whereIn('id', $notifications) |
||
113 | ->update(['read' => 1]); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Make read all notification not read |
||
118 | * |
||
119 | * @param $to_id |
||
120 | * @param $entity |
||
121 | * @return int |
||
122 | */ |
||
123 | public function readAll($to_id, $entity) |
||
124 | { |
||
125 | return $this->notification->withNotRead() |
||
126 | ->wherePolymorphic($to_id, $entity) |
||
127 | ->update(['read' => 1]); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Delete a notification giving the id |
||
132 | * of it |
||
133 | * |
||
134 | * @param $notification_id |
||
135 | * @return Bool |
||
136 | */ |
||
137 | public function delete($notification_id) |
||
141 | |||
142 | /** |
||
143 | * Delete All notifications about the |
||
144 | * current user |
||
145 | * |
||
146 | * @param $to_id int |
||
147 | * @param $entity |
||
148 | * @return Bool |
||
149 | */ |
||
150 | public function deleteAll($to_id, $entity) |
||
159 | |||
160 | /** |
||
161 | * Delete All notifications from a |
||
162 | * defined category |
||
163 | * |
||
164 | * @param $category_name int |
||
165 | * @param $expired Bool |
||
166 | * @return Bool |
||
167 | */ |
||
168 | public function deleteByCategory($category_name, $expired = false) |
||
183 | |||
184 | /** |
||
185 | * |
||
186 | * Delete numbers of notifications equals |
||
187 | * to the number passing as 2 parameter of |
||
188 | * the current user |
||
189 | * |
||
190 | * @param $user_id int |
||
191 | * @param $entity |
||
192 | * @param $number int |
||
193 | * @param $order string |
||
194 | * @return int |
||
195 | * @throws \Exception |
||
196 | */ |
||
197 | public function deleteLimit($user_id, $entity, $number, $order) |
||
213 | |||
214 | /** |
||
215 | * Retrive notifications not Read |
||
216 | * You can also limit the number of |
||
217 | * Notification if you don't it will get all |
||
218 | * |
||
219 | * @param $to_id |
||
220 | * @param $entity |
||
221 | * @param int|null $limit |
||
222 | * @param int|null $paginate |
||
223 | * @param string $orderDate |
||
224 | * @param Closure|null $filterScope |
||
225 | * @return mixed |
||
226 | */ |
||
227 | View Code Duplication | public function getNotRead( |
|
249 | |||
250 | /** |
||
251 | * Retrive all notifications, not read |
||
252 | * in first. |
||
253 | * You can also limit the number of |
||
254 | * Notifications if you don't, it will get all |
||
255 | * |
||
256 | * @param $to_id |
||
257 | * @param $entity |
||
258 | * @param null $limit |
||
259 | * @param int|null $paginate |
||
260 | * @param string $orderDate |
||
261 | * @param Closure $filterScope |
||
262 | * @return mixed |
||
263 | */ |
||
264 | View Code Duplication | public function getAll( |
|
285 | |||
286 | /** |
||
287 | * get number Notifications |
||
288 | * not read |
||
289 | * |
||
290 | * @param $to_id |
||
291 | * @param $entity |
||
292 | * @param Closure $filterScope |
||
293 | * @return mixed |
||
294 | */ |
||
295 | View Code Duplication | public function countNotRead($to_id, $entity, Closure $filterScope = null) |
|
305 | |||
306 | /** |
||
307 | * Get last notification of the current |
||
308 | * entity |
||
309 | * |
||
310 | * @param $to_id |
||
311 | * @param $entity |
||
312 | * @param Closure $filterScope |
||
313 | * @return mixed |
||
314 | */ |
||
315 | View Code Duplication | public function getLastNotification($to_id, $entity, Closure $filterScope = null) |
|
324 | |||
325 | /** |
||
326 | * Get last notification of the current |
||
327 | * entity of a specific category |
||
328 | * |
||
329 | * @param $category |
||
330 | * @param $to_id |
||
331 | * @param $entity |
||
332 | * @param Closure $filterScope |
||
333 | * @return mixed |
||
334 | */ |
||
335 | View Code Duplication | public function getLastNotificationByCategory($category, $to_id, $entity, Closure $filterScope = null) |
|
346 | |||
347 | /** |
||
348 | * Apply scope filters |
||
349 | * |
||
350 | * @param Closure $filterScope |
||
351 | * @param $query |
||
352 | */ |
||
353 | protected function applyFilter(Closure $filterScope = null, $query) |
||
363 | } |
||
364 |