From fafec70942834b36b38cebb6a6e040dcb7a273e4 Mon Sep 17 00:00:00 2001
From: Bertrand Gauthier <bertrand.gauthier@unicaen.fr>
Date: Wed, 30 Aug 2017 07:35:22 +0000
Subject: [PATCH] =?UTF-8?q?Abandon=20de=20l'interface=20ServiceManagerAwar?=
 =?UTF-8?q?eInterface=20d=C3=A9pr=C3=A9ci=C3=A9e.=20Remplac=C3=A9e=20par?=
 =?UTF-8?q?=20une=20injection=20de=20d=C3=A9pendance=20dans=20une=20factor?=
 =?UTF-8?q?y.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 config/module.config.php                     | 23 +++++++------
 src/UnicaenLdap/Entity/Group.php             |  2 +-
 src/UnicaenLdap/Entity/People.php            | 12 +++----
 src/UnicaenLdap/Service/AbstractService.php  | 34 +++-----------------
 src/UnicaenLdap/Service/GenericFactory.php   | 29 +++++++++++++++++
 src/UnicaenLdap/Service/GroupFactory.php     | 29 +++++++++++++++++
 src/UnicaenLdap/Service/PeopleFactory.php    | 29 +++++++++++++++++
 src/UnicaenLdap/Service/StructureFactory.php | 29 +++++++++++++++++
 src/UnicaenLdap/Service/SystemFactory.php    | 29 +++++++++++++++++
 9 files changed, 168 insertions(+), 48 deletions(-)
 create mode 100644 src/UnicaenLdap/Service/GenericFactory.php
 create mode 100644 src/UnicaenLdap/Service/GroupFactory.php
 create mode 100644 src/UnicaenLdap/Service/PeopleFactory.php
 create mode 100644 src/UnicaenLdap/Service/StructureFactory.php
 create mode 100644 src/UnicaenLdap/Service/SystemFactory.php

diff --git a/config/module.config.php b/config/module.config.php
index 8c396ef..23acffd 100644
--- a/config/module.config.php
+++ b/config/module.config.php
@@ -2,27 +2,26 @@
 
 use UnicaenLdap\LdapFactory;
 use UnicaenLdap\Options\ModuleOptionsFactory;
-use UnicaenLdap\Service\Generic as GenericService;
-use UnicaenLdap\Service\Group as GroupService;
-use UnicaenLdap\Service\People as PeopleService;
-use UnicaenLdap\Service\Structure as StructureService;
-use UnicaenLdap\Service\System as SystemService;
+use UnicaenLdap\Service\GenericFactory as GenericServiceFactory;
+use UnicaenLdap\Service\GroupFactory as GroupServiceFactory;
+use UnicaenLdap\Service\PeopleFactory as PeopleServiceFactory;
+use UnicaenLdap\Service\StructureFactory as StructureServiceFactory;
+use UnicaenLdap\Service\SystemFactory as SystemServiceFactory;
 
 return array(
     'unicaen-ldap' => [
 
     ],
     'service_manager' => [
-        'invokables' => [
-            'LdapServiceGeneric'   => GenericService::class,
-            'LdapServiceGroup'     => GroupService::class,
-            'LdapServicePeople'    => PeopleService::class,
-            'LdapServiceStructure' => StructureService::class,
-            'LdapServiceSystem'    => SystemService::class,
-        ],
         'factories' => [
             'Ldap'        => LdapFactory::class,
             'LdapOptions' => ModuleOptionsFactory::class,
+
+            'LdapServiceGeneric'   => GenericServiceFactory::class,
+            'LdapServiceGroup'     => GroupServiceFactory::class,
+            'LdapServicePeople'    => PeopleServiceFactory::class,
+            'LdapServiceStructure' => StructureServiceFactory::class,
+            'LdapServiceSystem'    => SystemServiceFactory::class,
         ],
     ],
 );
