| Conditions | 3 |
| Paths | 2 |
| Total Lines | 47 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | public function notifications() |
||
| 15 | { |
||
| 16 | $this->loadModel('Notifications'); |
||
| 17 | |||
| 18 | $notifications = $this->Notifications |
||
|
|
|||
| 19 | ->find() |
||
| 20 | ->where([ |
||
| 21 | 'user_id' => $this->request->session()->read('Auth.User.id') |
||
| 22 | ]) |
||
| 23 | ->order([ |
||
| 24 | 'is_read' => 'ASC', |
||
| 25 | 'created' => 'DESC' |
||
| 26 | ]) |
||
| 27 | ->limit(Configure::read('User.max_notifications')) |
||
| 28 | ->find('map', [ |
||
| 29 | 'session' => $this->request->session() |
||
| 30 | ]) |
||
| 31 | ->toArray(); |
||
| 32 | |||
| 33 | $statistics = [ |
||
| 34 | 'read' => 0, |
||
| 35 | 'unread' => 0 |
||
| 36 | ]; |
||
| 37 | |||
| 38 | //A map function to count the read/unread notifications |
||
| 39 | $map = function ($v) { |
||
| 40 | if ($v->is_read == 0) { |
||
| 41 | return 'unread'; |
||
| 42 | } else { |
||
| 43 | return 'read'; |
||
| 44 | } |
||
| 45 | }; |
||
| 46 | |||
| 47 | $statistics = array_merge( |
||
| 48 | $statistics, |
||
| 49 | array_count_values( |
||
| 50 | array_map( |
||
| 51 | $map, |
||
| 52 | $notifications |
||
| 53 | ) |
||
| 54 | ) |
||
| 55 | ); |
||
| 56 | |||
| 57 | $hasNewNotifs = ($statistics['unread'] >= 1) ? true : false; |
||
| 58 | |||
| 59 | $this->set(compact('notifications', 'statistics', 'hasNewNotifs')); |
||
| 60 | } |
||
| 61 | } |
||
| 62 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: