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( |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Retrive all notifications, not read |
||
| 256 | * in first. |
||
| 257 | * You can also limit the number of |
||
| 258 | * Notifications if you don't, it will get all |
||
| 259 | * |
||
| 260 | * @param $to_id |
||
| 261 | * @param $entity |
||
| 262 | * @param null $limit |
||
| 263 | * @param int|null $paginate |
||
| 264 | * @param string $orderDate |
||
| 265 | * @param Closure $filterScope |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function getAll( |
|
| 293 | |||
| 294 | /** |
||
| 295 | * get number Notifications |
||
| 296 | * not read |
||
| 297 | * |
||
| 298 | * @param $to_id |
||
| 299 | * @param $entity |
||
| 300 | * @param Closure $filterScope |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | View Code Duplication | public function countNotRead($to_id, $entity, Closure $filterScope = null) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get last notification of the current |
||
| 316 | * entity |
||
| 317 | * |
||
| 318 | * @param $to_id |
||
| 319 | * @param $entity |
||
| 320 | * @param Closure $filterScope |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function getLastNotification($to_id, $entity, Closure $filterScope = null) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get last notification of the current |
||
| 335 | * entity of a specific category |
||
| 336 | * |
||
| 337 | * @param $category |
||
| 338 | * @param $to_id |
||
| 339 | * @param $entity |
||
| 340 | * @param Closure $filterScope |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function getLastNotificationByCategory($category, $to_id, $entity, Closure $filterScope = null) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Apply scope filters |
||
| 357 | * |
||
| 358 | * @param Closure $filterScope |
||
| 359 | * @param $query |
||
| 360 | */ |
||
| 361 | protected function applyFilter(Closure $filterScope = null, $query) |
||
| 371 | } |
||
| 372 |