diff --git a/src/UnicaenLdap/Entity/Group.php b/src/UnicaenLdap/Entity/Group.php
index 2ce4ed8..f409d82 100644
--- a/src/UnicaenLdap/Entity/Group.php
+++ b/src/UnicaenLdap/Entity/Group.php
@@ -68,7 +68,7 @@ class Group extends Entity
     public function getPeople($orderBy = null)
     {
         /* @var $people \UnicaenLdap\Service\People */
-        $people = $this->getService()->getServiceManager()->get('ldapServicePeople');
+        $people = $this->getService()->getServiceLocator()->get('ldapServicePeople');
 
         /** @var People[] $result */
         $result = $people->getAllBy($this->get('member'), 'dn', $orderBy);
diff --git a/src/UnicaenLdap/Entity/People.php b/src/UnicaenLdap/Entity/People.php
index ec85e8e..085aff2 100644
--- a/src/UnicaenLdap/Entity/People.php
+++ b/src/UnicaenLdap/Entity/People.php
@@ -133,7 +133,7 @@ class People extends Entity
      */
     public function getGroups(DateTime $dateObservation = null, $orderBy = null)
     {
-        $group = $this->getService()->getServiceManager()->get('ldapServiceGroup');
+        $group = $this->getService()->getServiceLocator()->get('ldapServiceGroup');
 
         return $group->filterValids($group->getAllBy($this->get('memberOf'), 'dn', $orderBy), $dateObservation);
     }
@@ -175,7 +175,7 @@ class People extends Entity
      */
     public function getEduPersonOrgUnit()
     {
-        $structure = $this->getService()->getServiceManager()->get('ldapServiceStructure');
+        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
         $dn = $this->eduPersonOrgUnitDN;
         if (empty($dn)) return null;
 
@@ -189,7 +189,7 @@ class People extends Entity
      */
     public function getEduPersonPrimaryOrgUnit()
     {
-        $structure = $this->getService()->getServiceManager()->get('ldapServiceStructure');
+        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
         $dn = $this->eduPersonPrimaryOrgUnitDN;
         if (empty($dn)) return null;
 
@@ -206,7 +206,7 @@ class People extends Entity
     public function getEntiteAffectation()
     {
         throw new \Exception('Méthode pas finie');
-        $structure = $this->getService()->getServiceManager()->get('ldapServiceStructure');
+        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
         $codes = $this->getNode()->getAttribute('supannEntiteAffectation');
         var_dump($codes);
 
@@ -223,7 +223,7 @@ class People extends Entity
     public function getEntiteAffectationPrincipale()
     {
         throw new \Exception('Méthode pas finie');
-        $structure = $this->getService()->getServiceManager()->get('ldapServiceStructure');
+        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
 
         $codes = [];
         $affectations = $this->getNode()->getAttribute('supannAffectation');
@@ -246,7 +246,7 @@ class People extends Entity
     public function getAffectationDescription()
     {
         throw new \Exception('Méthode pas finie');
-        $structure = $this->getService()->getServiceManager()->get('ldapServiceStructure');
+        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
 
         list($code, $description) = explode(';', $this->supannAffectation);
         $code = $this->supannAffectation;
diff --git a/src/UnicaenLdap/Service/AbstractService.php b/src/UnicaenLdap/Service/AbstractService.php
index 29e74e1..3563248 100644
--- a/src/UnicaenLdap/Service/AbstractService.php
+++ b/src/UnicaenLdap/Service/AbstractService.php
@@ -7,6 +7,7 @@ use UnicaenLdap\Entity\Entity;
 use UnicaenLdap\Exception;
 use UnicaenLdap\Collection;
 use UnicaenLdap\Filter\Filter;
+use Zend\ServiceManager\ServiceLocatorAwareTrait;
 use Zend\ServiceManager\ServiceManager;
 use Zend\ServiceManager\ServiceManagerAwareInterface;
 use Zend\Ldap\Filter\AbstractFilter;
@@ -18,8 +19,10 @@ use Zend\Stdlib\ErrorHandler;
  *
  * @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
  */
-abstract class AbstractService implements ServiceManagerAwareInterface
+abstract class AbstractService
 {
+    use ServiceLocatorAwareTrait;
+
     /**
      * Limite de recherche par défaut
      */
@@ -30,11 +33,6 @@ abstract class AbstractService implements ServiceManagerAwareInterface
      */
     const DEFAULT_OFFSET = 0;
 
-    /**
-     * @var ServiceManager
-     */
-    protected $serviceManager;
-
     /**
      * @var Ldap
      */
@@ -59,28 +57,6 @@ abstract class AbstractService implements ServiceManagerAwareInterface
      */
     protected $count;
 
-    /**
-     * Get service manager
-     *
-     * @return ServiceManager
-     */
-    public function getServiceManager()
-    {
-        return $this->serviceManager;
-    }
-
-    /**
-     * Set service manager
-     *
-     * @param ServiceManager $serviceManager
-     * @return AbstractService
-     */
-    public function setServiceManager(ServiceManager $serviceManager)
-    {
-        $this->serviceManager = $serviceManager;
-        return $this;
-    }
-
     /**
      * Retourne le type du service
      * 
@@ -99,7 +75,7 @@ abstract class AbstractService implements ServiceManagerAwareInterface
     public function getLdap()
     {
         if (empty($this->ldap)){
-            $this->ldap = $this->getServiceManager()->get('ldap');
+            $this->ldap = $this->getServiceLocator()->get('ldap');
         }
         return $this->ldap;
     }
diff --git a/src/UnicaenLdap/Service/GenericFactory.php b/src/UnicaenLdap/Service/GenericFactory.php
new file mode 100644
index 0000000..13c7b29
--- /dev/null
+++ b/src/UnicaenLdap/Service/GenericFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace UnicaenLdap\Service;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ *
+ *
+ * @author Unicaen
+ */
+class GenericFactory implements FactoryInterface
+{
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return Generic
+     */
+    public function createService(ServiceLocatorInterface $serviceLocator)
+    {
+        $service = new Generic();
+
+        $service->setServiceLocator($serviceLocator);
+
+        return $service;
+    }
+}
diff --git a/src/UnicaenLdap/Service/GroupFactory.php b/src/UnicaenLdap/Service/GroupFactory.php
new file mode 100644
index 0000000..7009d9f
--- /dev/null
+++ b/src/UnicaenLdap/Service/GroupFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace UnicaenLdap\Service;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ *
+ *
+ * @author Unicaen
+ */
+class GroupFactory implements FactoryInterface
+{
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return Group
+     */
+    public function createService(ServiceLocatorInterface $serviceLocator)
+    {
+        $service = new Group();
+
+        $service->setServiceLocator($serviceLocator);
+
+        return $service;
+    }
+}
diff --git a/src/UnicaenLdap/Service/PeopleFactory.php b/src/UnicaenLdap/Service/PeopleFactory.php
new file mode 100644
index 0000000..93c0580
--- /dev/null
+++ b/src/UnicaenLdap/Service/PeopleFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace UnicaenLdap\Service;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ *
+ *
+ * @author Unicaen
+ */
+class PeopleFactory implements FactoryInterface
+{
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return People
+     */
+    public function createService(ServiceLocatorInterface $serviceLocator)
+    {
+        $service = new People();
+
+        $service->setServiceLocator($serviceLocator);
+
+        return $service;
+    }
+}
diff --git a/src/UnicaenLdap/Service/StructureFactory.php b/src/UnicaenLdap/Service/StructureFactory.php
new file mode 100644
index 0000000..57bd887
--- /dev/null
+++ b/src/UnicaenLdap/Service/StructureFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace UnicaenLdap\Service;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ *
+ *
+ * @author Unicaen
+ */
+class StructureFactory implements FactoryInterface
+{
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return Structure
+     */
+    public function createService(ServiceLocatorInterface $serviceLocator)
+    {
+        $service = new Structure();
+
+        $service->setServiceLocator($serviceLocator);
+
+        return $service;
+    }
+}
diff --git a/src/UnicaenLdap/Service/SystemFactory.php b/src/UnicaenLdap/Service/SystemFactory.php
new file mode 100644
index 0000000..9e58ea9
--- /dev/null
+++ b/src/UnicaenLdap/Service/SystemFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace UnicaenLdap\Service;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ *
+ *
+ * @author Unicaen
+ */
+class SystemFactory implements FactoryInterface
+{
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return System
+     */
+    public function createService(ServiceLocatorInterface $serviceLocator)
+    {
+        $service = new System();
+
+        $service->setServiceLocator($serviceLocator);
+
+        return $service;
+    }
+}
-- 
GitLab