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 #163 from snapshotpl/refresh-fix
Browse files Browse the repository at this point in the history
Fix refreshing updated modules
  • Loading branch information
Hounddog committed Nov 12, 2013
2 parents 41cd2dc + 034b19a commit 299abcf
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions module/ZfModule/src/ZfModule/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public function indexAction()
$sl = $this->getServiceLocator();
$client = $sl->get('EdpGithub\Client');

$repos = $client->api('current_user')->repos(array('type' =>'all', 'per_page' => 100));
$params = array(
'type' => 'all',
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
);

$repos = $client->api('current_user')->repos($params);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-user-' . $identity->getId();
Expand All @@ -104,7 +111,13 @@ public function organizationAction()
$client = $sl->get('EdpGithub\Client');

$owner = $this->params()->fromRoute('owner', null);
$repos = $client->api('user')->repos($owner);
$params = array(
'type' => 'all',
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
);
$repos = $client->api('user')->repos($owner, $params);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner;
Expand All @@ -116,43 +129,40 @@ public function organizationAction()
return $viewModel;
}

public function fetchModules($repos, $cacheKey)
public function fetchModules ($repos, $cacheKey)
{
$cacheKey .= '-github';
$sl = $this->getServiceLocator();
$mapper = $sl->get('zfmodule_mapper_module');
/* @var $client \EdpGithub\Client */
$client = $sl->get('EdpGithub\Client');
/* @var $cache StorageInterface */
$cache = $sl->get('zfmodule_cache');

$repositories = array();

foreach($repos as $key => $repo) {
foreach ($repos as $repo) {
$isModule = $this->getModuleService()->isModule($repo);
//Verify if repos have been modified
$httpClient = $client->getHttpClient();
/* @var $response \Zend\Http\Response */
$response = $httpClient->getResponse();
if($response->getStatusCode() == 304) {
if($cache->hasItem($cacheKey . '-github')) {
$repositories = $cache->getItem($cacheKey . '-github');
break;
}
} elseif ($cache->hasItem($cacheKey . '-github')) {
$cache->removeItem($cacheKey . '-github');
}

if($repo->fork || !$repo->permissions->push || !$this->getModuleService()->isModule($repo) ) {
continue;
}
$hasCache = $cache->hasItem($cacheKey);

$module = $mapper->findByName($repo->name);
if($module) {
continue;
if ($response->getStatusCode() == 304 && $hasCache) {
$repositories = $cache->getItem($cacheKey);
break;
}

$repositories[] = $repo;
if (!$repo->fork && $repo->permissions->push && $isModule && !$mapper->findByName($repo->name)) {
$repositories[] = $repo;
$cache->removeItem($cacheKey);
}
}

//save list of modules to cache
$cache->setItem($cacheKey . '-github', $repositories);
$cache->setItem($cacheKey, $repositories);

return $repositories;
}
Expand Down

0 comments on commit 299abcf

Please sign in to comment.