From 7ea02c75a19f6374c44aa01835285e025d1e2f0b Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:15:09 +0100 Subject: [PATCH 01/26] extracted repository calls to methods --- .../ZfModule/Controller/IndexController.php | 94 +++++++++++++------ 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 23bbceb6..bd6be875 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -51,6 +51,55 @@ public function __construct( $this->githubClient = $githubClient; } + private function getRepositoryCacheKey($user, $module) + { + return 'module-view-' . $user . '-' . $module; + } + + private function getRepositoryMetadata($user, $module) + { + Try { + $apiResponse = $this->githubClient->api('repos')->show($user, $module); + return json_decode($apiResponse); + } Catch(\Exception $e) + { + return false; + } + } + + private function getUserRepositories($user, $params = array()) + { + return $this->githubClient->api('user')->repos($user, $params); + } + + private function getRepositoryFileContent($user, $module, $filePath) + { + $contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); + + if( !isset($contentResponse->content) ){ + return false; + } + + return base64_decode($contentResponse->content); + } + + private function getRepositoryFileMetadata($user, $module, $filePath) + { + Try { + $apiResponse = $this->githubClient->api('repos')->content($user, $module, $filePath); + $apiResponse = json_decode($apiResponse); + return $apiResponse; + + } Catch(\Exception $e) { + return false; + } + } + + private function getAuthUserRepositories($params = array()) + { + return $this->githubClient->api('current_user')->repos($params); + } + public function viewAction() { $vendor = $this->params()->fromRoute('vendor', null); @@ -62,45 +111,32 @@ public function viewAction() return; } - $cacheKey = 'module-view-' . $vendor . '-' . $module; + $repositoryCacheKey = $this->getRepositoryCacheKey($vendor, $module); + $repository = $this->getRepositoryMetadata($vendor, $module); - $repository = json_decode($this->githubClient->api('repos')->show($vendor, $module)); $httpClient = $this->githubClient->getHttpClient(); $response= $httpClient->getResponse(); - if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $this->moduleCache->hasItem($cacheKey)) { - return $this->moduleCache->getItem($cacheKey); + if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $this->moduleCache->hasItem($repositoryCacheKey)) { + return $this->moduleCache->getItem($repositoryCacheKey); } - $readme = $this->githubClient->api('repos')->readme($vendor, $module); - $readme = json_decode($readme); + $readme = $this->getRepositoryFileContent($vendor, $module, 'README.md'); + $license = $this->getRepositoryFileContent($vendor, $module, 'LICENSE'); + $license = $license === false ? 'No license file found for this Module' : $license; - try { - $license = $this->githubClient->api('repos')->content($vendor, $module, 'LICENSE'); - $license = json_decode($license); - $license = base64_decode($license->content); - } catch (\Exception $e) { - $license = 'No license file found for this Module'; - } - - try { - $composerJson = $this->githubClient->api('repos')->content($vendor, $module, 'composer.json'); - $composerConf = json_decode($composerJson); - $composerConf = base64_decode($composerConf->content); - $composerConf = json_decode($composerConf, true); - } catch (\Exception $e) { - $composerConf = 'No composer.json file found for this Module'; - } + $composerConf = $this->getRepositoryFileContent($vendor, $module, 'composer.json'); + $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true); $viewModel = new ViewModel(array( 'vendor' => $vendor, 'module' => $module, 'repository' => $repository, - 'readme' => base64_decode($readme->content), + 'readme' => $readme, 'composerConf' => $composerConf, 'license' => $license, )); - $this->moduleCache->setItem($cacheKey, $viewModel); + $this->moduleCache->setItem($repositoryCacheKey, $viewModel); return $viewModel; } @@ -119,7 +155,7 @@ public function indexAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->githubClient->api('current_user')->repos($params); + $repos = $this->getAuthUserRepositories($params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-user-' . $identity->getId(); @@ -145,7 +181,7 @@ public function organizationAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->githubClient->api('user')->repos($owner, $params); + $repos = $this->getUserRepositories($owner, $params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner; @@ -210,8 +246,7 @@ public function addAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubClient->api('repos')->show($owner, $repo); - $repository = json_decode($repository); + $repository = $this->getRepositoryMetadata($owner, $repo); if (!($repository instanceof \stdClass)) { throw new Exception\RuntimeException( @@ -272,8 +307,7 @@ public function removeAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubClient->api('repos')->show($owner, $repo); - $repository = json_decode($repository); + $repository = $this->getRepositoryMetadata($owner, $repo); if (!$repository instanceof \stdClass) { throw new Exception\RuntimeException( From 623bff361b19d0da7e6dded4b25d619f7f2d75ab Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:29:16 +0100 Subject: [PATCH 02/26] created github service --- module/Application/config/module.config.php | 1 + .../src/Application/Service/GithubService.php | 112 ++++++++++++++++++ .../Service/GithubServiceFactory.php | 23 ++++ 3 files changed, 136 insertions(+) create mode 100644 module/Application/src/Application/Service/GithubService.php create mode 100644 module/Application/src/Application/Service/GithubServiceFactory.php diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index 94d2e3d4..c022d76f 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -87,6 +87,7 @@ 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', + 'GithubService' => 'Application\Service\GithubServiceFactory', ), ), 'translator' => array( diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php new file mode 100644 index 00000000..81f63577 --- /dev/null +++ b/module/Application/src/Application/Service/GithubService.php @@ -0,0 +1,112 @@ +githubClient = $githubClient; + } + + /** + * Return the Repository Cache Key + * + * @param $user + * @param $module + * @return string + */ + public function getRepositoryCacheKey($user, $module) + { + return 'module-view-' . $user . '-' . $module; + } + + /** + * Return MetaData from User Repository + * + * @param $user + * @param $module + * @return bool|mixed + */ + public function getRepositoryMetadata($user, $module) + { + Try { + $apiResponse = $this->githubClient->api('repos')->show($user, $module); + return json_decode($apiResponse); + } Catch(\Exception $e) + { + return false; + } + } + + /** + * Get all User Repositories + * + * @param $user + * @param array $params + * @return mixed + */ + public function getUserRepositories($user, $params = array()) + { + return $this->githubClient->api('user')->repos($user, $params); + } + + /** + * Get File Content from User Repository + * + * @param $user + * @param $module + * @param $filePath + * @return bool|string + */ + public function getRepositoryFileContent($user, $module, $filePath) + { + $contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); + + if( !isset($contentResponse->content) ){ + return false; + } + + return base64_decode($contentResponse->content); + } + + /** + * Return File MetaData from User Repository + * + * @param $user + * @param $module + * @param $filePath + * @return bool|mixed + */ + public function getRepositoryFileMetadata($user, $module, $filePath) + { + Try { + $apiResponse = $this->githubClient->api('repos')->content($user, $module, $filePath); + $apiResponse = json_decode($apiResponse); + return $apiResponse; + + } Catch(\Exception $e) { + return false; + } + } + + /** + * Return all Repositories from Auth User + * + * @param array $params + * @return mixed + */ + public function getAuthUserRepositories($params = array()) + { + return $this->githubClient->api('current_user')->repos($params); + } +} \ No newline at end of file diff --git a/module/Application/src/Application/Service/GithubServiceFactory.php b/module/Application/src/Application/Service/GithubServiceFactory.php new file mode 100644 index 00000000..c599f47c --- /dev/null +++ b/module/Application/src/Application/Service/GithubServiceFactory.php @@ -0,0 +1,23 @@ +get('EdpGithub\Client'); + + return new GithubService($githubClient); + } +} From 927b151c511a8c23c12a1f4c9eac751861bba839 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:32:16 +0100 Subject: [PATCH 03/26] added github service to controller dependencies --- .../src/ZfModule/Controller/IndexController.php | 11 ++++++++++- .../ZfModule/Controller/IndexControllerFactory.php | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index bd6be875..b0f3f745 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -2,6 +2,7 @@ namespace ZfModule\Controller; +use Application\Service\GithubService; use EdpGithub\Client; use EdpGithub\Collection\RepositoryCollection; use Zend\Cache; @@ -33,22 +34,30 @@ class IndexController extends AbstractActionController */ private $githubClient; + /** + * @var GithubService + */ + private $githubService; + /** * @param Cache\Storage\StorageInterface $moduleCache * @param Mapper\Module $moduleMapper * @param Service\Module $moduleService * @param Client $githubClient + * @param GithubService $githubService */ public function __construct( Cache\Storage\StorageInterface $moduleCache, Mapper\Module $moduleMapper, Service\Module $moduleService, - Client $githubClient + Client $githubClient, + GithubService $githubService ) { $this->moduleCache = $moduleCache; $this->moduleMapper = $moduleMapper; $this->moduleService = $moduleService; $this->githubClient = $githubClient; + $this->githubService = $githubService; } private function getRepositoryCacheKey($user, $module) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php index 16b09041..9dd55f06 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php @@ -33,11 +33,15 @@ public function createService(ServiceLocatorInterface $controllerManager) /* @var Client $githubClient */ $githubClient = $serviceManager->get('EdpGithub\Client'); + /* @var Client $githubClient */ + $githubService = $serviceManager->get('GithubService'); + return new IndexController( $moduleCache, $moduleMapper, $moduleService, - $githubClient + $githubClient, + $githubService ); } } From b69d6f6c8c05adc027c56dd87405a25878d0a60e Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:35:35 +0100 Subject: [PATCH 04/26] switched method calls to github service --- .../ZfModule/Controller/IndexController.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index b0f3f745..6dcbdf19 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -120,8 +120,8 @@ public function viewAction() return; } - $repositoryCacheKey = $this->getRepositoryCacheKey($vendor, $module); - $repository = $this->getRepositoryMetadata($vendor, $module); + $repositoryCacheKey = $this->githubService->getRepositoryCacheKey($vendor, $module); + $repository = $this->githubService->getRepositoryMetadata($vendor, $module); $httpClient = $this->githubClient->getHttpClient(); $response= $httpClient->getResponse(); @@ -129,11 +129,11 @@ public function viewAction() return $this->moduleCache->getItem($repositoryCacheKey); } - $readme = $this->getRepositoryFileContent($vendor, $module, 'README.md'); - $license = $this->getRepositoryFileContent($vendor, $module, 'LICENSE'); + $readme = $this->githubService->getRepositoryFileContent($vendor, $module, 'README.md'); + $license = $this->githubService->getRepositoryFileContent($vendor, $module, 'LICENSE'); $license = $license === false ? 'No license file found for this Module' : $license; - $composerConf = $this->getRepositoryFileContent($vendor, $module, 'composer.json'); + $composerConf = $this->githubService->getRepositoryFileContent($vendor, $module, 'composer.json'); $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true); $viewModel = new ViewModel(array( @@ -164,7 +164,7 @@ public function indexAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->getAuthUserRepositories($params); + $repos = $this->githubService->getAuthUserRepositories($params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-user-' . $identity->getId(); @@ -190,7 +190,7 @@ public function organizationAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->getUserRepositories($owner, $params); + $repos = $this->githubService->getUserRepositories($owner, $params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner; @@ -255,7 +255,7 @@ public function addAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->getRepositoryMetadata($owner, $repo); + $repository = $this->githubService->getRepositoryMetadata($owner, $repo); if (!($repository instanceof \stdClass)) { throw new Exception\RuntimeException( @@ -316,7 +316,7 @@ public function removeAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->getRepositoryMetadata($owner, $repo); + $repository = $this->githubService->getRepositoryMetadata($owner, $repo); if (!$repository instanceof \stdClass) { throw new Exception\RuntimeException( From caa9da7b761371c3f2dda54caedbccac33987a4d Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:36:17 +0100 Subject: [PATCH 05/26] removed old controller methods --- .../ZfModule/Controller/IndexController.php | 49 ------------------- 1 file changed, 49 deletions(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 6dcbdf19..14ecc7c1 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -60,55 +60,6 @@ public function __construct( $this->githubService = $githubService; } - private function getRepositoryCacheKey($user, $module) - { - return 'module-view-' . $user . '-' . $module; - } - - private function getRepositoryMetadata($user, $module) - { - Try { - $apiResponse = $this->githubClient->api('repos')->show($user, $module); - return json_decode($apiResponse); - } Catch(\Exception $e) - { - return false; - } - } - - private function getUserRepositories($user, $params = array()) - { - return $this->githubClient->api('user')->repos($user, $params); - } - - private function getRepositoryFileContent($user, $module, $filePath) - { - $contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); - - if( !isset($contentResponse->content) ){ - return false; - } - - return base64_decode($contentResponse->content); - } - - private function getRepositoryFileMetadata($user, $module, $filePath) - { - Try { - $apiResponse = $this->githubClient->api('repos')->content($user, $module, $filePath); - $apiResponse = json_decode($apiResponse); - return $apiResponse; - - } Catch(\Exception $e) { - return false; - } - } - - private function getAuthUserRepositories($params = array()) - { - return $this->githubClient->api('current_user')->repos($params); - } - public function viewAction() { $vendor = $this->params()->fromRoute('vendor', null); From bc18d8bd6f2c45cf542a30f16a61b8c6ab325050 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 02:43:59 +0100 Subject: [PATCH 06/26] cleanup --- .../src/Application/Service/GithubService.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php index 81f63577..d77be604 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/GithubService.php @@ -14,7 +14,8 @@ class GithubService /** * @param Client $githubClient */ - public function __construct(Client $githubClient) { + public function __construct(Client $githubClient) + { $this->githubClient = $githubClient; } @@ -39,11 +40,10 @@ public function getRepositoryCacheKey($user, $module) */ public function getRepositoryMetadata($user, $module) { - Try { + try { $apiResponse = $this->githubClient->api('repos')->show($user, $module); return json_decode($apiResponse); - } Catch(\Exception $e) - { + } catch (\Exception $e) { return false; } } @@ -72,7 +72,7 @@ public function getRepositoryFileContent($user, $module, $filePath) { $contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); - if( !isset($contentResponse->content) ){ + if (!isset($contentResponse->content)) { return false; } @@ -89,12 +89,12 @@ public function getRepositoryFileContent($user, $module, $filePath) */ public function getRepositoryFileMetadata($user, $module, $filePath) { - Try { + try { $apiResponse = $this->githubClient->api('repos')->content($user, $module, $filePath); $apiResponse = json_decode($apiResponse); return $apiResponse; - } Catch(\Exception $e) { + } catch (\Exception $e) { return false; } } @@ -109,4 +109,4 @@ public function getAuthUserRepositories($params = array()) { return $this->githubClient->api('current_user')->repos($params); } -} \ No newline at end of file +} From fec4129979be2de1401d36ebc0e9b4a2f529d48e Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 11:28:11 +0100 Subject: [PATCH 07/26] removed service cachekey method --- .../src/Application/Service/GithubService.php | 12 ------------ .../src/ZfModule/Controller/IndexController.php | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php index d77be604..33027717 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/GithubService.php @@ -19,18 +19,6 @@ public function __construct(Client $githubClient) $this->githubClient = $githubClient; } - /** - * Return the Repository Cache Key - * - * @param $user - * @param $module - * @return string - */ - public function getRepositoryCacheKey($user, $module) - { - return 'module-view-' . $user . '-' . $module; - } - /** * Return MetaData from User Repository * diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 14ecc7c1..7d22acc9 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -71,7 +71,7 @@ public function viewAction() return; } - $repositoryCacheKey = $this->githubService->getRepositoryCacheKey($vendor, $module); + $repositoryCacheKey = 'module-view-' . $vendor . '-' . $module; $repository = $this->githubService->getRepositoryMetadata($vendor, $module); $httpClient = $this->githubClient->getHttpClient(); From f0a1544750b692f25f5a8a03e093a138066437b0 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 11:35:10 +0100 Subject: [PATCH 08/26] added github client runtime exception renamed getRepositoryMetadata --- .../Application/src/Application/Service/GithubService.php | 7 ++++--- .../ZfModule/src/ZfModule/Controller/IndexController.php | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php index 33027717..8b1f6f8e 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/GithubService.php @@ -3,6 +3,7 @@ namespace Application\Service; use EdpGithub\Client; +use EdpGithub\Listener\Exception\RuntimeException; class GithubService { @@ -26,12 +27,12 @@ public function __construct(Client $githubClient) * @param $module * @return bool|mixed */ - public function getRepositoryMetadata($user, $module) + public function getUserRepositoryMetadata($user, $module) { try { $apiResponse = $this->githubClient->api('repos')->show($user, $module); return json_decode($apiResponse); - } catch (\Exception $e) { + } catch (RuntimeException $e) { return false; } } @@ -82,7 +83,7 @@ public function getRepositoryFileMetadata($user, $module, $filePath) $apiResponse = json_decode($apiResponse); return $apiResponse; - } catch (\Exception $e) { + } catch (RuntimeException $e) { return false; } } diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 7d22acc9..2b2f1d77 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -72,7 +72,7 @@ public function viewAction() } $repositoryCacheKey = 'module-view-' . $vendor . '-' . $module; - $repository = $this->githubService->getRepositoryMetadata($vendor, $module); + $repository = $this->githubService->getUserRepositoryMetadata($vendor, $module); $httpClient = $this->githubClient->getHttpClient(); $response= $httpClient->getResponse(); @@ -206,7 +206,7 @@ public function addAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubService->getRepositoryMetadata($owner, $repo); + $repository = $this->githubService->getUserRepositoryMetadata($owner, $repo); if (!($repository instanceof \stdClass)) { throw new Exception\RuntimeException( @@ -267,7 +267,7 @@ public function removeAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubService->getRepositoryMetadata($owner, $repo); + $repository = $this->githubService->getUserRepositoryMetadata($owner, $repo); if (!$repository instanceof \stdClass) { throw new Exception\RuntimeException( From 160505d35d7cf7f97fa5424cd8d012011368d3be Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 11:52:10 +0100 Subject: [PATCH 09/26] added generators --- .../src/Application/Service/GithubService.php | 26 ++++++++++++++----- .../ZfModule/Controller/IndexController.php | 4 +-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php index 8b1f6f8e..4f5f8826 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/GithubService.php @@ -3,6 +3,7 @@ namespace Application\Service; use EdpGithub\Client; +use EdpGithub\Collection\RepositoryCollection; use EdpGithub\Listener\Exception\RuntimeException; class GithubService @@ -38,15 +39,22 @@ public function getUserRepositoryMetadata($user, $module) } /** - * Get all User Repositories + * Get all Repositories from GitHub User * * @param $user * @param array $params - * @return mixed + * @return bool */ public function getUserRepositories($user, $params = array()) { - return $this->githubClient->api('user')->repos($user, $params); + $repositoryCollection = $this->githubClient->api('user')->repos($user, $params); + if( $repositoryCollection instanceof RepositoryCollection ) + { + foreach($repositoryCollection as $repository) + { + yield $repository; + } + } } /** @@ -89,13 +97,19 @@ public function getRepositoryFileMetadata($user, $module, $filePath) } /** - * Return all Repositories from Auth User + * Return all Repositories from current authenticated GitHub User * * @param array $params - * @return mixed */ public function getAuthUserRepositories($params = array()) { - return $this->githubClient->api('current_user')->repos($params); + $repositoryCollection = $this->githubClient->api('current_user')->repos($params); + if( $repositoryCollection instanceof RepositoryCollection ) + { + foreach($repositoryCollection as $repository) + { + yield $repository; + } + } } } diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 2b2f1d77..c8d08277 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -154,11 +154,11 @@ public function organizationAction() } /** - * @param RepositoryCollection $repos + * @param \Generator $repos * @param string $cacheKey * @return array */ - public function fetchModules(RepositoryCollection $repos, $cacheKey) + public function fetchModules(\Generator $repos, $cacheKey) { $cacheKey .= '-github'; From 29e40639271f7848b771d80500c70fdac8c4b5ed Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 11:54:13 +0100 Subject: [PATCH 10/26] doctype fix --- module/Application/src/Application/Service/GithubService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/GithubService.php index 4f5f8826..5d15b406 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/GithubService.php @@ -43,7 +43,6 @@ public function getUserRepositoryMetadata($user, $module) * * @param $user * @param array $params - * @return bool */ public function getUserRepositories($user, $params = array()) { From 75dd6024783b67cab558ecd01a457513e9061ec7 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 20:31:37 +0100 Subject: [PATCH 11/26] renamed service to RepositoryRetriever --- module/Application/config/module.config.php | 2 +- ...hubService.php => RepositoryRetriever.php} | 2 +- ...ory.php => RepositoryRetrieverFactory.php} | 6 ++-- .../ZfModule/Controller/IndexController.php | 28 +++++++++---------- .../Controller/IndexControllerFactory.php | 7 +++-- 5 files changed, 23 insertions(+), 22 deletions(-) rename module/Application/src/Application/Service/{GithubService.php => RepositoryRetriever.php} (99%) rename module/Application/src/Application/Service/{GithubServiceFactory.php => RepositoryRetrieverFactory.php} (72%) diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index c022d76f..e7b002aa 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -87,7 +87,7 @@ 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', - 'GithubService' => 'Application\Service\GithubServiceFactory', + 'RepositoryRetriever' => 'Application\Service\RepositoryRetrieverFactory', ), ), 'translator' => array( diff --git a/module/Application/src/Application/Service/GithubService.php b/module/Application/src/Application/Service/RepositoryRetriever.php similarity index 99% rename from module/Application/src/Application/Service/GithubService.php rename to module/Application/src/Application/Service/RepositoryRetriever.php index 5d15b406..7c58c2ca 100644 --- a/module/Application/src/Application/Service/GithubService.php +++ b/module/Application/src/Application/Service/RepositoryRetriever.php @@ -6,7 +6,7 @@ use EdpGithub\Collection\RepositoryCollection; use EdpGithub\Listener\Exception\RuntimeException; -class GithubService +class RepositoryRetriever { /** * @var Client diff --git a/module/Application/src/Application/Service/GithubServiceFactory.php b/module/Application/src/Application/Service/RepositoryRetrieverFactory.php similarity index 72% rename from module/Application/src/Application/Service/GithubServiceFactory.php rename to module/Application/src/Application/Service/RepositoryRetrieverFactory.php index c599f47c..c45b4bce 100644 --- a/module/Application/src/Application/Service/GithubServiceFactory.php +++ b/module/Application/src/Application/Service/RepositoryRetrieverFactory.php @@ -6,18 +6,18 @@ use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -class GithubServiceFactory implements FactoryInterface +class RepositoryRetrieverFactory implements FactoryInterface { /** * {@inheritDoc} * - * @return Module + * @return RepositoryRetriever */ public function createService(ServiceLocatorInterface $serviceLocator) { /* @var Client $githubClient */ $githubClient = $serviceLocator->get('EdpGithub\Client'); - return new GithubService($githubClient); + return new RepositoryRetriever($githubClient); } } diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index c8d08277..38f6773e 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -2,7 +2,7 @@ namespace ZfModule\Controller; -use Application\Service\GithubService; +use Application\Service\RepositoryRetriever; use EdpGithub\Client; use EdpGithub\Collection\RepositoryCollection; use Zend\Cache; @@ -35,29 +35,29 @@ class IndexController extends AbstractActionController private $githubClient; /** - * @var GithubService + * @var RepositoryRetriever */ - private $githubService; + private $repositoryRetriever; /** * @param Cache\Storage\StorageInterface $moduleCache * @param Mapper\Module $moduleMapper * @param Service\Module $moduleService * @param Client $githubClient - * @param GithubService $githubService + * @param RepositoryRetriever $repositoryRetriever */ public function __construct( Cache\Storage\StorageInterface $moduleCache, Mapper\Module $moduleMapper, Service\Module $moduleService, Client $githubClient, - GithubService $githubService + RepositoryRetriever $repositoryRetriever ) { $this->moduleCache = $moduleCache; $this->moduleMapper = $moduleMapper; $this->moduleService = $moduleService; $this->githubClient = $githubClient; - $this->githubService = $githubService; + $this->repositoryRetriever = $repositoryRetriever; } public function viewAction() @@ -72,7 +72,7 @@ public function viewAction() } $repositoryCacheKey = 'module-view-' . $vendor . '-' . $module; - $repository = $this->githubService->getUserRepositoryMetadata($vendor, $module); + $repository = $this->repositoryRetriever->getUserRepositoryMetadata($vendor, $module); $httpClient = $this->githubClient->getHttpClient(); $response= $httpClient->getResponse(); @@ -80,11 +80,11 @@ public function viewAction() return $this->moduleCache->getItem($repositoryCacheKey); } - $readme = $this->githubService->getRepositoryFileContent($vendor, $module, 'README.md'); - $license = $this->githubService->getRepositoryFileContent($vendor, $module, 'LICENSE'); + $readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md'); + $license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE'); $license = $license === false ? 'No license file found for this Module' : $license; - $composerConf = $this->githubService->getRepositoryFileContent($vendor, $module, 'composer.json'); + $composerConf = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'composer.json'); $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true); $viewModel = new ViewModel(array( @@ -115,7 +115,7 @@ public function indexAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->githubService->getAuthUserRepositories($params); + $repos = $this->repositoryRetriever->getAuthUserRepositories($params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-user-' . $identity->getId(); @@ -141,7 +141,7 @@ public function organizationAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->githubService->getUserRepositories($owner, $params); + $repos = $this->repositoryRetriever->getUserRepositories($owner, $params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner; @@ -206,7 +206,7 @@ public function addAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubService->getUserRepositoryMetadata($owner, $repo); + $repository = $this->repositoryRetriever->getUserRepositoryMetadata($owner, $repo); if (!($repository instanceof \stdClass)) { throw new Exception\RuntimeException( @@ -267,7 +267,7 @@ public function removeAction() $repo = $request->getPost()->get('repo'); $owner = $request->getPost()->get('owner'); - $repository = $this->githubService->getUserRepositoryMetadata($owner, $repo); + $repository = $this->repositoryRetriever->getUserRepositoryMetadata($owner, $repo); if (!$repository instanceof \stdClass) { throw new Exception\RuntimeException( diff --git a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php index 9dd55f06..e3323d02 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php @@ -2,6 +2,7 @@ namespace ZfModule\Controller; +use Application\Service\RepositoryRetriever; use EdpGithub\Client; use Zend\Cache; use Zend\Mvc\Controller\ControllerManager; @@ -33,15 +34,15 @@ public function createService(ServiceLocatorInterface $controllerManager) /* @var Client $githubClient */ $githubClient = $serviceManager->get('EdpGithub\Client'); - /* @var Client $githubClient */ - $githubService = $serviceManager->get('GithubService'); + /* @var RepositoryRetriever $repositoryRetriever */ + $repositoryRetriever = $serviceManager->get('RepositoryRetriever'); return new IndexController( $moduleCache, $moduleMapper, $moduleService, $githubClient, - $githubService + $repositoryRetriever ); } } From 410933d722fb74b957d97ea9914652f6fca425b7 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 16 Dec 2014 21:58:41 +0100 Subject: [PATCH 12/26] updated require php to ~5.5 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cd544d53..3c3a5ce5 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "BSD-3-Clause", "homepage": "https://modules.zendframework.com", "require": { - "php": "~5.4", + "php": "~5.5", "bshaffer/oauth2-server-php": "dev-develop", "zendframework/zendframework": "~2.3.0", "rwoverdijk/assetmanager": "1.3.*", From 34d798841c458d596eb4ff4f0da0081d2bcf3547 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 01:40:51 +0100 Subject: [PATCH 13/26] cs fixes --- .../Application/Service/RepositoryRetriever.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/module/Application/src/Application/Service/RepositoryRetriever.php b/module/Application/src/Application/Service/RepositoryRetriever.php index 7c58c2ca..2d3ccaff 100644 --- a/module/Application/src/Application/Service/RepositoryRetriever.php +++ b/module/Application/src/Application/Service/RepositoryRetriever.php @@ -47,10 +47,8 @@ public function getUserRepositoryMetadata($user, $module) public function getUserRepositories($user, $params = array()) { $repositoryCollection = $this->githubClient->api('user')->repos($user, $params); - if( $repositoryCollection instanceof RepositoryCollection ) - { - foreach($repositoryCollection as $repository) - { + if ($repositoryCollection instanceof RepositoryCollection) { + foreach ($repositoryCollection as $repository) { yield $repository; } } @@ -100,13 +98,11 @@ public function getRepositoryFileMetadata($user, $module, $filePath) * * @param array $params */ - public function getAuthUserRepositories($params = array()) + public function getAuthenticatedUserRepositories($params = array()) { $repositoryCollection = $this->githubClient->api('current_user')->repos($params); - if( $repositoryCollection instanceof RepositoryCollection ) - { - foreach($repositoryCollection as $repository) - { + if ($repositoryCollection instanceof RepositoryCollection) { + foreach ($repositoryCollection as $repository) { yield $repository; } } From d83a02697dff4ee37ce2f6d48f8f61024b5d2007 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 01:41:39 +0100 Subject: [PATCH 14/26] rename method --- module/ZfModule/src/ZfModule/Controller/IndexController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 38f6773e..9c5cb4de 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -115,7 +115,7 @@ public function indexAction() ); /* @var RepositoryCollection $repos */ - $repos = $this->repositoryRetriever->getAuthUserRepositories($params); + $repos = $this->repositoryRetriever->getAuthenticatedUserRepositories($params); $identity = $this->zfcUserAuthentication()->getIdentity(); $cacheKey = 'modules-user-' . $identity->getId(); @@ -158,7 +158,7 @@ public function organizationAction() * @param string $cacheKey * @return array */ - public function fetchModules(\Generator $repos, $cacheKey) + public function fetchModules($repos, $cacheKey) { $cacheKey .= '-github'; From 479488262dff4fe869fbc07e9e7a315efe96acba Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 01:41:58 +0100 Subject: [PATCH 15/26] added test --- .../Service/RepositoryRetrieverTest.php | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php new file mode 100644 index 00000000..ecbda4f0 --- /dev/null +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -0,0 +1,162 @@ +response = $this->getMock('Zend\Http\Response'); + $this->headers = $this->getMock('Zend\Http\Headers'); + $this->httpClient = $this->getMock('EdpGithub\Http\Client'); + $this->client = $this->getMock('EdpGithub\Client'); + } + + public function getClientMock(Api\AbstractApi $apiInstance, $result) + { + $this->response->expects($this->any()) + ->method('getBody') + ->will($this->returnValue($result)); + + $this->response->expects($this->any()) + ->method('getHeaders') + ->will($this->returnValue($this->headers)); + + $this->httpClient->expects($this->any()) + ->method('get') + ->will($this->returnValue($this->response)); + + $this->client->expects($this->any()) + ->method('getHttpClient') + ->will($this->returnValue($this->httpClient)); + + $apiInstance->setClient($this->client); + + $this->client->expects($this->any()) + ->method('api') + ->will($this->returnValue($apiInstance)); + + return $this->client; + } + + public function getRepositoryRetrieverInstance(Api\AbstractApi $apiInstance, $result) + { + $clientMock = $this->getClientMock($apiInstance, $result); + return new RepositoryRetriever($clientMock); + } + + public function testCanRetrieveUserRepositories() + { + $payload = [ + ['name' => 'foo'], + ['name' => 'bar'], + ['name' => 'baz'] + ]; + + $instance = $this->getRepositoryRetrieverInstance(new Api\User, json_encode($payload)); + + $repositories = $instance->getUserRepositories('foo'); + $this->assertInstanceOf('Generator', $repositories); + + $count = 0; + foreach ($repositories as $repository) { + $this->assertEquals(current($payload), (array)$repository); + next($payload); + ++$count; + } + + $this->assertEquals(count($payload), $count); + } + + public function testCanRetrieveUserRepositoryMetadata() + { + $payload = [ + 'name' => 'foo', + 'url' => 'http://foo.com' + ]; + + $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); + $metadata = $instance->getUserRepositoryMetadata('foo', 'bar'); + + $this->assertInstanceOf('stdClass', $metadata); + $this->assertEquals($payload, (array)$metadata); + } + + public function testErrorOnRetreiveUserRepositoryMetadata() + { + $this->client->expects($this->once()) + ->method('api') + ->willThrowException(new RuntimeException); + + $instance = new RepositoryRetriever($this->client); + $response = $instance->getUserRepositoryMetadata('foo', 'bar'); + $this->assertFalse($response); + } + + public function testCanRetrieveRepositoryFileContent() + { + $payload = [ + 'content' => base64_encode('foo') + ]; + $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); + $response = $instance->getRepositoryFileContent('foo', 'bar', 'foo.baz'); + + $this->assertEquals('foo', $response); + } + + public function testResponseContentMissingOnGetRepositoryFileContent() + { + $payload = []; + $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); + $response = $instance->getRepositoryFileContent('foo', 'bar', 'baz'); + + $this->assertFalse($response); + } + + public function testCanRetrieveRepositoryFileMetadata() + { + $payload = [ + 'name' => 'foo', + 'url' => 'http://foo.com' + ]; + + $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); + $metadata = $instance->getRepositoryFileMetadata('foo', 'bar', 'baz'); + + $this->assertInstanceOf('stdClass', $metadata); + $this->assertEquals($payload, (array)$metadata); + } + + public function testCanRetrieveAuthenticatedUserRepositories() + { + $payload = [ + ['name' => 'foo'], + ['name' => 'bar'], + ['name' => 'baz'] + ]; + + $instance = $this->getRepositoryRetrieverInstance(new Api\CurrentUser, json_encode($payload)); + + $repositories = $instance->getAuthenticatedUserRepositories(); + $this->assertInstanceOf('Generator', $repositories); + + $count = 0; + foreach ($repositories as $repository) { + $this->assertEquals(current($payload), (array)$repository); + next($payload); + ++$count; + } + + $this->assertEquals(count($payload), $count); + } +} From 671cde399375ff2ea3bc3de6b7be801f5dd9d381 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 01:48:53 +0100 Subject: [PATCH 16/26] removed travis php 5.4 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 92b92285..0871f65d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: php sudo: false php: - - 5.4 - 5.5 - 5.6 - hhvm From 1572a7291a267ed758390ef377536e2e457af07b Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 01:54:08 +0100 Subject: [PATCH 17/26] added test case --- .../Service/RepositoryRetrieverTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php index ecbda4f0..923678aa 100644 --- a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -137,6 +137,17 @@ public function testCanRetrieveRepositoryFileMetadata() $this->assertEquals($payload, (array)$metadata); } + public function testErrorOnRetrieveRepositoryFileMetadata() + { + $this->client->expects($this->once()) + ->method('api') + ->willThrowException(new RuntimeException); + + $instance = new RepositoryRetriever($this->client); + $response = $instance->getRepositoryFileMetadata('foo', 'bar', 'baz'); + $this->assertFalse($response); + } + public function testCanRetrieveAuthenticatedUserRepositories() { $payload = [ From b714f69d6c357af30fab03564f0323259ab36ddb Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Thu, 18 Dec 2014 22:03:00 +0100 Subject: [PATCH 18/26] added ::class syntax --- module/Application/config/module.config.php | 2 +- .../src/Application/Service/RepositoryRetrieverFactory.php | 2 +- .../src/ZfModule/Controller/IndexControllerFactory.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index e7b002aa..f2b3202d 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -87,7 +87,7 @@ 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', - 'RepositoryRetriever' => 'Application\Service\RepositoryRetrieverFactory', + \Application\Service\RepositoryRetriever::class => \Application\Service\RepositoryRetrieverFactory::class, ), ), 'translator' => array( diff --git a/module/Application/src/Application/Service/RepositoryRetrieverFactory.php b/module/Application/src/Application/Service/RepositoryRetrieverFactory.php index c45b4bce..796037c1 100644 --- a/module/Application/src/Application/Service/RepositoryRetrieverFactory.php +++ b/module/Application/src/Application/Service/RepositoryRetrieverFactory.php @@ -16,7 +16,7 @@ class RepositoryRetrieverFactory implements FactoryInterface public function createService(ServiceLocatorInterface $serviceLocator) { /* @var Client $githubClient */ - $githubClient = $serviceLocator->get('EdpGithub\Client'); + $githubClient = $serviceLocator->get(Client::class); return new RepositoryRetriever($githubClient); } diff --git a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php index e3323d02..588978eb 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexControllerFactory.php @@ -32,10 +32,10 @@ public function createService(ServiceLocatorInterface $controllerManager) $moduleService = $serviceManager->get('zfmodule_service_module'); /* @var Client $githubClient */ - $githubClient = $serviceManager->get('EdpGithub\Client'); + $githubClient = $serviceManager->get(Client::class); /* @var RepositoryRetriever $repositoryRetriever */ - $repositoryRetriever = $serviceManager->get('RepositoryRetriever'); + $repositoryRetriever = $serviceManager->get(RepositoryRetriever::class); return new IndexController( $moduleCache, From 8fe8d8b1c9a827e411d3fa449bea4172456ba229 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Mon, 22 Dec 2014 16:51:21 +0100 Subject: [PATCH 19/26] fixed visibility + added teardown --- .../Service/RepositoryRetrieverTest.php | 55 +++++++------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php index 923678aa..262f81d2 100644 --- a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -9,12 +9,12 @@ class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase { - public $response; - public $headers; - public $httpClient; - public $client; + private $response; + private $headers; + private $httpClient; + private $client; - public function setUp() + protected function setUp() { $this->response = $this->getMock('Zend\Http\Response'); $this->headers = $this->getMock('Zend\Http\Headers'); @@ -22,7 +22,15 @@ public function setUp() $this->client = $this->getMock('EdpGithub\Client'); } - public function getClientMock(Api\AbstractApi $apiInstance, $result) + protected function tearDown() + { + $this->response = null; + $this->headers = null; + $this->httpClient = null; + $this->client = null; + } + + private function getClientMock(Api\AbstractApi $apiInstance, $result) { $this->response->expects($this->any()) ->method('getBody') @@ -49,10 +57,11 @@ public function getClientMock(Api\AbstractApi $apiInstance, $result) return $this->client; } - public function getRepositoryRetrieverInstance(Api\AbstractApi $apiInstance, $result) + private function getRepositoryRetrieverInstance(Api\AbstractApi $apiInstance, $result) { - $clientMock = $this->getClientMock($apiInstance, $result); - return new RepositoryRetriever($clientMock); + return new RepositoryRetriever( + $this->getClientMock($apiInstance, $result) + ); } public function testCanRetrieveUserRepositories() @@ -66,7 +75,7 @@ public function testCanRetrieveUserRepositories() $instance = $this->getRepositoryRetrieverInstance(new Api\User, json_encode($payload)); $repositories = $instance->getUserRepositories('foo'); - $this->assertInstanceOf('Generator', $repositories); + $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); $count = 0; foreach ($repositories as $repository) { @@ -92,17 +101,6 @@ public function testCanRetrieveUserRepositoryMetadata() $this->assertEquals($payload, (array)$metadata); } - public function testErrorOnRetreiveUserRepositoryMetadata() - { - $this->client->expects($this->once()) - ->method('api') - ->willThrowException(new RuntimeException); - - $instance = new RepositoryRetriever($this->client); - $response = $instance->getUserRepositoryMetadata('foo', 'bar'); - $this->assertFalse($response); - } - public function testCanRetrieveRepositoryFileContent() { $payload = [ @@ -120,7 +118,7 @@ public function testResponseContentMissingOnGetRepositoryFileContent() $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); $response = $instance->getRepositoryFileContent('foo', 'bar', 'baz'); - $this->assertFalse($response); + $this->assertNull($response); } public function testCanRetrieveRepositoryFileMetadata() @@ -137,17 +135,6 @@ public function testCanRetrieveRepositoryFileMetadata() $this->assertEquals($payload, (array)$metadata); } - public function testErrorOnRetrieveRepositoryFileMetadata() - { - $this->client->expects($this->once()) - ->method('api') - ->willThrowException(new RuntimeException); - - $instance = new RepositoryRetriever($this->client); - $response = $instance->getRepositoryFileMetadata('foo', 'bar', 'baz'); - $this->assertFalse($response); - } - public function testCanRetrieveAuthenticatedUserRepositories() { $payload = [ @@ -159,7 +146,7 @@ public function testCanRetrieveAuthenticatedUserRepositories() $instance = $this->getRepositoryRetrieverInstance(new Api\CurrentUser, json_encode($payload)); $repositories = $instance->getAuthenticatedUserRepositories(); - $this->assertInstanceOf('Generator', $repositories); + $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); $count = 0; foreach ($repositories as $repository) { From 48746fad7203670f91924cfb0343a62edfe0dd5b Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Mon, 22 Dec 2014 17:15:17 +0100 Subject: [PATCH 20/26] renamed vars for better understanding --- .../Service/RepositoryRetrieverTest.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php index 262f81d2..1c6199c5 100644 --- a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -57,7 +57,7 @@ private function getClientMock(Api\AbstractApi $apiInstance, $result) return $this->client; } - private function getRepositoryRetrieverInstance(Api\AbstractApi $apiInstance, $result) + private function prepareService(Api\AbstractApi $apiInstance, $result) { return new RepositoryRetriever( $this->getClientMock($apiInstance, $result) @@ -72,9 +72,9 @@ public function testCanRetrieveUserRepositories() ['name' => 'baz'] ]; - $instance = $this->getRepositoryRetrieverInstance(new Api\User, json_encode($payload)); + $service = $this->prepareService(new Api\User, json_encode($payload)); - $repositories = $instance->getUserRepositories('foo'); + $repositories = $service->getUserRepositories('foo'); $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); $count = 0; @@ -94,8 +94,8 @@ public function testCanRetrieveUserRepositoryMetadata() 'url' => 'http://foo.com' ]; - $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); - $metadata = $instance->getUserRepositoryMetadata('foo', 'bar'); + $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $metadata = $service->getUserRepositoryMetadata('foo', 'bar'); $this->assertInstanceOf('stdClass', $metadata); $this->assertEquals($payload, (array)$metadata); @@ -106,8 +106,8 @@ public function testCanRetrieveRepositoryFileContent() $payload = [ 'content' => base64_encode('foo') ]; - $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); - $response = $instance->getRepositoryFileContent('foo', 'bar', 'foo.baz'); + $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $response = $service->getRepositoryFileContent('foo', 'bar', 'foo.baz'); $this->assertEquals('foo', $response); } @@ -115,8 +115,8 @@ public function testCanRetrieveRepositoryFileContent() public function testResponseContentMissingOnGetRepositoryFileContent() { $payload = []; - $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); - $response = $instance->getRepositoryFileContent('foo', 'bar', 'baz'); + $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $response = $service->getRepositoryFileContent('foo', 'bar', 'baz'); $this->assertNull($response); } @@ -128,8 +128,8 @@ public function testCanRetrieveRepositoryFileMetadata() 'url' => 'http://foo.com' ]; - $instance = $this->getRepositoryRetrieverInstance(new Api\Repos, json_encode($payload)); - $metadata = $instance->getRepositoryFileMetadata('foo', 'bar', 'baz'); + $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $metadata = $service->getRepositoryFileMetadata('foo', 'bar', 'baz'); $this->assertInstanceOf('stdClass', $metadata); $this->assertEquals($payload, (array)$metadata); @@ -143,9 +143,9 @@ public function testCanRetrieveAuthenticatedUserRepositories() ['name' => 'baz'] ]; - $instance = $this->getRepositoryRetrieverInstance(new Api\CurrentUser, json_encode($payload)); + $service = $this->prepareService(new Api\CurrentUser, json_encode($payload)); - $repositories = $instance->getAuthenticatedUserRepositories(); + $repositories = $service->getAuthenticatedUserRepositories(); $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); $count = 0; From eb5dc544bef9a9136e1214bf5f9679be8c29c734 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Mon, 22 Dec 2014 17:15:47 +0100 Subject: [PATCH 21/26] changed methods return directly --- .../Service/RepositoryRetriever.php | 45 +++++-------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/module/Application/src/Application/Service/RepositoryRetriever.php b/module/Application/src/Application/Service/RepositoryRetriever.php index 2d3ccaff..0b50d7e1 100644 --- a/module/Application/src/Application/Service/RepositoryRetriever.php +++ b/module/Application/src/Application/Service/RepositoryRetriever.php @@ -23,51 +23,39 @@ public function __construct(Client $githubClient) /** * Return MetaData from User Repository - * * @param $user * @param $module - * @return bool|mixed + * @return array|null */ public function getUserRepositoryMetadata($user, $module) { - try { - $apiResponse = $this->githubClient->api('repos')->show($user, $module); - return json_decode($apiResponse); - } catch (RuntimeException $e) { - return false; - } + return json_decode($this->githubClient->api('repos')->show($user, $module)); } /** * Get all Repositories from GitHub User - * * @param $user * @param array $params + * @return RepositoryCollection */ public function getUserRepositories($user, $params = array()) { - $repositoryCollection = $this->githubClient->api('user')->repos($user, $params); - if ($repositoryCollection instanceof RepositoryCollection) { - foreach ($repositoryCollection as $repository) { - yield $repository; - } - } + return $this->githubClient->api('user')->repos($user, $params); } /** * Get File Content from User Repository - * * @param $user * @param $module * @param $filePath - * @return bool|string + * @return string|null */ public function getRepositoryFileContent($user, $module, $filePath) { $contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); if (!isset($contentResponse->content)) { - return false; + return null; } return base64_decode($contentResponse->content); @@ -75,36 +63,23 @@ public function getRepositoryFileContent($user, $module, $filePath) /** * Return File MetaData from User Repository - * * @param $user * @param $module * @param $filePath - * @return bool|mixed + * @return mixed */ public function getRepositoryFileMetadata($user, $module, $filePath) { - try { - $apiResponse = $this->githubClient->api('repos')->content($user, $module, $filePath); - $apiResponse = json_decode($apiResponse); - return $apiResponse; - - } catch (RuntimeException $e) { - return false; - } + return json_decode($this->githubClient->api('repos')->content($user, $module, $filePath)); } /** * Return all Repositories from current authenticated GitHub User - * * @param array $params + * @return RepositoryCollection */ public function getAuthenticatedUserRepositories($params = array()) { - $repositoryCollection = $this->githubClient->api('current_user')->repos($params); - if ($repositoryCollection instanceof RepositoryCollection) { - foreach ($repositoryCollection as $repository) { - yield $repository; - } - } + return $this->githubClient->api('current_user')->repos($params); } } From 89a3f78e4b29e2e32107a23aee9534296c43192c Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Mon, 22 Dec 2014 22:02:01 +0100 Subject: [PATCH 22/26] inline method + added class keyword --- .../ApplicationTest/Service/RepositoryRetrieverTest.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php index 1c6199c5..aa1b7ce3 100644 --- a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -4,7 +4,6 @@ use Application\Service\RepositoryRetriever; use EdpGithub\Api; -use EdpGithub\Listener\Exception\RuntimeException; use PHPUnit_Framework_TestCase; class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase @@ -59,9 +58,7 @@ private function getClientMock(Api\AbstractApi $apiInstance, $result) private function prepareService(Api\AbstractApi $apiInstance, $result) { - return new RepositoryRetriever( - $this->getClientMock($apiInstance, $result) - ); + return new RepositoryRetriever($this->getClientMock($apiInstance, $result)); } public function testCanRetrieveUserRepositories() @@ -75,7 +72,7 @@ public function testCanRetrieveUserRepositories() $service = $this->prepareService(new Api\User, json_encode($payload)); $repositories = $service->getUserRepositories('foo'); - $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); + $this->assertInstanceOf(\EdpGithub\Collection\RepositoryCollection::class, $repositories); $count = 0; foreach ($repositories as $repository) { @@ -146,7 +143,7 @@ public function testCanRetrieveAuthenticatedUserRepositories() $service = $this->prepareService(new Api\CurrentUser, json_encode($payload)); $repositories = $service->getAuthenticatedUserRepositories(); - $this->assertInstanceOf('EdpGithub\Collection\RepositoryCollection', $repositories); + $this->assertInstanceOf(\EdpGithub\Collection\RepositoryCollection::class, $repositories); $count = 0; foreach ($repositories as $repository) { From afc153a38fd4a6fc0ebb30f000e8ab8f079b2cb3 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Sat, 10 Jan 2015 21:48:59 +0100 Subject: [PATCH 23/26] changes to DAMP style --- .../Service/RepositoryRetrieverTest.php | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php index aa1b7ce3..b3793d37 100644 --- a/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php +++ b/module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php @@ -56,11 +56,6 @@ private function getClientMock(Api\AbstractApi $apiInstance, $result) return $this->client; } - private function prepareService(Api\AbstractApi $apiInstance, $result) - { - return new RepositoryRetriever($this->getClientMock($apiInstance, $result)); - } - public function testCanRetrieveUserRepositories() { $payload = [ @@ -69,7 +64,8 @@ public function testCanRetrieveUserRepositories() ['name' => 'baz'] ]; - $service = $this->prepareService(new Api\User, json_encode($payload)); + $clientMock = $this->getClientMock(new Api\User, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); $repositories = $service->getUserRepositories('foo'); $this->assertInstanceOf(\EdpGithub\Collection\RepositoryCollection::class, $repositories); @@ -91,7 +87,8 @@ public function testCanRetrieveUserRepositoryMetadata() 'url' => 'http://foo.com' ]; - $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); $metadata = $service->getUserRepositoryMetadata('foo', 'bar'); $this->assertInstanceOf('stdClass', $metadata); @@ -103,7 +100,10 @@ public function testCanRetrieveRepositoryFileContent() $payload = [ 'content' => base64_encode('foo') ]; - $service = $this->prepareService(new Api\Repos, json_encode($payload)); + + $clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); + $response = $service->getRepositoryFileContent('foo', 'bar', 'foo.baz'); $this->assertEquals('foo', $response); @@ -112,7 +112,10 @@ public function testCanRetrieveRepositoryFileContent() public function testResponseContentMissingOnGetRepositoryFileContent() { $payload = []; - $service = $this->prepareService(new Api\Repos, json_encode($payload)); + + $clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); + $response = $service->getRepositoryFileContent('foo', 'bar', 'baz'); $this->assertNull($response); @@ -125,7 +128,9 @@ public function testCanRetrieveRepositoryFileMetadata() 'url' => 'http://foo.com' ]; - $service = $this->prepareService(new Api\Repos, json_encode($payload)); + $clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); + $metadata = $service->getRepositoryFileMetadata('foo', 'bar', 'baz'); $this->assertInstanceOf('stdClass', $metadata); @@ -140,7 +145,8 @@ public function testCanRetrieveAuthenticatedUserRepositories() ['name' => 'baz'] ]; - $service = $this->prepareService(new Api\CurrentUser, json_encode($payload)); + $clientMock = $this->getClientMock(new Api\CurrentUser, json_encode($payload)); + $service = new RepositoryRetriever($clientMock); $repositories = $service->getAuthenticatedUserRepositories(); $this->assertInstanceOf(\EdpGithub\Collection\RepositoryCollection::class, $repositories); From 8d4eea1a318a9b30715aa28513e368e8b9579532 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Sat, 10 Jan 2015 22:02:06 +0100 Subject: [PATCH 24/26] cleanup & docblocks & type hint --- .../Service/RepositoryRetriever.php | 18 +++++++++--------- .../ZfModule/Controller/IndexController.php | 9 ++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/module/Application/src/Application/Service/RepositoryRetriever.php b/module/Application/src/Application/Service/RepositoryRetriever.php index 0b50d7e1..a2aa50e9 100644 --- a/module/Application/src/Application/Service/RepositoryRetriever.php +++ b/module/Application/src/Application/Service/RepositoryRetriever.php @@ -23,9 +23,9 @@ public function __construct(Client $githubClient) /** * Return MetaData from User Repository - * @param $user - * @param $module - * @return array|null + * @param string $user + * @param string $module + * @return mixed */ public function getUserRepositoryMetadata($user, $module) { @@ -34,11 +34,11 @@ public function getUserRepositoryMetadata($user, $module) /** * Get all Repositories from GitHub User - * @param $user + * @param string $user * @param array $params * @return RepositoryCollection */ - public function getUserRepositories($user, $params = array()) + public function getUserRepositories($user, array $params = array()) { return $this->githubClient->api('user')->repos($user, $params); } @@ -63,9 +63,9 @@ public function getRepositoryFileContent($user, $module, $filePath) /** * Return File MetaData from User Repository - * @param $user - * @param $module - * @param $filePath + * @param string $user + * @param string $module + * @param string $filePath * @return mixed */ public function getRepositoryFileMetadata($user, $module, $filePath) @@ -78,7 +78,7 @@ public function getRepositoryFileMetadata($user, $module, $filePath) * @param array $params * @return RepositoryCollection */ - public function getAuthenticatedUserRepositories($params = array()) + public function getAuthenticatedUserRepositories(array $params = array()) { return $this->githubClient->api('current_user')->repos($params); } diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 9c5cb4de..431a8436 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -82,10 +82,10 @@ public function viewAction() $readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md'); $license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE'); - $license = $license === false ? 'No license file found for this Module' : $license; + $license = !$license ? 'No license file found for this Module' : $license; $composerConf = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'composer.json'); - $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true); + $composerConf = !$composerConf ? 'No composer.json file found for this Module' : json_decode($composerConf, true); $viewModel = new ViewModel(array( 'vendor' => $vendor, @@ -114,7 +114,6 @@ public function indexAction() 'direction' => 'desc', ); - /* @var RepositoryCollection $repos */ $repos = $this->repositoryRetriever->getAuthenticatedUserRepositories($params); $identity = $this->zfcUserAuthentication()->getIdentity(); @@ -154,11 +153,11 @@ public function organizationAction() } /** - * @param \Generator $repos + * @param RepositoryCollection $repos * @param string $cacheKey * @return array */ - public function fetchModules($repos, $cacheKey) + private function fetchModules(RepositoryCollection $repos, $cacheKey) { $cacheKey .= '-github'; From 1ffd29b18a52c77cded19dacc753e5221f0720a6 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Sat, 10 Jan 2015 22:03:09 +0100 Subject: [PATCH 25/26] added doctypes --- .../src/Application/Service/RepositoryRetriever.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/module/Application/src/Application/Service/RepositoryRetriever.php b/module/Application/src/Application/Service/RepositoryRetriever.php index a2aa50e9..7b716240 100644 --- a/module/Application/src/Application/Service/RepositoryRetriever.php +++ b/module/Application/src/Application/Service/RepositoryRetriever.php @@ -45,9 +45,9 @@ public function getUserRepositories($user, array $params = array()) /** * Get File Content from User Repository - * @param $user - * @param $module - * @param $filePath + * @param string $user + * @param string $module + * @param string $filePath * @return string|null */ public function getRepositoryFileContent($user, $module, $filePath) From 9964ba1c7d9cd318621dc32e71992b49aef29801 Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Sat, 10 Jan 2015 22:03:59 +0100 Subject: [PATCH 26/26] removed inline comment --- module/ZfModule/src/ZfModule/Controller/IndexController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/module/ZfModule/src/ZfModule/Controller/IndexController.php b/module/ZfModule/src/ZfModule/Controller/IndexController.php index 431a8436..fed11a14 100644 --- a/module/ZfModule/src/ZfModule/Controller/IndexController.php +++ b/module/ZfModule/src/ZfModule/Controller/IndexController.php @@ -139,7 +139,6 @@ public function organizationAction() 'direction' => 'desc', ); - /* @var RepositoryCollection $repos */ $repos = $this->repositoryRetriever->getUserRepositories($owner, $params); $identity = $this->zfcUserAuthentication()->getIdentity();