From eb8d874b0e7144346260049bd162f3bf5f273dca Mon Sep 17 00:00:00 2001
From: Patrizio Bekerle <patrizio@bekerle.com>
Date: Tue, 18 Jan 2022 16:41:15 +0100
Subject: [PATCH] Add PersonForExternalServiceEvent and remove
 LDAPApiProviderInterface

---
 README.md                                   |  2 +
 src/API/LDAPApiProviderInterface.php        | 20 ----------
 src/Event/PersonForExternalServiceEvent.php | 44 +++++++++++++++++++++
 src/Resources/config/services.yaml          |  3 --
 src/Service/DummyLDAPApiProvider.php        | 34 ----------------
 src/Service/LDAPApi.php                     | 14 +++----
 src/TestUtils/DummyLDAPApiProvider.php      | 25 ------------
 7 files changed, 51 insertions(+), 91 deletions(-)
 delete mode 100644 src/API/LDAPApiProviderInterface.php
 create mode 100644 src/Event/PersonForExternalServiceEvent.php
 delete mode 100644 src/Service/DummyLDAPApiProvider.php
 delete mode 100644 src/TestUtils/DummyLDAPApiProvider.php

diff --git a/README.md b/README.md
index 3c28ebc..b03fa1f 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,8 @@ https://symfony.com/doc/current/bundles/configuration.html
 
 ## Customization
 
+TODO: Update
+
 You can implement the [LDAPApiProviderInterface](https://gitlab.tugraz.at/dbp/relay/dbp-relay-base-person-connector-ldap-bundle/-/blob/main/src/API/LDAPApiProviderInterface.php)
 to customize how attributes are fetched from the LDAP server and assigned to the `Person` entity or what `Person` entities
 are fetched from certain external services like in the [ALMA Bundle](https://gitlab.tugraz.at/dbp/library/api-alma-bundle).
diff --git a/src/API/LDAPApiProviderInterface.php b/src/API/LDAPApiProviderInterface.php
deleted file mode 100644
index 51d9bc3..0000000
--- a/src/API/LDAPApiProviderInterface.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Dbp\Relay\BasePersonConnectorLdapBundle\API;
-
-use Dbp\Relay\BasePersonBundle\Entity\Person;
-
-interface LDAPApiProviderInterface
-{
-    /**
-     * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem".
-     */
-    public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false);
-
-    /**
-     * Allows to fetch a person for a services by service id.
-     */
-    public function getPersonForExternalServiceHook(string $service, string $serviceID): Person;
-}
diff --git a/src/Event/PersonForExternalServiceEvent.php b/src/Event/PersonForExternalServiceEvent.php
new file mode 100644
index 0000000..cca8c76
--- /dev/null
+++ b/src/Event/PersonForExternalServiceEvent.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Dbp\Relay\BasePersonConnectorLdapBundle\Event;
+
+use Dbp\Relay\BasePersonBundle\Entity\Person;
+use Symfony\Contracts\EventDispatcher\Event;
+
+class PersonForExternalServiceEvent extends Event
+{
+    public const NAME = 'dbp.relay.base_person_connector_ldap_bundle.person_for_external_service';
+
+    protected $service;
+    protected $serviceID;
+    protected $person;
+
+    public function __construct(string $service, string $serviceID)
+    {
+        $this->service = $service;
+        $this->serviceID = $serviceID;
+        $this->person = null;
+    }
+
+    public function getService(): string
+    {
+        return $this->service;
+    }
+
+    public function getServiceID(): string
+    {
+        return $this->serviceID;
+    }
+
+    public function setPerson(Person $person): void
+    {
+        $this->person = $person;
+    }
+
+    public function getPerson(): ?Person
+    {
+        return $this->person;
+    }
+}
diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml
index 9d2c377..e574c64 100644
--- a/src/Resources/config/services.yaml
+++ b/src/Resources/config/services.yaml
@@ -9,6 +9,3 @@ services:
 
   Dbp\Relay\AuthBundle\API\UserRolesInterface:
     '@Dbp\Relay\BasePersonConnectorLdapBundle\Service\CustomUserRoles'
-
-  Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface:
-    '@Dbp\Relay\BasePersonConnectorLdapBundle\Service\DummyLDAPApiProvider'
diff --git a/src/Service/DummyLDAPApiProvider.php b/src/Service/DummyLDAPApiProvider.php
deleted file mode 100644
index a54c492..0000000
--- a/src/Service/DummyLDAPApiProvider.php
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Dbp\Relay\BasePersonConnectorLdapBundle\Service;
-
-use Dbp\Relay\BasePersonBundle\Entity\Person;
-use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface;
-use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
-
-class DummyLDAPApiProvider implements LDAPApiProviderInterface
-{
-    /**
-     * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem".
-     */
-    public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false)
-    {
-        // For example, you can parse the date of birth from the LDAP attribute and set it to the person object.
-
-//        $birthDate = $attributes['dateofbirth'][0];
-//        $person->setBirthDate($birthDate);
-    }
-
-    public function getPersonForExternalServiceHook(string $service, string $serviceID): Person
-    {
-        // For example, you can use the service and serviceID to get the person from some other service.
-
-//        if ($service === 'SOME-SERVICE') {
-//            return getPersonFromSomeService($serviceID);
-//        }
-
-        throw new BadRequestHttpException("Unknown service: $service");
-    }
-}
diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php
index c659988..245d3d3 100644
--- a/src/Service/LDAPApi.php
+++ b/src/Service/LDAPApi.php
@@ -15,7 +15,7 @@ use Adldap\Connections\ProviderInterface;
 use Adldap\Models\User;
 use Adldap\Query\Builder;
 use Dbp\Relay\BasePersonBundle\Entity\Person;
