Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Notifier component to limit amount of notifications to return. #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,29 @@ be defined here.
### Lists
Of course you want to get a list of notifications per user. Here are some examples:

// getting a list of all notifications of the current logged in user
// getting a list of all notifications of the current Logged In user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think that change was valid :)

$this->Notifier->getNotifications();

// getting a list of all notifications of the user with id 2
$this->Notifier->getNotifications(2);

// getting a list of all unread notifications
$this->Notifier->allNotificationList(2, true);
// getting a list of all unread notifications for user with id 2
$this->Notifier->getNotifications(2, true);

// getting a list of all read notifications
$this->Notifier->allNotificationList(2, false);
// getting a list of all read notifications for a user with id 2
$this->Notifier->getNotifications(2, false);

// getting a limited list of all notifications for user with id 2
$options = ['limit' => 5];
$this->Notifier->getNotifications(2, null, $options); // Return 5 notifications

// getting a limited list of all unread notifications for Logged In user
$options = ['limit' => 5];
$this->Notifier->getNotifications(null, true, $options); // Return 5 notifications

// getting a limited list of all notifications for logged in User
$options = ['limit' => 5];
$this->Notifier->getNotifications(null, null, $options); // Return 5 notifications

// getting a number of all notifications of the current logged in user
$this->Notifier->countNotifications();
Expand Down
13 changes: 11 additions & 2 deletions src/Controller/Component/NotifierComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @since 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Notifier\Controller\Component;

use Cake\Controller\Component;
Expand Down Expand Up @@ -88,12 +89,17 @@ public function setController($controller)
*
* // get all read notifications
* $this->Notifier->getNotifications(1, false);
*
* // get 4 latest unread notifications
* $options = ['limit' => 4];
* $this->Notifier->getNotifications(1, true, $options);
* ```
* @param int|null $userId Id of the user.
* @param bool|null $state The state of notifications: `true` for unread, `false` for read, `null` for all.
* @param array|empty $options Currently supports 'limit', which limits returned notifications
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|empty should be removed. its invalid doc block type markup.

* @return array
*/
public function getNotifications($userId = null, $state = null)
public function getNotifications($userId = null, $state = null, $options = [])
{
if (!$userId) {
$userId = $this->Controller->Auth->user('id');
Expand All @@ -106,6 +112,10 @@ public function getNotifications($userId = null, $state = null)
if (!is_null($state)) {
$query->where(['Notifications.state' => $state]);
}

if (!empty($options['limit'])) {
$query->limit($options['limit']);
}

return $query->toArray();
}
Expand Down Expand Up @@ -179,7 +189,6 @@ public function markAsRead($notificationId = null, $user = null)
$query = $model->find('all')->where([
'user_id' => $user,
'id' => $notificationId

]);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Controller/Component/NotifierComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public function testGetNotifications()
$this->assertEquals(2, count($this->Notifier->getNotifications(2)));
$this->assertEquals(0, count($this->Notifier->getNotifications(2, true)));
$this->assertEquals(2, count($this->Notifier->getNotifications(2, false)));
$this->assertEquals(1, count($this->Notifier->getNotifications(1, true, ['limit' => 1])));
$this->assertEquals(2, count($this->Notifier->getNotifications(2, false, ['limit' => 3])));
}

public function testMarkAsReadWithAuth()
Expand Down