Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #304 from ins0/fix/302
Browse files Browse the repository at this point in the history
Fix: added error catch in github retreiver service
  • Loading branch information
GeeH committed Jan 22, 2015
2 parents c9219d5 + 15f3631 commit 63a545d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 19 deletions.
36 changes: 29 additions & 7 deletions module/Application/src/Application/Service/RepositoryRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,84 @@ public function __construct(Client $githubClient)

/**
* Return MetaData from User Repository
*
* @param string $user
* @param string $module
*
* @return mixed
*/
public function getUserRepositoryMetadata($user, $module)
{
return json_decode($this->githubClient->api('repos')->show($user, $module));
try {
$apiResponse = $this->githubClient->api('repos')->show($user, $module);
return json_decode($apiResponse);
} catch (RuntimeException $e) {
return false;
}
}

/**
* Get all Repositories from GitHub User
*
* @param string $user
* @param array $params
*
* @return RepositoryCollection
*/
public function getUserRepositories($user, array $params = array())
public function getUserRepositories($user, $params = array())
{
return $this->githubClient->api('user')->repos($user, $params);
}

/**
* Get File Content from User Repository
*
* @param string $user
* @param string $module
* @param string $filePath
* @return string|null
*
* @return bool|string
*/
public function getRepositoryFileContent($user, $module, $filePath)
{
$contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath);

if (!isset($contentResponse->content)) {
return null;
return false;
}

return base64_decode($contentResponse->content);
}

/**
* Return File MetaData from User Repository
*
* @param string $user
* @param string $module
* @param string $filePath
* @return mixed
*
* @return bool|string
*/
public function getRepositoryFileMetadata($user, $module, $filePath)
{
return json_decode($this->githubClient->api('repos')->content($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 all Repositories from current authenticated GitHub User
*
* @param array $params
*
* @return RepositoryCollection
*/
public function getAuthenticatedUserRepositories(array $params = array())
public function getAuthenticatedUserRepositories($params = array())
{
return $this->githubClient->api('current_user')->repos($params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Application\Service\RepositoryRetriever;
use EdpGithub\Api;
use EdpGithub\Collection;
use EdpGithub\Listener\Exception;
use PHPUnit_Framework_TestCase;

class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -86,10 +87,9 @@ public function testResponseContentMissingOnGetRepositoryFileContent()
);

$service = new RepositoryRetriever($client);

$response = $service->getRepositoryFileContent('foo', 'bar', 'baz');

$this->assertNull($response);
$this->assertFalse($response);
}

public function testCanRetrieveRepositoryFileMetadata()
Expand Down Expand Up @@ -141,6 +141,32 @@ public function testCanRetrieveAuthenticatedUserRepositories()
$this->assertEquals(count($payload), $count);
}


public function testRepositoryFileContentFails()
{
$clientMock = $this->getMock('EdpGithub\Client');
$clientMock->expects($this->any())
->method('api')
->willThrowException(new Exception\RuntimeException);

$service = new RepositoryRetriever($clientMock);
$response = $service->getRepositoryFileContent('foo', 'bar', 'baz');
$this->assertFalse($response);
}

public function testRepositoryDoesNotExists()
{
$clientMock = $this->getMock('EdpGithub\Client');
$clientMock->expects($this->any())
->method('api')
->willThrowException(new Exception\RuntimeException);

$service = new RepositoryRetriever($clientMock);
$response = $service->getUserRepositoryMetadata('foo', 'bar');
$this->assertFalse($response);
}


/**
* @param Api\AbstractApi $apiInstance
* @param array $payload
Expand Down
5 changes: 0 additions & 5 deletions module/ZfModule/src/ZfModule/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ZfModule\Controller;

use Application\Service\RepositoryRetriever;
use EdpGithub\Client;
use EdpGithub\Collection\RepositoryCollection;
use Zend\Cache;
use Zend\Http;
Expand Down Expand Up @@ -56,13 +55,9 @@ public function viewAction()
}

$repository = $this->repositoryRetriever->getUserRepositoryMetadata($vendor, $module);

$readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md');
$license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE');
$license = !$license ? 'No license file found for this Module' : $license;

$composerConf = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'composer.json');
$composerConf = !$composerConf ? 'No composer.json file found for this Module' : json_decode($composerConf, true);

$viewModel = new ViewModel(array(
'vendor' => $vendor,
Expand Down
24 changes: 19 additions & 5 deletions module/ZfModule/view/zf-module/index/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@

<div class="tab-content">
<div class="tab-pane active" id="readme">
<?php echo $this->markdown($this->readme); ?>
<?php if($readme): ?>
<?php echo $this->markdown($this->readme); ?>
<?php else: ?>
No README file found for this Module
<?php endif; ?>
</div>
<!--<div class="tab-pane" id="composer">
<?php if (is_array($composerConf)): ?>
<?php echo $this->composerView($this->composerConf); ?>
<?php if($this->composerConf): ?>
<?php if (is_array($composerConf)): ?>
<?php echo $this->composerView($this->composerConf); ?>
<?php else: ?>
<?php echo $this->composerConf; ?>
<?php endif; ?>
<?php else: ?>
<?php echo $this->composerConf; ?>
No composer.json file found for this Module
<?php endif; ?>
</div>-->
<div class="tab-pane" id="license">
<pre><?php echo $license; ?></pre>
<pre>
<?php if($readme): ?>
<?php echo $license; ?>
<?php else: ?>
No license file found for this Module
<?php endif; ?>
</pre>
</div>
</div>
</div>
Expand Down

0 comments on commit 63a545d

Please sign in to comment.