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

Commit 5a77073

Browse files
committed
Merge pull request #447 from snapshotpl/contributors
2 parents 6d74b54 + 418e973 commit 5a77073

16 files changed

Lines changed: 818 additions & 3 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require-dev": {
1919
"bjyoungblood/BjyProfiler": "1.1.0",
2020
"fabpot/php-cs-fixer": "~1.4",
21+
"fzaninotto/faker": "^1.4.0",
2122
"phpunit/phpunit": "4.0.*",
2223
"zendframework/zend-developer-tools": "dev-master",
2324
"zendframework/zftool": "dev-master"

composer.lock

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/Application/config/module.config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
use Psr\Log;
77

88
return [
9+
'zf-modules' => [
10+
'repository' => [
11+
'owner' => 'zendframework',
12+
'name' => 'modules.zendframework.com',
13+
],
14+
],
915
'router' => [
1016
'routes' => [
1117
'live-search' => [
@@ -30,6 +36,16 @@
3036
'priority' => 1,
3137
'may_terminate' => true,
3238
],
39+
'contributors' => [
40+
'type' => 'Literal',
41+
'options' => [
42+
'route' => '/contributors',
43+
'defaults' => [
44+
'controller' => Controller\ContributorsController::class,
45+
'action' => 'index',
46+
],
47+
],
48+
],
3349
'feed' => [
3450
'type' => 'Literal',
3551
'options' => [
@@ -76,6 +92,7 @@
7692
'controllers' => [
7793
'factories' => [
7894
Controller\IndexController::class => Controller\IndexControllerFactory::class,
95+
Controller\ContributorsController::class => Controller\ContributorsControllerFactory::class,
7996
Controller\SearchController::class => Controller\SearchControllerFactory::class,
8097
],
8198
],
@@ -111,6 +128,7 @@
111128
],
112129
'view_helpers' => [
113130
'factories' => [
131+
'gitHubRepositoryUrl' => View\Helper\GitHubRepositoryUrlFactory::class,
114132
'sanitizeHtml' => View\Helper\SanitizeHtmlFactory::class,
115133
],
116134
],
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Application\Controller;
4+
5+
use Application\Service\RepositoryRetriever;
6+
use Zend\Mvc\Controller\AbstractActionController;
7+
use Zend\View\Model\ViewModel;
8+
9+
class ContributorsController extends AbstractActionController
10+
{
11+
const LIST_LIMIT = 36;
12+
13+
/**
14+
* @var RepositoryRetriever
15+
*/
16+
private $repositoryRetriever;
17+
18+
/**
19+
* @var array
20+
*/
21+
private $repositoryData;
22+
23+
/**
24+
* @param RepositoryRetriever $repositoryRetriever
25+
* @param array $repositoryData
26+
*/
27+
public function __construct(RepositoryRetriever $repositoryRetriever, array $repositoryData)
28+
{
29+
$this->repositoryRetriever = $repositoryRetriever;
30+
$this->repositoryData = $repositoryData;
31+
}
32+
33+
public function indexAction()
34+
{
35+
$contributors = $this->repositoryRetriever->getContributors(
36+
$this->repositoryData['owner'],
37+
$this->repositoryData['name'],
38+
self::LIST_LIMIT
39+
);
40+
41+
$metadata = $this->repositoryRetriever->getUserRepositoryMetadata(
42+
$this->repositoryData['owner'],
43+
$this->repositoryData['name']
44+
);
45+
46+
return new ViewModel([
47+
'contributors' => $contributors,
48+
'metadata' => $metadata,
49+
]);
50+
}
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Application\Controller;
4+
5+
use Application\Service\RepositoryRetriever;
6+
use Zend\ServiceManager\FactoryInterface;
7+
use Zend\ServiceManager\ServiceLocatorInterface;
8+
9+
class ContributorsControllerFactory implements FactoryInterface
10+
{
11+
public function createService(ServiceLocatorInterface $serviceLocator)
12+
{
13+
$serviceManager = $serviceLocator->getServiceLocator();
14+
$repositoryRetriever = $serviceManager->get(RepositoryRetriever::class);
15+
$repositoryData = $serviceManager->get('Config')['zf-modules']['repository'];
16+
17+
return new ContributorsController($repositoryRetriever, $repositoryData);
18+
}
19+
}

module/Application/src/Application/Service/RepositoryRetriever.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ public function getUserRepositories($user, $params = [])
5353
return $this->githubClient->api('user')->repos($user, $params);
5454
}
5555

56+
/**
57+
* Get repository contributors list
58+
*
59+
* @param string $owner
60+
* @param string $repo
61+
* @return array
62+
*/
63+
public function getContributors($owner, $repo, $limit = 20)
64+
{
65+
try {
66+
$contributors = $this->githubClient->api('repos')->contributors($owner, $repo);
67+
$data = json_decode($contributors, true);
68+
$data = array_reverse($data);
69+
70+
return array_slice($data, 0, $limit);
71+
} catch (RuntimeException $e) {
72+
return false;
73+
}
74+
}
75+
5676
/**
5777
* Get File Content from User Repository
5878
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Application\View\Helper;
4+
5+
use Zend\View\Helper\AbstractHelper;
6+
7+
class GitHubRepositoryUrl extends AbstractHelper
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $owner;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $name;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $url;
23+
24+
/**
25+
* @param string $owner
26+
* @param string $name
27+
*/
28+
public function __construct($owner, $name)
29+
{
30+
$this->owner = (string) $owner;
31+
$this->name = (string) $name;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function __invoke()
38+
{
39+
if (null === $this->url) {
40+
$this->url = sprintf(
41+
'https://github.com/%s/%s',
42+
$this->owner,
43+
$this->name
44+
);
45+
}
46+
47+
return $this->url;
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Application\View\Helper;
4+
5+
use Zend\ServiceManager\FactoryInterface;
6+
use Zend\ServiceManager\ServiceLocatorInterface;
7+
use Zend\View\HelperPluginManager;
8+
9+
class GitHubRepositoryUrlFactory implements FactoryInterface
10+
{
11+
/**
12+
* {@inheritDoc}
13+
*
14+
* @return GitHubRepositoryUrl
15+
*/
16+
public function createService(ServiceLocatorInterface $serviceLocator)
17+
{
18+
/* @var HelperPluginManager $serviceLocator */
19+
$serviceManager = $serviceLocator->getServiceLocator();
20+
21+
$config = $serviceManager->get('Config')['zf-modules']['repository'];
22+
23+
return new GitHubRepositoryUrl(
24+
$config['owner'],
25+
$config['name']
26+
);
27+
}
28+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace ApplicationTest\Integration\Controller;
4+
5+
use Application\Controller;
6+
use Application\Service;
7+
use ApplicationTest\Integration\Util\Bootstrap;
8+
use stdClass;
9+
use Zend\Http;
10+
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
11+
12+
class ContributorsControllerTest extends AbstractHttpControllerTestCase
13+
{
14+
protected function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->setApplicationConfig(Bootstrap::getConfig());
19+
}
20+
21+
public function testContributorsActionCanBeAccessed()
22+
{
23+
$vendor = 'foo';
24+
$name = 'bar';
25+
26+
$config = $this->getApplicationServiceLocator()->get('Config');
27+
28+
$config['zf-modules'] = [
29+
'repository' => [
30+
'owner' => $vendor,
31+
'name' => $name,
32+
],
33+
];
34+
35+
$repositoryRetriever = $this->getMockBuilder(Service\RepositoryRetriever::class)
36+
->disableOriginalConstructor()
37+
->getMock()
38+
;
39+
40+
$repositoryRetriever
41+
->expects($this->once())
42+
->method('getContributors')
43+
->with(
44+
$this->equalTo($vendor),
45+
$this->equalTo($name)
46+
)
47+
->willReturn([])
48+
;
49+
50+
$metaData = new stdClass();
51+
$metaData->forks_count = 200;
52+
$metaData->stargazers_count = 250;
53+
$metaData->watchers_count = 300;
54+
55+
$repositoryRetriever
56+
->expects($this->once())
57+
->method('getUserRepositoryMetadata')
58+
->with(
59+
$this->equalTo($vendor),
60+
$this->equalTo($name)
61+
)
62+
->willReturn($metaData)
63+
;
64+
65+
$this->getApplicationServiceLocator()
66+
->setAllowOverride(true)
67+
->setService(
68+
'Config',
69+
$config
70+
)
71+
->setService(
72+
Service\RepositoryRetriever::class,
73+
$repositoryRetriever
74+
)
75+
;
76+
77+
$this->dispatch('/contributors');
78+
79+
$this->assertControllerName(Controller\ContributorsController::class);
80+
$this->assertActionName('index');
81+
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_200);
82+
}
83+
}

0 commit comments

Comments
 (0)