Skip to content

Commit fd6a5f9

Browse files
committed
Merge pull request #238 from dennisdegreef/addNotificationsApi
Implemented notifications from github API
2 parents 62d3824 + 8246468 commit fd6a5f9

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

doc/notification.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Notification API
2+
[Back to the navigation](index.md)
3+
4+
Listing notifications and marking them as read.
5+
Wraps [GitHub Notification API](https://developer.github.com/v3/activity/notifications/).
6+
7+
### List notifications
8+
9+
```php
10+
$issues = $client->api('notification')->all();
11+
```
12+
13+
Returns an array of unread notifications.
14+
15+
### Include already read notifications, including participating, or since a certain date
16+
17+
```php
18+
$includingRead = true;
19+
$participating = true;
20+
$since = new DateTime('1970/01/01');
21+
$issues = $client->api('notification')->all($includingRead, $participating, $since);
22+
```
23+
24+
Returns an array of all notifications
25+
26+
### Mark notifications as read
27+
28+
```php
29+
$client->api('notification')->markRead();
30+
```
31+
32+
or up until a certain date
33+
34+
```php
35+
$client->api('notification')->markRead(new DateTime('2015/01/01'));
36+
```
37+
38+
Marks all notifications as read up until the current date, unless a date is given

lib/Github/Api/Notification.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
use DateTime;
6+
7+
/**
8+
* API for accessing Notifications from your Git/Github repositories.
9+
*
10+
* Important! You have to be authenticated to perform these methods
11+
*
12+
* @link https://developer.github.com/v3/activity/notifications/
13+
* @author Dennis de Greef <github@link0.net>
14+
*/
15+
class Notification extends AbstractApi
16+
{
17+
/**
18+
* Get a listing of notifications
19+
*
20+
* @link https://developer.github.com/v3/activity/notifications/
21+
*
22+
* @param bool $includingRead
23+
* @param bool $participating
24+
* @param DateTime|null $since
25+
*
26+
* @return array array of notifications
27+
*/
28+
public function all($includingRead = false, $participating = false, DateTime $since = null)
29+
{
30+
$parameters = array(
31+
'all' => $includingRead,
32+
'participating' => $participating
33+
);
34+
35+
if($since !== null) {
36+
$parameters['since'] = $since->format(DateTime::ISO8601);
37+
}
38+
39+
return $this->get('notifications', $parameters);
40+
}
41+
42+
/**
43+
* Marks all notifications as read from the current date
44+
* Optionally give DateTime to mark as read before that date
45+
*
46+
* @link https://developer.github.com/v3/activity/notifications/#mark-as-read
47+
*
48+
* @param DateTime|null $since
49+
*/
50+
public function markRead(DateTime $since = null)
51+
{
52+
$parameters = array();
53+
54+
if($since !== null) {
55+
$parameters['last_read_at'] = $since->format(DateTime::ISO8601);
56+
}
57+
58+
$this->put('notifications', $parameters);
59+
}
60+
}

lib/Github/Client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @method Api\Issue issue()
2323
* @method Api\Issue issues()
2424
* @method Api\Markdown markdown()
25+
* @method Api\Notification notification()
26+
* @method Api\Notification notifications()
2527
* @method Api\Organization organization()
2628
* @method Api\Organization organizations()
2729
* @method Api\PullRequest pr()
@@ -143,6 +145,11 @@ public function api($name)
143145
$api = new Api\Markdown($this);
144146
break;
145147

148+
case 'notification':
149+
case 'notifications':
150+
$api = new Api\Notification($this);
151+
break;
152+
146153
case 'organization':
147154
case 'organizations':
148155
$api = new Api\Organization($this);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
use DateTime;
6+
7+
class NotificationTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetNotifications()
13+
{
14+
$parameters = array(
15+
'all' => false,
16+
'participating' => false,
17+
);
18+
19+
$api = $this->getApiMock();
20+
$api->expects($this->once())
21+
->method('get')
22+
->with('notifications', $parameters);
23+
24+
$api->all();
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function shouldGetNotificationsSince()
31+
{
32+
$since = new DateTime('now');
33+
34+
$parameters = array(
35+
'all' => false,
36+
'participating' => false,
37+
'since' => $since->format(DateTime::ISO8601),
38+
);
39+
40+
$api = $this->getApiMock();
41+
$api->expects($this->once())
42+
->method('get')
43+
->with('notifications', $parameters);
44+
45+
$api->all(false, false, $since);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function shouldGetNotificationsIncludingAndParticipating()
52+
{
53+
$parameters = array(
54+
'all' => true,
55+
'participating' => true,
56+
);
57+
58+
$api = $this->getApiMock();
59+
$api->expects($this->once())
60+
->method('get')
61+
->with('notifications', $parameters);
62+
63+
$api->all(true, true);
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function shouldMarkNotificationsAsRead()
70+
{
71+
$parameters = array();
72+
73+
$api = $this->getApiMock();
74+
$api->expects($this->once())
75+
->method('put')
76+
->with('notifications', $parameters);
77+
78+
$api->markRead();
79+
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function shouldMarkNotificationsAsReadForGivenDate()
85+
{
86+
$since = new DateTime('now');
87+
88+
$parameters = array(
89+
'last_read_at' => $since->format(DateTime::ISO8601),
90+
);
91+
92+
$api = $this->getApiMock();
93+
$api->expects($this->once())
94+
->method('put')
95+
->with('notifications', $parameters);
96+
97+
$api->markRead($since);
98+
}
99+
100+
protected function getApiClass()
101+
{
102+
return 'Github\Api\Notification';
103+
}
104+
}

0 commit comments

Comments
 (0)