Completed
Push — master ( cecc77...9f2f81 )
by
unknown
02:31
created

NotificationManager::getPaginatedIfNeeded()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 12
rs 9.2
cc 4
eloc 7
nc 3
nop 3
1
<?php
2
3
namespace Fenos\Notifynder\Notifications;
4
5
use Closure;
6
use Fenos\Notifynder\Contracts\NotificationDB;
7
use Fenos\Notifynder\Contracts\NotifynderNotification;
8
use Fenos\Notifynder\Exceptions\NotificationNotFoundException;
9
use Fenos\Notifynder\Models\Notification as NotificationModel;
10
use Fenos\Notifynder\Models\NotifynderCollection;
11
use Illuminate\Pagination\LengthAwarePaginator;
12
13
/**
14
 * Class NotifynderNotification.
15
 *
16
 * The notification manager is responsible to manage the CRUD operations
17
 * of the notifications.
18
 */
19
class NotificationManager implements NotifynderNotification
20
{
21
    /**
22
     * @var NotificationDB
23
     */
24
    protected $notifynderRepo;
25
26
    /**
27
     * @var string | null
28
     */
29
    protected $entity;
30
31
    /**
32
     * @param NotificationDB $notifynderRepo
33
     */
34
    public function __construct(NotificationDB $notifynderRepo)
35
    {
36
        $this->notifynderRepo = $notifynderRepo;
37
    }
38
39
    /**
40
     * Set the entity for polymorphic.
41
     *
42
     * @param $name
43
     * @return $this
44
     */
45
    public function entity($name)
46
    {
47
        $this->entity = $name;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Find a notification by ID.
54
     *
55
     * @param $notificationId
56
     * @return NotificationModel|\Illuminate\Database\Eloquent\Model|static
57
     * @throws \Fenos\Notifynder\Exceptions\NotificationNotFoundException
58
     */
59
    public function find($notificationId)
60
    {
61
        $notification = $this->notifynderRepo->find($notificationId);
62
63
        if (is_null($notification)) {
64
            $error = 'Notification Not found';
65
            throw new NotificationNotFoundException($error);
66
        }
67
68
        return $notification;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $notification; (Illuminate\Database\Eloq...ontracts\NotificationDB) is incompatible with the return type declared by the interface Fenos\Notifynder\Contrac...ynderNotification::find of type Illuminate\Database\Eloq...\NotifynderNotification.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
    }
70
71
    /**
72
     * Make read one notification giving
73
     * the ID of it.
74
     *
75
     * @param $notificationId
76
     * @return bool|\Fenos\Notifynder\Models\Notification
77
     */
78
    public function readOne($notificationId)
79
    {
80
        $notification = $this->find($notificationId);
81
82
        return $this->notifynderRepo->readOne($notification);
0 ignored issues
show
Documentation introduced by
$notification is of type object<Illuminate\Databa...ntracts\NotificationDB>, but the function expects a object<Fenos\Notifynder\Models\Notification>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
    }
84
85
    /**
86
     * Read notifications in base the number
87
     * Given.
88
     *
89
     * @param         $toId
90
     * @param         $numbers
91
     * @param  string $order
92
     * @return mixed
93
     */
94
    public function readLimit($toId, $numbers, $order = 'ASC')
95
    {
96
        return $this->notifynderRepo->readLimit($toId, $this->entity, $numbers, $order);
97
    }
98
99
    /**
100
     * Read all notification of the
101
     * given entity.
102
     *
103
     * @param $toId
104
     * @return Number
105
     */
106
    public function readAll($toId)
107
    {
108
        return $this->notifynderRepo->readAll($toId, $this->entity);
109
    }
110
111
    /**
112
     * Delete a notification giving the id
113
     * of it.
114
     *
115
     * @param $notificationId
116
     * @return bool
117
     */
118
    public function delete($notificationId)
119
    {
120
        return $this->notifynderRepo->delete($notificationId);
121
    }
122
123
    /**
124
     * Delete numbers of notifications equals
125
     * to the number passing as 2 parameter of
126
     * the current user.
127
     *
128
     * @param $entityId
129
     * @param $number
130
     * @param $order
131
     * @return mixed
132
     */
133
    public function deleteLimit($entityId, $number, $order = 'asc')
134
    {
135
        return $this->notifynderRepo->deleteLimit($entityId, $this->entity, $number, $order);
136
    }
137
138
    /**
139
     * Delete all notification of a given
140
     * Entity.
141
     *
142
     * @param $entityId
143
     * @return bool
144
     */
145
    public function deleteAll($entityId)
146
    {
147
        return $this->notifynderRepo->deleteAll($entityId, $this->entity);
148
    }
149
150
    /**
151
     * Delete All notifications from a
152
     * defined category.
153
     *
154
     * @param $categoryName string
155
     * @param $expired Bool
156
     * @return bool
157
     */
158
    public function deleteByCategory($categoryName, $expired = false)
159
    {
160
        return $this->notifynderRepo->deleteByCategory($categoryName, $expired);
161
    }
162
163
    /**
164
     * Get notifications not read
165
     * of the entity given.
166
     *
167
     * @param           $toId
168
     * @param           $limit
169
     * @param  int|null $paginate
170
     * @param  string   $orderDate
171
     * @param Closure   $filterScope
172
     * @return mixed
173
     */
174 View Code Duplication
    public function getNotRead($toId, $limit = null, $paginate = null, $orderDate = 'desc', Closure $filterScope = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $notifications = $this->notifynderRepo->getNotRead(
177
            $toId, $this->entity,
178
            $limit, null, $orderDate,
179
            $filterScope
180
        );
181
182
        return $this->getPaginatedIfNeeded($notifications, $limit, $paginate);
183
    }
184
185
    /**
186
     * Get All notifications.
187
     *
188
     * @param           $toId
189
     * @param           $limit
190
     * @param  int|null $paginate
191
     * @param  string   $orderDate
192
     * @param Closure   $filterScope
193
     * @return mixed
194
     */
195 View Code Duplication
    public function getAll($toId, $limit = null, $paginate = null, $orderDate = 'desc', Closure $filterScope = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        $notifications = $this->notifynderRepo->getAll(
198
            $toId, $this->entity,
199
            $limit, null, $orderDate,
200
            $filterScope
201
        );
202
203
        return $this->getPaginatedIfNeeded($notifications, $limit, $paginate);
204
    }
205
206
    protected function getPaginatedIfNeeded(NotifynderCollection $notifications, $limit, $paginate)
207
    {
208
        if ($paginate === false || is_null($paginate)) {
209
            return $notifications->parse();
210
        } elseif ($paginate === true) {
211
            $paginate = null;
212
        }
213
214
        return new LengthAwarePaginator($notifications->parse(), $notifications->count(), $limit, $paginate, [
215
            'path' => LengthAwarePaginator::resolveCurrentPath(),
216
        ]);
217
    }
218
219
    /**
220
     * Get last notification of the
221
     * given entity.
222
     *
223
     * @param         $toId
224
     * @param Closure $filterScope
225
     * @return mixed
226
     */
227
    public function getLastNotification($toId, Closure $filterScope = null)
228
    {
229
        return $this->notifynderRepo->getLastNotification($toId, $this->entity, $filterScope);
230
    }
231
232
    /**
233
     * Get last notification of the
234
     * given entity of the specific category.
235
     *
236
     * @param         $category
237
     * @param         $toId
238
     * @param Closure $filterScope
239
     * @return mixed
240
     */
241
    public function getLastNotificationByCategory($category, $toId, Closure $filterScope = null)
242
    {
243
        return $this->notifynderRepo->getLastNotificationByCategory($category, $toId, $this->entity, $filterScope);
244
    }
245
246
    /**
247
     * Send single notification.
248
     *
249
     * @param  array  $info
250
     * @return static
251
     */
252
    public function sendOne(array $info)
253
    {
254
        return $this->notifynderRepo->storeSingle($info);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->notifynderRepo->storeSingle($info); (Fenos\Notifynder\Models\Notification) is incompatible with the return type declared by the interface Fenos\Notifynder\Contrac...erNotification::sendOne of type Fenos\Notifynder\Contracts\NotifynderNotification.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
255
    }
256
257
    /**
258
     * Send multiple notifications.
259
     *
260
     * @param  array $info
261
     * @return mixed
262
     */
263
    public function sendMultiple(array $info)
264
    {
265
        return $this->notifynderRepo->storeMultiple($info);
266
    }
267
268
    /**
269
     * Get number of notification
270
     * not read.
271
     *
272
     * @param         $toId
273
     * @param Closure $filterScope
274
     * @return mixed
275
     */
276
    public function countNotRead($toId, Closure $filterScope = null)
277
    {
278
        return $this->notifynderRepo->countNotRead($toId, $this->entity, $filterScope);
279
    }
280
}
281