diff --git a/src/Notifynder/Notifications/NotificationManager.php b/src/Notifynder/Notifications/NotificationManager.php index 44a9f87..703a877 100755 --- a/src/Notifynder/Notifications/NotificationManager.php +++ b/src/Notifynder/Notifications/NotificationManager.php @@ -7,6 +7,7 @@ use Fenos\Notifynder\Contracts\NotifynderNotification; use Fenos\Notifynder\Exceptions\NotificationNotFoundException; use Fenos\Notifynder\Models\Notification as NotificationModel; +use Fenos\Notifynder\Models\NotifynderCollection; use Illuminate\Pagination\LengthAwarePaginator; /** @@ -174,17 +175,11 @@ public function getNotRead($toId, $limit = null, $paginate = null, $orderDate = { $notifications = $this->notifynderRepo->getNotRead( $toId, $this->entity, - $limit, $paginate, $orderDate, + $limit, null, $orderDate, $filterScope ); - if (is_int(intval($paginate)) && $paginate) { - return new LengthAwarePaginator($notifications->parse(), $notifications->total(), $limit, $paginate, [ - 'path' => LengthAwarePaginator::resolveCurrentPath(), - ]); - } - - return $notifications->parse(); + return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); } /** @@ -201,17 +196,24 @@ public function getAll($toId, $limit = null, $paginate = null, $orderDate = 'des { $notifications = $this->notifynderRepo->getAll( $toId, $this->entity, - $limit, $paginate, $orderDate, + $limit, null, $orderDate, $filterScope ); - if (is_int(intval($paginate)) && $paginate) { - return new LengthAwarePaginator($notifications->parse(), $notifications->total(), $limit, $paginate, [ - 'path' => LengthAwarePaginator::resolveCurrentPath(), - ]); + return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); + } + + protected function getPaginatedIfNeeded(NotifynderCollection $notifications, $limit, $paginate) + { + if ($paginate === false || is_null($paginate)) { + return $notifications->parse(); + } elseif ($paginate === true) { + $paginate = null; } - return $notifications->parse(); + return new LengthAwarePaginator($notifications->parse(), $notifications->count(), $limit, $paginate, [ + 'path' => LengthAwarePaginator::resolveCurrentPath(), + ]); } /** diff --git a/tests/integration/Notifications/NotificationTest.php b/tests/integration/Notifications/NotificationTest.php index c91575c..94a6006 100755 --- a/tests/integration/Notifications/NotificationTest.php +++ b/tests/integration/Notifications/NotificationTest.php @@ -67,6 +67,40 @@ public function it_retrieve_notification_by_limiting_the_number() $this->assertCount(1, $notifications); } + /** @test */ + public function it_retrieve_notification_by_disable_pagination() + { + app('config')->set('notifynder.polymorphic', false); + $extraValues = json_encode(['look' => 'Amazing']); + + $category = $this->createCategory(['text' => 'parse this {extra.look} value']); + + $notification = $this->createNotification(['extra' => $extraValues, 'category_id' => $category->id]); + $this->createMultipleNotifications(['to_id' => $notification->to_id]); + + $notifications = $this->notification->getNotRead($notification->to->id, 10, false); + + $this->assertCount(10, $notifications); + $this->assertInstanceOf(\Fenos\Notifynder\Models\NotifynderCollection::class, $notifications); + } + + /** @test */ + public function it_retrieve_notification_by_paginating_with_bool() + { + app('config')->set('notifynder.polymorphic', false); + $extraValues = json_encode(['look' => 'Amazing']); + + $category = $this->createCategory(['text' => 'parse this {extra.look} value']); + + $notification = $this->createNotification(['extra' => $extraValues, 'category_id' => $category->id]); + $this->createMultipleNotifications(['to_id' => $notification->to_id]); + + $notifications = $this->notification->getNotRead($notification->to->id, 5, true); + + $this->assertCount(5, $notifications); + $this->assertInstanceOf(\Illuminate\Contracts\Pagination\LengthAwarePaginator::class, $notifications); + } + /** @test */ public function it_retrieve_notification_by_paginating_the_number() { @@ -81,6 +115,7 @@ public function it_retrieve_notification_by_paginating_the_number() $notifications = $this->notification->getNotRead($notification->to->id, 5, 1); $this->assertCount(5, $notifications); + $this->assertInstanceOf(\Illuminate\Contracts\Pagination\LengthAwarePaginator::class, $notifications); } /**