From 7e5e260f88d7a015d29c9d3cc0b267cec7eaeba4 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Sun, 8 Feb 2015 14:12:42 +0100 Subject: [PATCH 1/9] Implemented notifications from github API --- lib/Github/Api/Activity/Notification.php | 62 ++++++++++++++++++++++++ lib/Github/Client.php | 5 ++ 2 files changed, 67 insertions(+) create mode 100644 lib/Github/Api/Activity/Notification.php diff --git a/lib/Github/Api/Activity/Notification.php b/lib/Github/Api/Activity/Notification.php new file mode 100644 index 00000000000..0fc2c8940e5 --- /dev/null +++ b/lib/Github/Api/Activity/Notification.php @@ -0,0 +1,62 @@ + + */ +class Notification extends AbstractApi +{ + /** + * Get a listing of a notifications + * @link https://developer.github.com/v3/activity/notifications/ + * + * @param bool $includingRead + * @param bool $participating + * @param null $since + * + * @return array array of notifications + */ + public function all($includingRead = false, $participating = false, $since = null) + { + $parameters = array( + 'all' => $includingRead, + 'participating' => $participating + ); + + if($since !== null) { + $parameters['since'] = $since; + } + + return $this->get('notifications', $parameters); + } + + /** + * Marks all notifications as read from the current date + * Optionally give DateTimeInterface to mark as read before that date + * + * @link https://developer.github.com/v3/activity/notifications/#mark-as-read + * + * @param DateTimeInterface $since + * + */ + public function markRead(DateTimeInterface $since = null) + { + $parameters = array(); + + if($since !== null) { + $parameters['last_read_at'] = $since->format(DateTime::ISO8601); + } + + $this->put('notifications', $parameters); + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index c1779663858..46434b7ed92 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -143,6 +143,11 @@ public function api($name) $api = new Api\Markdown($this); break; + case 'notification': + case 'notifications': + $api = new Api\Activity\Notification($this); + break; + case 'organization': case 'organizations': $api = new Api\Organization($this); From c7cd41a3ce6d8c8cd0b2f57a8f34e02f1c0ed12c Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Mon, 9 Feb 2015 22:48:50 +0100 Subject: [PATCH 2/9] Added method declarations to docblock to describe __call functionality --- lib/Github/Client.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 46434b7ed92..ac17f5e3084 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -22,6 +22,8 @@ * @method Api\Issue issue() * @method Api\Issue issues() * @method Api\Markdown markdown() + * @method Api\Activity\Notification notification() + * @method Api\Activity\Notification notifications() * @method Api\Organization organization() * @method Api\Organization organizations() * @method Api\PullRequest pr() From 300a09194226a66fd5a85c7949b8dbb07b6493b2 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Tue, 10 Feb 2015 16:03:53 +0100 Subject: [PATCH 3/9] Moved the Notification API outside of Activity --- lib/Github/Api/{Activity => }/Notification.php | 3 +-- lib/Github/Client.php | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) rename lib/Github/Api/{Activity => }/Notification.php (96%) diff --git a/lib/Github/Api/Activity/Notification.php b/lib/Github/Api/Notification.php similarity index 96% rename from lib/Github/Api/Activity/Notification.php rename to lib/Github/Api/Notification.php index 0fc2c8940e5..0d090c5e848 100644 --- a/lib/Github/Api/Activity/Notification.php +++ b/lib/Github/Api/Notification.php @@ -1,10 +1,9 @@ Date: Fri, 13 Feb 2015 00:36:19 +0100 Subject: [PATCH 4/9] Compatability with PHP 5.3 and 5.4 --- lib/Github/Api/Notification.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Github/Api/Notification.php b/lib/Github/Api/Notification.php index 0d090c5e848..289598815c8 100644 --- a/lib/Github/Api/Notification.php +++ b/lib/Github/Api/Notification.php @@ -45,10 +45,9 @@ public function all($includingRead = false, $participating = false, $since = nul * * @link https://developer.github.com/v3/activity/notifications/#mark-as-read * - * @param DateTimeInterface $since - * + * @param \DateTime $since */ - public function markRead(DateTimeInterface $since = null) + public function markRead(DateTime $since = null) { $parameters = array(); From 55df423cba997eb681b85f4b19d0adbe017464f0 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Fri, 13 Feb 2015 01:14:48 +0100 Subject: [PATCH 5/9] Added test for Notification API --- test/Github/Tests/Api/NotificationTest.php | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 test/Github/Tests/Api/NotificationTest.php diff --git a/test/Github/Tests/Api/NotificationTest.php b/test/Github/Tests/Api/NotificationTest.php new file mode 100644 index 00000000000..5b4e233f85d --- /dev/null +++ b/test/Github/Tests/Api/NotificationTest.php @@ -0,0 +1,104 @@ + false, + 'participating' => false, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('notifications', $parameters); + + $api->all(); + } + + /** + * @test + */ + public function shouldGetNotificationsSince() + { + $since = '2015-01-01 00:00:00'; + + $parameters = array( + 'all' => false, + 'participating' => false, + 'since' => $since, + ); + + $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'; + } +} From 985755b0ccda05a9fbbe25d066322aa50b7d11fb Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Fri, 13 Feb 2015 01:16:20 +0100 Subject: [PATCH 6/9] Forgot to remove old use statement --- lib/Github/Api/Notification.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Github/Api/Notification.php b/lib/Github/Api/Notification.php index 289598815c8..914c046a1e9 100644 --- a/lib/Github/Api/Notification.php +++ b/lib/Github/Api/Notification.php @@ -3,7 +3,6 @@ namespace Github\Api; use DateTime; -use DateTimeInterface; /** * API for accessing Notifications from your Git/Github repositories. From be2433092799f09ee66db32c3a7cfeec942fff26 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Fri, 13 Feb 2015 10:39:20 +0100 Subject: [PATCH 7/9] Fixed weird sentence --- lib/Github/Api/Notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/Notification.php b/lib/Github/Api/Notification.php index 914c046a1e9..83d83f0032d 100644 --- a/lib/Github/Api/Notification.php +++ b/lib/Github/Api/Notification.php @@ -15,7 +15,7 @@ class Notification extends AbstractApi { /** - * Get a listing of a notifications + * Get a listing of notifications * @link https://developer.github.com/v3/activity/notifications/ * * @param bool $includingRead From 49488d87ea6e33fdf28b7b669144a501169aa317 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Fri, 13 Feb 2015 10:44:48 +0100 Subject: [PATCH 8/9] Made implementation more consistent with DateTime, altered comments and tests accordingly --- lib/Github/Api/Notification.php | 15 ++++++++------- test/Github/Tests/Api/NotificationTest.php | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Github/Api/Notification.php b/lib/Github/Api/Notification.php index 83d83f0032d..75831137df3 100644 --- a/lib/Github/Api/Notification.php +++ b/lib/Github/Api/Notification.php @@ -16,15 +16,16 @@ class Notification extends AbstractApi { /** * Get a listing of notifications + * * @link https://developer.github.com/v3/activity/notifications/ * - * @param bool $includingRead - * @param bool $participating - * @param null $since + * @param bool $includingRead + * @param bool $participating + * @param DateTime|null $since * * @return array array of notifications */ - public function all($includingRead = false, $participating = false, $since = null) + public function all($includingRead = false, $participating = false, DateTime $since = null) { $parameters = array( 'all' => $includingRead, @@ -32,7 +33,7 @@ public function all($includingRead = false, $participating = false, $since = nul ); if($since !== null) { - $parameters['since'] = $since; + $parameters['since'] = $since->format(DateTime::ISO8601); } return $this->get('notifications', $parameters); @@ -40,11 +41,11 @@ public function all($includingRead = false, $participating = false, $since = nul /** * Marks all notifications as read from the current date - * Optionally give DateTimeInterface to mark as read before that date + * Optionally give DateTime to mark as read before that date * * @link https://developer.github.com/v3/activity/notifications/#mark-as-read * - * @param \DateTime $since + * @param DateTime|null $since */ public function markRead(DateTime $since = null) { diff --git a/test/Github/Tests/Api/NotificationTest.php b/test/Github/Tests/Api/NotificationTest.php index 5b4e233f85d..a212530fffd 100644 --- a/test/Github/Tests/Api/NotificationTest.php +++ b/test/Github/Tests/Api/NotificationTest.php @@ -29,12 +29,12 @@ public function shouldGetNotifications() */ public function shouldGetNotificationsSince() { - $since = '2015-01-01 00:00:00'; + $since = new DateTime('now'); $parameters = array( 'all' => false, 'participating' => false, - 'since' => $since, + 'since' => $since->format(DateTime::ISO8601), ); $api = $this->getApiMock(); From 8246468b0e758323635e80a0a5cc34c3898fc548 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Sun, 15 Feb 2015 23:02:10 +0100 Subject: [PATCH 9/9] Added documentation for new notification API --- doc/notification.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 doc/notification.md diff --git a/doc/notification.md b/doc/notification.md new file mode 100644 index 00000000000..e2d941ec02b --- /dev/null +++ b/doc/notification.md @@ -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