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 #365 from localheinz/fix/plugin-vs-service
Browse files Browse the repository at this point in the history
Fix: Move plugin functionality into appropriate service
  • Loading branch information
Ocramius committed Feb 8, 2015
2 parents 329ed86 + a760788 commit aa444d5
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 128 deletions.
5 changes: 4 additions & 1 deletion module/User/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

return [
'controllers' => [
'invokables' => [
'aliases' => [
'zfcuser' => Controller\UserController::class,
],
'factories' => [
Controller\UserController::class => Controller\UserControllerFactory::class,
],
],
'view_manager' => [
'template_map' => [
Expand Down
18 changes: 15 additions & 3 deletions module/User/src/User/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
use Zend\View\Model\ViewModel;
use ZfcUser\Controller\Plugin\ZfcUserAuthentication;
use ZfcUser\Controller\UserController as ZfcUserController;
use ZfModule\Service;

/**
* @method array listModule(array $options)
* @method ZfcUserAuthentication zfcUserAuthentication()
*/
class UserController extends ZfcUserController
{
/**
* @var Service\Module
*/
private $moduleService;

/**
* @param Service\Module $moduleService
*/
public function __construct(Service\Module $moduleService)
{
$this->moduleService = $moduleService;
}

public function indexAction()
{
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute(static::ROUTE_LOGIN);
}

$viewModel = new ViewModel([
'modules' => $this->listModule([
'user' => true,
]),
'modules' => $this->moduleService->currentUserModules(),
]);

$viewModel->setTemplate('zfc-user/user/index');
Expand Down
26 changes: 26 additions & 0 deletions module/User/src/User/Controller/UserControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace User\Controller;

use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfModule\Service;

class UserControllerFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $controllerManager
* @return UserController
*/
public function createService(ServiceLocatorInterface $controllerManager)
{
/* @var ControllerManager $controllerManager */
$serviceManager = $controllerManager->getServiceLocator();

/* @var Service\Module $moduleService */
$moduleService = $serviceManager->get('zfmodule_service_module');

return new UserController($moduleService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
namespace UserTest\Integration\Controller;

use ApplicationTest\Integration\Util\Bootstrap;
use User\Controller;
use User\Entity\User;
use User\View\Helper\UserOrganizations;
use Zend\Authentication\AuthenticationService;
use Zend\Http;
use Zend\Mvc;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use Zend\View;
use ZfModule\Mvc\Controller\Plugin\ListModule;
use ZfModule\Service;
use ZfModule\View\Helper\TotalModules;

/**
Expand Down Expand Up @@ -74,38 +72,28 @@ public function testIndexActionSetsModulesIfAuthenticated()
->willReturn(new User())
;

$serviceManager = $this->getApplicationServiceLocator();

$serviceManager
->setAllowOverride(true)
->setService(
'zfcuser_auth_service',
$authenticationService
)
;

$listModule = $this->getMockBuilder(ListModule::class)
$moduleService = $this->getMockBuilder(Service\Module::class)
->disableOriginalConstructor()
->getMock()
;

$listModule
$moduleService
->expects($this->once())
->method('__invoke')
->with($this->equalTo([
'user' => true,
]))
->method('currentUserModules')
->willReturn([])
;

/* @var Mvc\Controller\PluginManager $controllerPluginManager */
$controllerPluginManager = $serviceManager->get('ControllerPluginManager');
$serviceManager = $this->getApplicationServiceLocator();

$controllerPluginManager
$serviceManager
->setAllowOverride(true)
->setService(
'listModule',
$listModule
'zfcuser_auth_service',
$authenticationService
)
->setService(
'zfmodule_service_module',
$moduleService
)
;

Expand Down
66 changes: 0 additions & 66 deletions module/ZfModule/src/ZfModule/Mvc/Controller/Plugin/ListModule.php

This file was deleted.

This file was deleted.

48 changes: 48 additions & 0 deletions module/ZfModule/src/ZfModule/Service/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace ZfModule\Service;

use EdpGithub\Client;
use EdpGithub\Collection\RepositoryCollection;
use stdClass;
use ZfcBase\EventManager\EventProvider;
use ZfModule\Entity;
use ZfModule\Mapper;

class Module extends EventProvider
Expand Down Expand Up @@ -80,4 +82,50 @@ public function isModule(stdClass $repository)

return false;
}

/**
* @param $limit
* @return Entity\Module[]
*/
public function allModules($limit = null)
{
return $this->moduleMapper->findAll(
$limit,
'created_at',
'DESC'
);
}

/**
* @return Entity\Module[]
*/
public function currentUserModules()
{
/* @var RepositoryCollection $repositoryCollection */
$repositoryCollection = $this->githubClient->api('current_user')->repos([
'type' => 'all',
'per_page' => 100,
]);

$modules = [];

foreach ($repositoryCollection as $repository) {
if (true === $repository->fork) {
continue;
}

if (false === $repository->permissions->push) {
continue;
}

$module = $this->moduleMapper->findByName($repository->name);
if (!($module instanceof Entity\Module)) {
continue;
}

array_push($modules, $module);
}

return $modules;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace ZfModuleTest\Mock\Collection;

use Iterator;

class RepositoryCollection implements Iterator
{
/**
* @var array
*/
private $repositories;

public function __construct(array $repositories = [])
{
$this->repositories = $repositories;
}

public function current()
{
return current($this->repositories);
}

public function next()
{
next($this->repositories);
}

public function key()
{
return key($this->repositories);
}

public function valid()
{
$key = key($this->repositories);

if (null === $key || false === $key) {
return false;
}

return true;
}

public function rewind()
{
reset($this->repositories);
}
}
Loading

0 comments on commit aa444d5

Please sign in to comment.