Skip to content

Commit

Permalink
Merge pull request #238 from dennisdegreef/addNotificationsApi
Browse files Browse the repository at this point in the history
Implemented notifications from github API
  • Loading branch information
pilot committed Feb 18, 2015
2 parents 62d3824 + 8246468 commit fd6a5f9
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
38 changes: 38 additions & 0 deletions doc/notification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Notification API
[Back to the navigation](index.md)

Listing notifications and marking them as read.
Wraps [GitHub Notification API](https://developer.github.com/v3/activity/notifications/).

### List notifications

```php
$issues = $client->api('notification')->all();
```

Returns an array of unread notifications.

### Include already read notifications, including participating, or since a certain date

```php
$includingRead = true;
$participating = true;
$since = new DateTime('1970/01/01');
$issues = $client->api('notification')->all($includingRead, $participating, $since);
```

Returns an array of all notifications

### Mark notifications as read

```php
$client->api('notification')->markRead();
```

or up until a certain date

```php
$client->api('notification')->markRead(new DateTime('2015/01/01'));
```

Marks all notifications as read up until the current date, unless a date is given
60 changes: 60 additions & 0 deletions lib/Github/Api/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Github\Api;

use DateTime;

/**
* API for accessing Notifications from your Git/Github repositories.
*
* Important! You have to be authenticated to perform these methods
*
* @link https://developer.github.com/v3/activity/notifications/
* @author Dennis de Greef <[email protected]>
*/
class Notification extends AbstractApi
{
/**
* Get a listing of notifications
*
* @link https://developer.github.com/v3/activity/notifications/
*
* @param bool $includingRead
* @param bool $participating
* @param DateTime|null $since
*
* @return array array of notifications
*/
public function all($includingRead = false, $participating = false, DateTime $since = null)
{
$parameters = array(
'all' => $includingRead,
'participating' => $participating
);

if($since !== null) {
$parameters['since'] = $since->format(DateTime::ISO8601);
}

return $this->get('notifications', $parameters);
}

/**
* Marks all notifications as read from the current date
* Optionally give DateTime to mark as read before that date
*
* @link https://developer.github.com/v3/activity/notifications/#mark-as-read
*
* @param DateTime|null $since
*/
public function markRead(DateTime $since = null)
{
$parameters = array();

if($since !== null) {
$parameters['last_read_at'] = $since->format(DateTime::ISO8601);
}

$this->put('notifications', $parameters);
}
}
7 changes: 7 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @method Api\Issue issue()
* @method Api\Issue issues()
* @method Api\Markdown markdown()
* @method Api\Notification notification()
* @method Api\Notification notifications()
* @method Api\Organization organization()
* @method Api\Organization organizations()
* @method Api\PullRequest pr()
Expand Down Expand Up @@ -143,6 +145,11 @@ public function api($name)
$api = new Api\Markdown($this);
break;

case 'notification':
case 'notifications':
$api = new Api\Notification($this);
break;

case 'organization':
case 'organizations':
$api = new Api\Organization($this);
Expand Down
104 changes: 104 additions & 0 deletions test/Github/Tests/Api/NotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Github\Tests\Api;

use DateTime;

class NotificationTest extends TestCase
{
/**
* @test
*/
public function shouldGetNotifications()
{
$parameters = array(
'all' => false,
'participating' => false,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('notifications', $parameters);

$api->all();
}

/**
* @test
*/
public function shouldGetNotificationsSince()
{
$since = new DateTime('now');

$parameters = array(
'all' => false,
'participating' => false,
'since' => $since->format(DateTime::ISO8601),
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('notifications', $parameters);

$api->all(false, false, $since);
}

/**
* @test
*/
public function shouldGetNotificationsIncludingAndParticipating()
{
$parameters = array(
'all' => true,
'participating' => true,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('notifications', $parameters);

$api->all(true, true);
}

/**
* @test
*/
public function shouldMarkNotificationsAsRead()
{
$parameters = array();

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('notifications', $parameters);

$api->markRead();
}

/**
* @test
*/
public function shouldMarkNotificationsAsReadForGivenDate()
{
$since = new DateTime('now');

$parameters = array(
'last_read_at' => $since->format(DateTime::ISO8601),
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('notifications', $parameters);

$api->markRead($since);
}

protected function getApiClass()
{
return 'Github\Api\Notification';
}
}

0 comments on commit fd6a5f9

Please sign in to comment.