Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/Notifynder/Notifications/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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(),
]);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/Notifications/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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);
}

/**
Expand Down