diff --git a/composer.json b/composer.json index 57dbc66..4d4a46f 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ ], "require": { "php": ">=5.4.0", - "curl/curl": "1.3.0" + "curl/curl": "1.3.0", + "nesbot/carbon": "1.21.0" }, "require-dev": { "phpunit/phpunit": "~4.0|~5.0", diff --git a/src/Sifter/Sifter.php b/src/Sifter/Sifter.php index 0a619fc..402106d 100644 --- a/src/Sifter/Sifter.php +++ b/src/Sifter/Sifter.php @@ -1,11 +1,11 @@ curl->get($url); if ($this->curl->error) { - throw new \Exception('cURL GET failed with code '.$this->curl->error_code); + throw new \Exception('cURL GET failed with code ' . $this->curl->error_code); } else { - $projects = []; - $json = json_decode($this->curl->response); - if ( ! isset($json->projects) || ! is_array($json->projects)) { - throw new Exception('Projects were not returned'); - } else { - foreach ($json->projects as $project) { - $projects[] = new Project( - $project->name, - $project->primary_company_name, - $project->archived, - $project->url, - $project->issues_url, - $project->milestones_url, - $project->api_url, - $project->api_issues_url, - $project->api_milestones_url, - $project->api_categories_url, - $project->api_people_url - ); - } - - return $projects; + $projects = $this->jsonToProjects(json_decode($this->curl->response)); + return $projects; + } + } + + + /* + * HELPER FUNCTIONS + */ + + private function jsonToProjects($json) + { + $projects = []; + if ( ! isset($json->projects) || ! is_array($json->projects)) { + throw new \Exception('Projects were not returned'); + } else { + foreach ($json->projects as $project) { + $projects[] = new Project( + $project->name, + $project->primary_company_name, + $project->archived, + $project->url, + $project->issues_url, + $project->milestones_url, + $project->api_url, + $project->api_issues_url, + $project->api_milestones_url, + $project->api_categories_url, + $project->api_people_url + ); } + return $projects; } } diff --git a/src/Sifter/model/Issue.php b/src/Sifter/model/Issue.php new file mode 100644 index 0000000..5890cd9 --- /dev/null +++ b/src/Sifter/model/Issue.php @@ -0,0 +1,199 @@ +number = $number; + $this->categoryName = $categoryName; + $this->priority = $priority; + $this->subject = $subject; + $this->description = $description; + $this->milestoneName = $milestoneName; + $this->openerName = $openerName; + $this->openerNmail = $openerNmail; + $this->assigneeName = $assigneeName; + $this->assigneeNmail = $assigneeNmail; + $this->status = $status; + $this->commentCount = $commentCount; + $this->attachmentCount = $attachmentCount; + $this->createdAt = new Carbon($createdAt); + $this->updatedAt = new Carbon($updatedAt); + $this->url = $url; + $this->apiUrl = $apiUrl; + } + + /** + * @return mixed + */ + public function getNumber() + { + return $this->number; + } + + /** + * @return mixed + */ + public function getCategoryName() + { + return $this->categoryName; + } + + /** + * @return mixed + */ + public function getPriority() + { + return $this->priority; + } + + /** + * @return mixed + */ + public function getSubject() + { + return $this->subject; + } + + /** + * @return mixed + */ + public function getDescription() + { + return $this->description; + } + + /** + * @return mixed + */ + public function getMilestoneName() + { + return $this->milestoneName; + } + + /** + * @return mixed + */ + public function getOpenerName() + { + return $this->openerName; + } + + /** + * @return mixed + */ + public function getOpenerNmail() + { + return $this->openerNmail; + } + + /** + * @return mixed + */ + public function getAssigneeName() + { + return $this->assigneeName; + } + + /** + * @return mixed + */ + public function getAssigneeNmail() + { + return $this->assigneeNmail; + } + + /** + * @return mixed + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return mixed + */ + public function getCommentCount() + { + return $this->commentCount; + } + + /** + * @return mixed + */ + public function getAttachmentCount() + { + return $this->attachmentCount; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt->copy(); + } + + /** + * @return mixed + */ + public function getUpdatedAt() + { + return $this->updatedAt->copy(); + } + + /** + * @return mixed + */ + public function getUrl() + { + return $this->url; + } + + + + +} \ No newline at end of file diff --git a/src/Sifter/Project.php b/src/Sifter/model/Project.php similarity index 91% rename from src/Sifter/Project.php rename to src/Sifter/model/Project.php index e6f818f..439cc33 100644 --- a/src/Sifter/Project.php +++ b/src/Sifter/model/Project.php @@ -1,4 +1,7 @@ -error) { throw new \Exception('cURL GET failed with code '.$curl->error_code); } else { - // TODO: Create an IssueResults class and return that (should contain list of Issues as well as other info such as 'page' - return json_decode($curl->response); + return IssuesResource::issuesResourceFromJson($curl->response); } } diff --git a/src/Sifter/resource/IssuesResource.php b/src/Sifter/resource/IssuesResource.php new file mode 100644 index 0000000..1e7b5de --- /dev/null +++ b/src/Sifter/resource/IssuesResource.php @@ -0,0 +1,133 @@ +number, + $issue->category_name, + $issue->priority, + $issue->subject, + $issue->description, + $issue->milestone_name, + $issue->opener_name, + $issue->opener_email, + $issue->assignee_name, + $issue->assignee_email, + $issue->status, + $issue->comment_count, + $issue->attachment_count, + $issue->created_at, + $issue->updated_at, + $issue->url, + $issue->api_url + ); + } + $this->issues = $issueObjects; + } + + /** + * @return array + */ + public function get() { + return $this->issues; + } + + + /** + * @return mixed + */ + public function first() { + return $this->issues[0]; + } + + /** + * @return bool|IssuesResource + * @throws \Exception + */ + public function nextPage() + { + if(parent::getNextPageUrl() == null) { + return false; + } else { + return $this->changePage(parent::getNextPageUrl()); + } + } + + /** + * @return bool|IssuesResource + * @throws \Exception + */ + public function previousPage() + { + if(parent::getPreviousPageUrl() == null) { + return false; + } else { + return $this->changePage(parent::getPreviousPageUrl()); + } + } + + /** + * @param $json + * @return IssuesResource + */ + public static function issuesResourceFromJson($json) { + if(is_string($json)) { + $json = json_decode($json); + } + return new IssuesResource( + $json->issues, + $json->page, + $json->per_page, + $json->total_pages, + $json->next_page_url, + $json->previous_page_url + ); + } + + /** + * @param $pageUrl + * @return IssuesResource + * @throws \Exception + */ + private function changePage($pageUrl) { + $curl = SifterCurl::instance()->getCurl(); + $curl->get($pageUrl); + + if($curl->error) { + throw new \Exception('cURL GET failed with code '.$curl->error_code); + } else { + $json = json_decode($curl->response); + return new IssuesResource( + $json->issues, + $json->page, + $json->per_page, + $json->total_pages, + $json->next_page_url, + $json->previous_page_url + ); + } + } + + +} \ No newline at end of file diff --git a/src/Sifter/resource/Resource.php b/src/Sifter/resource/Resource.php new file mode 100644 index 0000000..6a9f548 --- /dev/null +++ b/src/Sifter/resource/Resource.php @@ -0,0 +1,69 @@ +page = $page; + $this->perPage = $perPage; + $this->totalPages = $totalPages; + $this->nextPageUrl = $nextPageUrl; + $this->previousPageUrl = $previousPageUrl; + } + + abstract function nextPage(); + abstract function previousPage(); + + /** + * @return mixed + */ + public function getPage() + { + return $this->page; + } + + /** + * @return mixed + */ + public function getPerPage() + { + return $this->perPage; + } + + /** + * @return mixed + */ + public function getTotalPages() + { + return $this->totalPages; + } + + /** + * @return mixed + */ + public function getNextPageUrl() + { + return $this->nextPageUrl; + } + + /** + * @return mixed + */ + public function getPreviousPageUrl() + { + return $this->previousPageUrl; + } +} \ No newline at end of file diff --git a/tests/Sifter/SifterTest.php b/tests/Sifter/SifterTest.php index 0b5a7d6..c15077e 100644 --- a/tests/Sifter/SifterTest.php +++ b/tests/Sifter/SifterTest.php @@ -1,12 +1,4 @@ -assertEquals('https://example.sifterapp.com/api/', $sifter->getApiBaseUrl()); - $this->assertArrayHasKey('Accept', $sifter->getHeaders()); - $this->assertArrayHasKey('X-Sifter-Token', $sifter->getHeaders()); - $this->assertEquals('application/json', $sifter->getHeader('Accept')); - $this->assertEquals('1234', $sifter->getHeader('X-Sifter-Token')); } public function testAllProjects() { @@ -45,11 +33,24 @@ public function testAllProjects() { $projects = $sifter->allProjects(); $this->assertTrue(is_array($projects)); + } + + public function testProject() { + + } - if(count($projects) > 0) { - $project = $projects[0]; - $this->assertInstanceOf('Sifter\Project', $project); - } + public function testIssuesThroughProject() { + $sifter = new Sifter(self::$apiKey, self::$apiSubdomain); + $projects = $sifter->allProjects(); + + $this->assertTrue(is_array($projects)); + $this->assertNotEmpty($projects); + $issues = $projects[0]->issues(); + $issue = $issues->first(); + $this->assertInstanceOf('Sifter\Resource\IssuesResource', $issues); + $this->assertInstanceOf('Sifter\Model\Issue', $issue); + $this->assertInstanceOf('Carbon\Carbon', $issue->getCreatedAt()); + $this->assertInstanceOf('Carbon\Carbon', $issue->getUpdatedAt()); }