From d067885e6c93055c7992ee2674035efd292c7b45 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 16:22:06 +0200 Subject: [PATCH] Fix Code Style #21 Fix BuildEntity Add Exception #8 --- src/Client.php | 14 +++++--------- src/Core/{BuildEntiy.php => BuildEntity.php} | 8 ++------ src/Exception/ErrorHttpStatusException.php | 17 +++++++++++++++++ src/Exception/InvalidModelException.php | 17 +++++++++++++++++ src/Exception/NotDataResponseException.php | 17 +++++++++++++++++ src/Query/DispacherQuery.php | 10 +++++----- src/Query/LoginQuery.php | 9 +++++---- src/Query/Query.php | 12 ++++-------- src/Query/RunQuery.php | 16 +++++++++------- src/Query/ViescolaireQuery.php | 8 +++++--- src/Service/Request.php | 12 ++++++------ tests/Query/LoginQueryTest.php | 9 +++++++-- 12 files changed, 99 insertions(+), 50 deletions(-) rename src/Core/{BuildEntiy.php => BuildEntity.php} (91%) create mode 100644 src/Exception/ErrorHttpStatusException.php create mode 100644 src/Exception/InvalidModelException.php create mode 100644 src/Exception/NotDataResponseException.php diff --git a/src/Client.php b/src/Client.php index c943809..f745dbe 100644 --- a/src/Client.php +++ b/src/Client.php @@ -11,8 +11,8 @@ namespace Studoo\Api\EcoleDirecte; -use GuzzleHttp\Exception\GuzzleException; use Studoo\Api\EcoleDirecte\Entity\Login; +use Studoo\Api\EcoleDirecte\Exception\InvalidModelException; use Studoo\Api\EcoleDirecte\Query\RunQuery; /** @@ -57,16 +57,14 @@ public function __construct(array $config = []) 'Content-Type' => 'text/plain', ], ], $config); - - - } //end __construct() + } + //end __construct() /** * Accès à l'API EcoleDirecte avec les identifiants de l'utilisateur * Retourne un objet Login * @return object - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException + * @throws InvalidModelException */ public function fetchAccessToken(): object { @@ -83,8 +81,7 @@ public function fetchAccessToken(): object * Retourne les informations de l'utilisateur sur sa vie scolaire * @param int $idEtudiant Identifiant de l'étudiant * @return object - * @throws GuzzleException - * @throws \JsonException + * @throws InvalidModelException */ public function getVieScolaire(int $idEtudiant): object { @@ -109,5 +106,4 @@ public function getLibVerion(): string { return self::LIBVER; } - } diff --git a/src/Core/BuildEntiy.php b/src/Core/BuildEntity.php similarity index 91% rename from src/Core/BuildEntiy.php rename to src/Core/BuildEntity.php index ae13c14..f183dcb 100644 --- a/src/Core/BuildEntiy.php +++ b/src/Core/BuildEntity.php @@ -15,7 +15,7 @@ * Traitement d'une entité * @package Studoo\Api\EcoleDirecte\Core */ -trait BuildEntiy +class BuildEntity { /** * Rempli l'entité avec les données d'un tableau @@ -26,16 +26,12 @@ trait BuildEntiy public static function hasPacked(object $entity, array $data): object { foreach ($data as $key => $value) { - $method = "set{ucfirst($key)}"; + $method = "set" . ucfirst($key); if (method_exists($entity, $method) === true) { $entity->$method($value); } } return $entity; - } - //end hasPacked() - - } diff --git a/src/Exception/ErrorHttpStatusException.php b/src/Exception/ErrorHttpStatusException.php new file mode 100644 index 0000000..a799f2f --- /dev/null +++ b/src/Exception/ErrorHttpStatusException.php @@ -0,0 +1,17 @@ +setToken($data['token']); if (isset($data['data']['accounts'][0]) === true) { - self::hasPacked($login, $data['data']['accounts'][0]); + BuildEntity::hasPacked($login, $data['data']['accounts'][0]); } else { - // TODO: Throw an exception - throw new Exception('Aucune donnée n\'a été trouvée'); + throw new NotDataResponseException(); } return $login; diff --git a/src/Query/Query.php b/src/Query/Query.php index 24b2396..9a0ad9f 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -1,23 +1,19 @@ $replace) { - $this->path = str_replace("<{$search}>", $replace, $this->path); + $this->path = str_replace("<$search>", $replace, $this->path); } return $this; } /** - * retourne les paramètres de la requête + * Retourne les paramètres de la requête * @return array */ public function getQuery(): array diff --git a/src/Query/RunQuery.php b/src/Query/RunQuery.php index 4902f9d..56efe55 100644 --- a/src/Query/RunQuery.php +++ b/src/Query/RunQuery.php @@ -11,10 +11,11 @@ namespace Studoo\Api\EcoleDirecte\Query; -use Exception; use GuzzleHttp\Exception\GuzzleException; use JsonException; use Psr\Http\Message\ResponseInterface; +use Studoo\Api\EcoleDirecte\Exception\ErrorHttpStatusException; +use Studoo\Api\EcoleDirecte\Exception\InvalidModelException; use Studoo\Api\EcoleDirecte\Service\Request; /** @@ -34,12 +35,11 @@ class RunQuery * BuildQuery constructor. * @param string $model Nom d'appel API * @param array $config Configuration de l'API - * @throws Exception + * @throws InvalidModelException */ public function __construct(string $model, array $config) { $finalModel = $this->dispacherForModel($model); - // TODO Mettre un try catch pour la gestion d'erreur $this->apiModel = new $finalModel(); $this->config = $config; } @@ -52,6 +52,7 @@ public function __construct(string $model, array $config) * @return object * @throws GuzzleException * @throws JsonException + * @throws ErrorHttpStatusException */ public function run( array $body = [], @@ -59,10 +60,11 @@ public function run( 'Content-Type' => 'text/plain', ], array $param = [] - ): object - { + ): object { // Add pathID to path ('pathID' => []) - (isset($param['pathID'])) ? $this->apiModel->setParamToPath($param['pathID']) : null; + if (isset($param['pathID'])) { + $this->apiModel->setParamToPath($param['pathID']); + } // Fix body si vide (isset($body)) ? $bodyReponse = json_encode($body, JSON_THROW_ON_ERROR) : $bodyReponse = "{}"; @@ -76,7 +78,7 @@ public function run( ); if ($response->getStatusCode() !== 200) { - throw new Exception('Error'); + throw new ErrorHttpStatusException(); } $this->apiModel->setrawSource($response); diff --git a/src/Query/ViescolaireQuery.php b/src/Query/ViescolaireQuery.php index c24d49e..5239008 100644 --- a/src/Query/ViescolaireQuery.php +++ b/src/Query/ViescolaireQuery.php @@ -10,7 +10,9 @@ namespace Studoo\Api\EcoleDirecte\Query; +use Studoo\Api\EcoleDirecte\Core\BuildEntity; use Studoo\Api\EcoleDirecte\Entity\Viescolaire; +use Studoo\Api\EcoleDirecte\Exception\NotDataResponseException; /** * Traitement de la requête sur la vie scolaire par requête API EcoleDirecte @@ -29,16 +31,16 @@ public function __construct() * Retourne l'entité de la requête API * @param array $data * @return object + * @throws NotDataResponseException */ public function buildEntity(array $data): object { $vieScolaire = new Viescolaire(); if (isset($data['data']) === true) { - self::hasPacked($vieScolaire, $data['data']); + BuildEntity::hasPacked($vieScolaire, $data['data']); } else { - // TODO: Throw an exception - throw new \Exception('Aucune donnée n\'a été trouvée'); + throw new NotDataResponseException(); } return $vieScolaire; } diff --git a/src/Service/Request.php b/src/Service/Request.php index efc70e2..5f4740e 100644 --- a/src/Service/Request.php +++ b/src/Service/Request.php @@ -10,6 +10,8 @@ namespace Studoo\Api\EcoleDirecte\Service; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Psr\Http\Message\ResponseInterface; /** @@ -45,13 +47,12 @@ public function __construct(array $config = []) } /** - * Requete vers l'API + * Request vers l'API * @param string $methode Method of the request (GET, POST, PUT, DELETE) * @param string $path Path of the request (ex: 'v3/eleves/123456789') * @param array $query Query of the request (ex: ['body' => 'data=', 'headers' => ['Content-Type' => 'text/plain']]) * @return ResponseInterface - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException + * @throws GuzzleException */ public function query( string $methode, @@ -62,11 +63,10 @@ public function query( 'Content-Type' => 'text/plain', ] ] - ): ResponseInterface - { + ): ResponseInterface { $this->headers = array_merge($this->headers, $query['headers']); - $client = new \GuzzleHttp\Client([ + $client = new Client([ 'base_uri' => $this->basePath . '/' . $this->version . '/', 'timeout' => $this->timeout, 'connect_timeout' => $this->connectTimeout, diff --git a/tests/Query/LoginQueryTest.php b/tests/Query/LoginQueryTest.php index 68903c9..03833c5 100644 --- a/tests/Query/LoginQueryTest.php +++ b/tests/Query/LoginQueryTest.php @@ -2,8 +2,8 @@ namespace API; -use Studoo\Api\EcoleDirecte\Query\LoginQuery; use PHPUnit\Framework\TestCase; +use Studoo\Api\EcoleDirecte\Query\LoginQuery; class LoginQueryTest extends TestCase { @@ -13,7 +13,12 @@ class LoginQueryTest extends TestCase public function setUp(): void { $this->loginQuery = new LoginQuery(); - $this->jsonContent = json_decode(file_get_contents(__DIR__ . '/../Data/loginV3TypeP.json'), true); + $this->jsonContent = json_decode( + file_get_contents(__DIR__ . '/../Data/loginV3TypeP.json'), + true, + 512, + JSON_THROW_ON_ERROR + ); } public function testGetQuery()