Explorar o código

https://assistance.opentalent.fr/browse/V8-1877

Olivier Massot %!s(int64=4) %!d(string=hai) anos
pai
achega
940bc3be65

+ 51 - 14
ot_core/Classes/Domain/Repository/BaseApiRepository.php

@@ -8,6 +8,8 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerAwareTrait;
+use Symfony\Component\Yaml\Exception\ParseException;
+use Symfony\Component\Yaml\Yaml;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
 
@@ -19,13 +21,16 @@ abstract class BaseApiRepository implements LoggerAwareInterface
 {
     use LoggerAwareTrait;
 
-    const BASE_URI = 'https://api.opentalent.fr/api/';
+    const DEFAULT_BASE_URI = 'https://api.opentalent.fr/api/';
+    const URI_TRAILING_PART = '';
     const HYDRA_TYPE = '';
     const HTTP_METHOD = 'GET';
     const DEFAULT_ITEMS_PER_PAGE = 8;
 
-    protected $client;
-    protected $context;
+    protected string $base_uri = self::DEFAULT_BASE_URI;
+    protected array $variants_uris = [];
+    protected Client $client;
+    protected ApplicationContext $context;
 
     /**
      * BaseApiRepository constructor.
@@ -38,16 +43,49 @@ abstract class BaseApiRepository implements LoggerAwareInterface
         ?Client $client = null,
         ?ApplicationContext $context = null
     ) {
+        if ($context === null) {
+            $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
+        } else {
+            $this->context = $context;
+        }
+
+        $this->loadConf();
+
         if ($client === null) {
-            $this->client = new Client(['base_uri' => static::BASE_URI]);
+            $this->client = new Client(['base_uri' => $this->getApiUri()]);
         } else {
             $this->client = $client;
         }
-        if ($context === null) {
-            $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
+    }
+
+    private function loadConf() {
+        $conf_path = $_ENV['TYPO3_PATH_ROOT'] . '/typo3conf/ext/ot_core/Configuration/ot_config.yaml';
+        $conf = Yaml::parseFile($conf_path);
+
+        // api_variant_uri: Should we set an alternative uri for the API? (dev and testing only)
+        if ($this->context->isDevelopment() || $this->context->isTesting()) {
+            $this->variants_uris = $conf['api_variant_uri'];
+        }
+    }
+
+    /**
+     * Return the API URI for the current repository
+     *
+     * @param string $trailing_part
+     * @return string
+     */
+    protected function getApiUri(string $trailing_part = null): string
+    {
+        $host = $_SERVER['HTTP_HOST'];
+        if (isset($this->variants_uris[$host])) {
+            $uri = $this->variants_uris[$host];
         } else {
-            $this->context = $context;
+            $uri = self::DEFAULT_BASE_URI;
         }
+
+        $trailing_part = $trailing_part ?? $this::URI_TRAILING_PART;
+        $uri = trim($uri, '/') . '/' . trim($trailing_part, '/');
+        return $uri;
     }
 
     /**
@@ -62,13 +100,13 @@ abstract class BaseApiRepository implements LoggerAwareInterface
      * Send a request to the API and
      * returns the records as an array (members)
      *
-     * @param string $uri
      * @param array $params
      * @return ApiPagedCollection
      * @throws ApiRequestException
      */
-    protected function getApiRecords(string $uri, $params = []) {
-        $body = $this->getJsonDecoded($uri, $params);
+    protected function getApiRecords($params = []): ApiPagedCollection
+    {
+        $body = $this->getJsonDecoded($this->getApiUri(), $params);
 
         $page = (int)($params['page'] ?? 1);
 
@@ -104,16 +142,15 @@ abstract class BaseApiRepository implements LoggerAwareInterface
      * Send a request to the API and
      * returns the first record (member)
      *
-     * @param string $uri
      * @param array $params
      * @return object
      * @throws ApiRequestException
      */
-    protected function getApiFirstRecord(string $uri, $params = []): object
+    protected function getApiFirstRecord($params = []): object
     {
         $params['page'] = '1';
         $params['totalItems'] = '1';
-        $collection = $this->getApiRecords($uri, $params);
+        $collection = $this->getApiRecords($params);
         return $collection->getMembers()[0];
     }
 
@@ -164,7 +201,7 @@ abstract class BaseApiRepository implements LoggerAwareInterface
             $uri = $uri . '&' . http_build_query($params);
         }
         try {
-            if (!$this->context->isProduction()) {
+            if ($this->context->isDevelopment() || $this->context->isTesting()) {
                 $this->logger->info('API Call: ' . $uri);
             }
             return $this->client->request(static::HTTP_METHOD, $uri);

+ 3 - 3
ot_core/Classes/Domain/Repository/DonorRepository.php

@@ -7,7 +7,7 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 
 class DonorRepository extends BaseApiRepository
 {
-    CONST URI = BaseApiRepository::BASE_URI . 'public/donors';
+    const URI_TRAILING_PART = 'public/donors';
     const HYDRA_TYPE = 'PortailDonor';
 
     /**
@@ -23,7 +23,7 @@ class DonorRepository extends BaseApiRepository
         $params['organizationId'] = $organizationId;
         $params['page'] = $page;
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**
@@ -40,7 +40,7 @@ class DonorRepository extends BaseApiRepository
         $params['parent'] = 1;
         $params['page'] = $page;
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**

+ 7 - 7
ot_core/Classes/Domain/Repository/EventRepository.php

@@ -8,19 +8,19 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 
 class EventRepository extends BaseApiRepository
 {
-    CONST URI = BaseApiRepository::BASE_URI . 'public/events';
+    const URI_TRAILING_PART = 'public/events';
     const HYDRA_TYPE = 'PortailEvent';
 
     /**
      * Get the event matching the given id
      *
      * @param int $id The id of the event
-     * @return array Event
+     * @return object Event
      * @throws ApiRequestException
      */
     public function findById(int $id) {
         $params = ["filter[where][id]" => $id];
-        return $this->getApiFirstRecord($this::URI, $params);
+        return $this->getApiFirstRecord($this->getApiUri(), $params);
     }
 
     /**
@@ -55,7 +55,7 @@ class EventRepository extends BaseApiRepository
         }
         $params['filter[order][0][datetimeStart]'] = 'ASC';
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**
@@ -92,7 +92,7 @@ class EventRepository extends BaseApiRepository
 
         $params['parent'] = '1';
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**
@@ -123,7 +123,7 @@ class EventRepository extends BaseApiRepository
 
         $params['children'] = '1';
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**
@@ -141,7 +141,7 @@ class EventRepository extends BaseApiRepository
         $params['filter[order][0][datetimeStart]'] = 'ASC';
         $params = array_merge($params, $searchParams);
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**

+ 9 - 4
ot_core/Classes/Domain/Repository/MemberRepository.php

@@ -9,11 +9,16 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 
 class MemberRepository extends BaseApiRepository
 {
-    CONST URI = BaseApiRepository::BASE_URI . 'public/members';
-    CONST URI_CA = BaseApiRepository::BASE_URI . 'public/members_ca';
+    const URI_TRAILING_PART = 'public/members';
+    const URI_TRAILING_PART_CA = 'public/members_ca';
     const HYDRA_TYPE = 'PortailMemberBySpeciality';
     const HYDRA_TYPE_CA = 'PortailMemberByRole';
 
+    protected function getApiUriCa(): string
+    {
+        return $this->getApiUri(self::URI_TRAILING_PART_CA);
+    }
+
     /**
      * Get the members of the organization
      * If $only_ca is true, returns only the members onf the CA
@@ -30,9 +35,9 @@ class MemberRepository extends BaseApiRepository
         $params['itemsPerPage'] = '200';
 
         if ($only_ca) {
-            return $this->getApiRecords($this::URI_CA, $params);
+            return $this->getApiRecords($this->getApiUriCa(), $params);
         } else {
-            return $this->getApiRecords($this::URI, $params);
+            return $this->getApiRecords($this->getApiUri(), $params);
         }
     }
 

+ 5 - 5
ot_core/Classes/Domain/Repository/OrganizationRepository.php

@@ -8,7 +8,7 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 
 class OrganizationRepository extends BaseApiRepository
 {
-    CONST URI = BaseApiRepository::BASE_URI . 'public/organizations';
+    const URI_TRAILING_PART = 'public/organizations';
     const HYDRA_TYPE = 'PortailOrganization';
 
     /**
@@ -22,7 +22,7 @@ class OrganizationRepository extends BaseApiRepository
     {
         $params = [];
         $params['filter[where][id]'] = $id;
-        $organization = $this->getApiFirstRecord($this::URI, $params);
+        $organization = $this->getApiFirstRecord($params);
         if($organization == null) {
             throw new ApiRequestException('Organization with id ' . $id . ' does not exist');
         }
@@ -41,7 +41,7 @@ class OrganizationRepository extends BaseApiRepository
     {
         $params = [];
         $params['filter[where][name]'] = $name;
-        $organization = $this->getApiFirstRecord($this::URI, $params);
+        $organization = $this->getApiFirstRecord($params);
         if($organization == null) {
             throw new ApiRequestException('Organization with name "' . $name . '" does not exist');
         }
@@ -66,7 +66,7 @@ class OrganizationRepository extends BaseApiRepository
 
         $params = array_merge($params, $searchParams);
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**
@@ -80,7 +80,7 @@ class OrganizationRepository extends BaseApiRepository
         $params = [];
         $params['itemsPerPage'] = 999999;
 
-        return $this->getApiRecords($this::URI, $params);
+        return $this->getApiRecords($params);
     }
 
     /**

+ 5 - 0
ot_core/Configuration/ot_config.yaml

@@ -0,0 +1,5 @@
+
+# Variant Uri for development purposes
+api_variant_uri:
+  preprod.opentalent.fr: https://api.preprod.opentalent.fr/api/
+  local.opentalent.fr: https://local.api.opentalent.fr/api/