diff --git a/clover.xml b/clover.xml new file mode 100644 index 0000000..f7f45af --- /dev/null +++ b/clover.xml @@ -0,0 +1,313 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index 826f141..a7c351c 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "start": [ "Composer\\Config::disableProcessTimeout", "php -S localhost:8042 -t examples" ], - "test": "php vendor/bin/phpunit --testdox tests", + "test": "php vendor/bin/phpunit --testdox tests --coverage-clover clover.xml", "docker-start": "docker compose up -d", "docker-stop": "docker compose down" } diff --git a/src/Client.php b/src/Client.php index 9bc4adf..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; /** @@ -27,8 +27,16 @@ class Client private const API_VERSION = 'v3'; - private array $config = []; + /** + * Configuration de l'API + * @var array + */ + private array $config; + /** + * Objet Login contenant les informations de connexion et de l'utilisateur + * @var Login + */ private Login $login; @@ -50,13 +58,13 @@ public function __construct(array $config = []) ], ], $config); } + //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 { @@ -73,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 { @@ -99,5 +106,4 @@ public function getLibVerion(): string { return self::LIBVER; } - } diff --git a/src/Core/BuildEntiy.php b/src/Core/BuildEntity.php similarity index 72% rename from src/Core/BuildEntiy.php rename to src/Core/BuildEntity.php index 236940b..f183dcb 100644 --- a/src/Core/BuildEntiy.php +++ b/src/Core/BuildEntity.php @@ -15,24 +15,23 @@ * Traitement d'une entité * @package Studoo\Api\EcoleDirecte\Core */ -trait BuildEntiy +class BuildEntity { /** * Rempli l'entité avec les données d'un tableau - * @param object $entity - * @param array $data + * @param object $entity Classe de l'entité + * @param array $data Tableau de correspondance entre les données et les méthodes * @return object */ public static function hasPacked(object $entity, array $data): object { foreach ($data as $key => $value) { - $method = 'set' . ucfirst($key); - if (method_exists($entity, $method)) { + $method = "set" . ucfirst($key); + if (method_exists($entity, $method) === true) { $entity->$method($value); } } return $entity; } - } diff --git a/src/Entity/Login.php b/src/Entity/Login.php index 764177f..5aa8e25 100644 --- a/src/Entity/Login.php +++ b/src/Entity/Login.php @@ -59,10 +59,15 @@ class Login private string $email; /** - * Nom et Prenom de l'utilisateur + * Nom et de l'utilisateur * @var string */ private string $nom; + + /** + * Prenom de l'utilisateur + * @var string + */ private string $prenom; /** @@ -83,7 +88,6 @@ class Login */ private array $profile; - public function getToken(): string { return $this->token; diff --git a/src/Entity/Viescolaire.php b/src/Entity/Viescolaire.php index bc0cc63..872a11a 100644 --- a/src/Entity/Viescolaire.php +++ b/src/Entity/Viescolaire.php @@ -31,7 +31,6 @@ class Viescolaire */ private array $parametrage; - /** * Retourne la liste des absences et retards * @return array 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 4cef2c9..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; /** @@ -19,13 +21,20 @@ class Request { private string $basePath; + private string $version; + private int $timeout; + private int $connectTimeout; + private bool $verify; + private bool $debug; + private array $headers; + public function __construct(array $config = []) { $this->basePath = $config['base_path']; @@ -38,33 +47,32 @@ 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, string $path, - array $query = ['body' => 'data={}', + array $query = [ + 'body' => 'data={}', 'headers' => [ 'Content-Type' => 'text/plain', ] ] - ): ResponseInterface - { + ): ResponseInterface { $this->headers = array_merge($this->headers, $query['headers']); - $client = new \GuzzleHttp\Client([ - 'base_uri' => $this->basePath . '/' . $this->version . '/', - 'timeout' => $this->timeout, + $client = new Client([ + 'base_uri' => $this->basePath . '/' . $this->version . '/', + 'timeout' => $this->timeout, 'connect_timeout' => $this->connectTimeout, - 'verify' => $this->verify, - 'debug' => $this->debug, - 'headers' => $this->headers, + 'verify' => $this->verify, + 'debug' => $this->debug, + 'headers' => $this->headers, ]); return $client->request($methode, $path, $query); 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()