-use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface;
+use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonForExternalServiceEvent;
 use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent;
 use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPreEvent;
 use Dbp\Relay\CoreBundle\API\UserSessionInterface;
@@ -70,17 +70,14 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
 
     private $birthdayAttributeName;
 
-    private $ldapApiProvider;
-
     /** @var EventDispatcherInterface */
     private $dispatcher;
 
-    public function __construct(ContainerInterface $locator, LDAPApiProviderInterface $ldapApiProvider, EventDispatcherInterface $dispatcher)
+    public function __construct(ContainerInterface $locator, EventDispatcherInterface $dispatcher)
     {
         $this->ad = new Adldap();
         $this->cacheTTL = 0;
         $this->currentPerson = null;
-        $this->ldapApiProvider = $ldapApiProvider;
         $this->locator = $locator;
         $this->deploymentEnv = 'production';
         $this->dispatcher = $dispatcher;
@@ -280,9 +277,6 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
             }
         }
 
-        // Call post-processing hook
-        $this->ldapApiProvider->personFromUserItemPostHook($attributes, $person, $full);
-
         $postEvent = new PersonFromUserItemPostEvent($attributes, $person, $full);
         $this->dispatcher->dispatch($postEvent, PersonFromUserItemPostEvent::NAME);
 
@@ -323,7 +317,9 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
 
     public function getPersonForExternalService(string $service, string $serviceID): Person
     {
-        $person = $this->ldapApiProvider->getPersonForExternalServiceHook($service, $serviceID);
+        $event = new PersonForExternalServiceEvent($service, $serviceID);
+        $this->dispatcher->dispatch($event, PersonForExternalServiceEvent::NAME);
+        $person = $event->getPerson();
 
         if (!$person) {
             throw new BadRequestHttpException("Unknown service: $service");
diff --git a/src/TestUtils/DummyLDAPApiProvider.php b/src/TestUtils/DummyLDAPApiProvider.php
deleted file mode 100644
index ffdf281..0000000
--- a/src/TestUtils/DummyLDAPApiProvider.php
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils;
-
-use Dbp\Relay\BasePersonBundle\Entity\Person;
-use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface;
-use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
-
-class DummyLDAPApiProvider implements LDAPApiProviderInterface
-{
-    /**
-     * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem".
-     */
-    public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false)
-    {
-        $person->setExtraData('test', 'my-test-string');
-    }
-
-    public function getPersonForExternalServiceHook(string $service, string $serviceID): Person
-    {
-        throw new BadRequestHttpException("Unknown service: $service");
-    }
-}
-- 
GitLab