Commit 2dca4963 authored by David Surville's avatar David Surville
Browse files

Ajout d'une interface et d'un trait pour chaque service et utilisation dans les factories

parent 5ca3ac88
...@@ -67,11 +67,11 @@ class Group extends Entity ...@@ -67,11 +67,11 @@ class Group extends Entity
*/ */
public function getPeople($orderBy = null) public function getPeople($orderBy = null)
{ {
/* @var $people \UnicaenLdap\Service\People */ /* @var $peopleService \UnicaenLdap\Service\People */
$people = $this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\People::class); $peopleService = $this->getService()->getLdapPeopleService();
/** @var People[] $result */ /** @var People[] $result */
$result = $people->getAllBy($this->get('member'), 'dn', $orderBy); $result = $peopleService->getAllBy($this->get('member'), 'dn', $orderBy);
return $result; return $result;
} }
......
...@@ -133,9 +133,9 @@ class People extends Entity ...@@ -133,9 +133,9 @@ class People extends Entity
*/ */
public function getGroups(DateTime $dateObservation = null, $orderBy = null) public function getGroups(DateTime $dateObservation = null, $orderBy = null)
{ {
$group = $this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\Group::class); $groupService = $this->getService()->getLdapGroupService();
return $group->filterValids($group->getAllBy($this->get('memberOf'), 'dn', $orderBy), $dateObservation); return $groupService->filterValids($groupService->getAllBy($this->get('memberOf'), 'dn', $orderBy), $dateObservation);
} }
/** /**
...@@ -168,67 +168,50 @@ class People extends Entity ...@@ -168,67 +168,50 @@ class People extends Entity
return 0 === strpos($this->uid, 'i'); return 0 === strpos($this->uid, 'i');
} }
/**
* @var \UnicaenLdap\Service\Structure
*/
protected $ldapStructureService;
/**
* @return \UnicaenLdap\Service\Structure
*/
protected function getLdapStructureService()
{
if (null === $this->ldapStructureService) {
$this->ldapStructureService =
$this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\Structure::class);
}
return $this->ldapStructureService;
}
/** /**
* Retourne les structures auxquelles appartiennent la personne * Retourne les structures auxquelles appartiennent la personne
* *
* @return Structure[] * @return Entity[]|Structure[]
*/ */
public function getEduPersonOrgUnit() public function getEduPersonOrgUnit()
{ {
$structure = $this->getLdapStructureService(); $structureService = $this->getService()->getLdapStructureService();
$dn = $this->eduPersonOrgUnitDN; $dn = $this->eduPersonOrgUnitDN;
if (empty($dn)) return null; if (empty($dn)) return null;
return $structure->getAllBy($dn, 'dn'); return $structureService->getAllBy($dn, 'dn');
} }
/** /**
* Retourne la structure principale à laquelle appartient la personne * Retourne la structure principale à laquelle appartient la personne
* *
* @return Structure * @return Entity|Structure
* @throws \UnicaenLdap\Exception
*/ */
public function getEduPersonPrimaryOrgUnit() public function getEduPersonPrimaryOrgUnit()
{ {
$structure = $this->getLdapStructureService(); $structureService = $this->getService()->getLdapStructureService();
$dn = $this->eduPersonPrimaryOrgUnitDN; $dn = $this->eduPersonPrimaryOrgUnitDN;
if (empty($dn)) return null; if (empty($dn)) return null;
return $structure->getBy($dn, 'dn'); return $structureService->getBy($dn, 'dn');
} }
/** /**
* Retourne la structure d'affectation de la personne * Retourne la structure d'affectation de la personne
* *
* @todo à terminer * @todo à terminer
* @return Structure[] * @return Entity|Structure
* @throws \Exception * @throws \Exception
*/ */
public function getEntiteAffectation() public function getEntiteAffectation()
{ {
throw new \Exception('Méthode pas finie'); throw new \Exception('Méthode pas finie');
$structure = $this->getLdapStructureService(); $structureService = $this->getService()->getLdapStructureService();
$codes = $this->getNode()->getAttribute('supannEntiteAffectation'); $codes = $this->getNode()->getAttribute('supannEntiteAffectation');
var_dump($codes); var_dump($codes);
return $structure->getBy($dn, 'dn'); return $structureService->getBy($dn, 'dn');
} }
/** /**
...@@ -241,7 +224,7 @@ class People extends Entity ...@@ -241,7 +224,7 @@ class People extends Entity
public function getEntiteAffectationPrincipale() public function getEntiteAffectationPrincipale()
{ {
throw new \Exception('Méthode pas finie'); throw new \Exception('Méthode pas finie');
$structure = $this->getLdapStructureService(); $structureService = $this->getService()->getLdapStructureService();
$codes = []; $codes = [];
$affectations = $this->getNode()->getAttribute('supannAffectation'); $affectations = $this->getNode()->getAttribute('supannAffectation');
...@@ -251,7 +234,7 @@ class People extends Entity ...@@ -251,7 +234,7 @@ class People extends Entity
$code = $this->supannAffectation; $code = $this->supannAffectation;
if (empty($dn)) return null; if (empty($dn)) return null;
return $structure->getBy($dn, 'dn'); return $structureService->getBy($dn, 'dn');
} }
/** /**
...@@ -264,13 +247,13 @@ class People extends Entity ...@@ -264,13 +247,13 @@ class People extends Entity
public function getAffectationDescription() public function getAffectationDescription()
{ {
throw new \Exception('Méthode pas finie'); throw new \Exception('Méthode pas finie');
$structure = $this->getLdapStructureService(); $structureService = $this->getService()->getLdapStructureService();
list($code, $description) = explode(';', $this->supannAffectation); list($code, $description) = explode(';', $this->supannAffectation);
$code = $this->supannAffectation; $code = $this->supannAffectation;
if (empty($dn)) return null; if (empty($dn)) return null;
return $structure->getBy($dn, 'dn'); return $structureService->getBy($dn, 'dn');
} }
/** /**
......
...@@ -17,8 +17,19 @@ use Zend\Stdlib\ErrorHandler; ...@@ -17,8 +17,19 @@ use Zend\Stdlib\ErrorHandler;
* *
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr> * @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/ */
abstract class AbstractService abstract class AbstractService implements
LdapPeopleServiceAwareInterface,
LdapStructureServiceAwareInterface,
LdapGroupServiceAwareInterface,
LdapSystemServiceAwareInterface,
LdapGenericServiceAwareInterface
{ {
use LdapPeopleServiceAwareTrait;
use LdapStructureServiceAwareTrait;
use LdapGroupServiceAwareTrait;
use LdapSystemServiceAwareTrait;
use LdapGenericServiceAwareTrait;
/** /**
* Limite de recherche par défaut * Limite de recherche par défaut
*/ */
...@@ -53,37 +64,6 @@ abstract class AbstractService ...@@ -53,37 +64,6 @@ abstract class AbstractService
*/ */
protected $count; protected $count;
/**
* @var array
*/
protected $ldapServicesByEntityClass = [
\UnicaenLdap\Entity\People::class => null,
\UnicaenLdap\Entity\Structure::class => null,
\UnicaenLdap\Entity\System::class => null,
\UnicaenLdap\Entity\Group::class => null,
\UnicaenLdap\Entity\Generic::class => null,
];
/**
* @param array
*/
public function setLdapServicesByEntityClass(array $ldapServicesByEntityClass)
{
$this->ldapServicesByEntityClass = $ldapServicesByEntityClass;
}
/**
* @param string $entityClass
* @return mixed
*/
public function getLdapServiceForEntityClass($entityClass)
{
if (! isset($this->ldapServicesByEntityClass[$entityClass])) {
throw new RuntimeException("Le service de recherche LDAP pour la classe d'entité $entityClass n'a pas été injecté.");
}
return $this->ldapServicesByEntityClass[$entityClass];
}
/** /**
* Retourne le type du service * Retourne le type du service
......
...@@ -3,14 +3,11 @@ ...@@ -3,14 +3,11 @@
namespace UnicaenLdap\Service; namespace UnicaenLdap\Service;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use UnicaenLdap\Ldap;
use UnicaenLdap\Service;
use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorInterface;
/**
*
*
* @author Unicaen
*/
class GenericFactory implements FactoryInterface class GenericFactory implements FactoryInterface
{ {
public function createService(ServiceLocatorInterface $serviceLocator) public function createService(ServiceLocatorInterface $serviceLocator)
...@@ -18,22 +15,35 @@ class GenericFactory implements FactoryInterface ...@@ -18,22 +15,35 @@ class GenericFactory implements FactoryInterface
return $this->__invoke($serviceLocator, '?'); return $this->__invoke($serviceLocator, '?');
} }
/**
* Create service
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return object|Generic
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{ {
$service = new Generic(); /**
* @var Ldap $ldap
/** @var \UnicaenLdap\Ldap $ldap */ * @var Service\People $ldapPeopleService
* @var Service\Structure $ldapStructureService
* @var Service\Group $ldapGroupService
* @var Service\System $ldapSystemService
*/
$ldap = $container->get('ldap'); $ldap = $container->get('ldap');
$ldapPeopleService = $container->get('ldapServicePeople');
$ldapStructureService = $container->get('ldapServiceStructure');
$ldapGroupService = $container->get('ldapServiceGroup');
$ldapSystemService = $container->get('ldapServiceSystem');
$service = new Generic();
$service->setLdap($ldap); $service->setLdap($ldap);
$service->setLdapPeopleService($ldapPeopleService);
$service->setLdapServicesByEntityClass([ $service->setLdapStructureService($ldapStructureService);
\UnicaenLdap\Entity\People::class => $container->get('ldapServicePeople'), $service->setLdapGroupService($ldapGroupService);
\UnicaenLdap\Entity\Structure::class => $container->get('ldapServiceStructure'), $service->setLdapSystemService($ldapSystemService);
\UnicaenLdap\Entity\System::class => $container->get('ldapServiceSystem'),
\UnicaenLdap\Entity\Group::class => $container->get('ldapServiceGroup'),
// \UnicaenLdap\Entity\Generic::class => $container->get('ldapServiceGeneric'),
]);
return $service; return $service;
} }
......
...@@ -3,14 +3,11 @@ ...@@ -3,14 +3,11 @@
namespace UnicaenLdap\Service; namespace UnicaenLdap\Service;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use UnicaenLdap\Ldap;
use UnicaenLdap\Service;
use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorInterface;
/**
*
*
* @author Unicaen
*/
class GroupFactory implements FactoryInterface class GroupFactory implements FactoryInterface
{ {
public function createService(ServiceLocatorInterface $serviceLocator) public function createService(ServiceLocatorInterface $serviceLocator)
...@@ -18,22 +15,35 @@ class GroupFactory implements FactoryInterface ...@@ -18,22 +15,35 @@ class GroupFactory implements FactoryInterface
return $this->__invoke($serviceLocator, '?'); return $this->__invoke($serviceLocator, '?');
} }
/**
* Create service
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return object|Group
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{ {
$service = new Group(); /**
* @var Ldap $ldap
/** @var \UnicaenLdap\Ldap $ldap */ * @var Service\People $ldapPeopleService
* @var Service\Structure $ldapStructureService
* @var Service\System $ldapSystemService
* @var Service\Generic $ldapGenericService
*/
$ldap = $container->get('ldap'); $ldap = $container->get('ldap');
$ldapPeopleService = $container->get('ldapServicePeople');
$ldapStructureService = $container->get('ldapServiceStructure');
$ldapSystemService = $container->get('ldapServiceSystem');
$ldapGenericService = $container->get('ldapServiceGeneric');
$service = new Group();
$service->setLdap($ldap); $service->setLdap($ldap);
$service->setLdapPeopleService($ldapPeopleService);
$service->setLdapServicesByEntityClass([ $service->setLdapStructureService($ldapStructureService);
\UnicaenLdap\Entity\People::class => $container->get('ldapServicePeople'), $service->setLdapSystemService($ldapSystemService);
\UnicaenLdap\Entity\Structure::class => $container->get('ldapServiceStructure'), $service->setLdapGenericService($ldapGenericService);
\UnicaenLdap\Entity\System::class => $container->get('ldapServiceSystem'),
// \UnicaenLdap\Entity\Group::class => $container->get('ldapServiceGroup'),
\UnicaenLdap\Entity\Generic::class => $container->get('ldapServiceGeneric'),
]);
return $service; return $service;
} }
......
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Generic as LdapGenericService;
interface LdapGenericServiceAwareInterface
{
/**
* @param LdapGenericService $ldapGenericService
* @return mixed
*/
public function setLdapGenericService(LdapGenericService $ldapGenericService);
/**
* @return LdapGenericService
*/
public function getLdapGenericService();
}
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Generic as LdapGenericService;
trait LdapGenericServiceAwareTrait
{
/**
* @var LdapGenericService
*/
protected $ldapGenericService;
/**
* @param LdapGenericService $ldapGenericService
*/
public function setLdapGenericService(LdapGenericService $ldapGenericService)
{
$this->ldapGenericService = $ldapGenericService;
}
/**
* @return LdapGenericService
*/
public function getLdapGenericService()
{
return $this->ldapGenericService;
}
}
\ No newline at end of file
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Group as LdapGroupService;
interface LdapGroupServiceAwareInterface
{
/**
* @param LdapGroupService $ldapGroupService
* @return mixed
*/
public function setLdapGroupService(LdapGroupService $ldapGroupService);
/**
* @return LdapGroupService
*/
public function getLdapGroupService();
}
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Group as LdapGroupService;
trait LdapGroupServiceAwareTrait
{
/**
* @var LdapGroupService
*/
protected $ldapGroupService;
/**
* @param LdapGroupService $ldapGroupService
*/
public function setLdapGroupService(LdapGroupService $ldapGroupService)
{
$this->ldapGroupService = $ldapGroupService;
}
/**
* @return LdapGroupService
*/
public function getLdapGroupService()
{
return $this->ldapGroupService;
}
}
\ No newline at end of file
...@@ -6,5 +6,14 @@ use UnicaenLdap\Service\People as LdapPeopleService; ...@@ -6,5 +6,14 @@ use UnicaenLdap\Service\People as LdapPeopleService;
interface LdapPeopleServiceAwareInterface interface LdapPeopleServiceAwareInterface
{ {
/**
* @param LdapPeopleService $ldapPeopleService
* @return mixed
*/
public function setLdapPeopleService(LdapPeopleService $ldapPeopleService); public function setLdapPeopleService(LdapPeopleService $ldapPeopleService);
}
\ No newline at end of file /**
* @return LdapPeopleService
*/
public function getLdapPeopleService();
}
...@@ -18,4 +18,12 @@ trait LdapPeopleServiceAwareTrait ...@@ -18,4 +18,12 @@ trait LdapPeopleServiceAwareTrait
{ {
$this->ldapPeopleService = $ldapPeopleService; $this->ldapPeopleService = $ldapPeopleService;
} }
/**
* @return LdapPeopleService
*/
public function getLdapPeopleService()
{
return $this->ldapPeopleService;
}
} }
\ No newline at end of file
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Structure as LdapStructureService;
interface LdapStructureServiceAwareInterface
{
/**
* @param LdapStructureService $ldapStructureService
* @return mixed
*/
public function setLdapStructureService(LdapStructureService $ldapStructureService);
/**
* @return LdapStructureService
*/
public function getLdapStructureService();
}
<?php
namespace UnicaenLdap\Service;
use UnicaenLdap\Service\Structure as LdapStructureService;
trait LdapStructureServiceAwareTrait
{
/**
* @var LdapStructureService
*/
protected $ldapStructureService;
/**
* @param LdapStructureService $ldapStructureService
*/
public function setLdapStructureService(LdapStructureService $ldapStructureService)
{
$this->ldapStructureService = $ldapStructureService;
}
/**
* @return LdapStructureService
*/
public function getLdapStructureService()