Skip to content

Commit

Permalink
Fix Code Style #21
Browse files Browse the repository at this point in the history
Fix BuildEntity
Add Exception #8
  • Loading branch information
bfoujols committed Mar 27, 2023
1 parent ee9d8eb commit d067885
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 50 deletions.
14 changes: 5 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -109,5 +106,4 @@ public function getLibVerion(): string
{
return self::LIBVER;
}

}
8 changes: 2 additions & 6 deletions src/Core/BuildEntiy.php → src/Core/BuildEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()


}
17 changes: 17 additions & 0 deletions src/Exception/ErrorHttpStatusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* Ce fichier fait partie du Studoo
*
* @author Benoit Foujols
*
* Pour les informations complètes sur les droits d'auteur et la licence,
* veuillez consulter le fichier LICENSE qui a été distribué avec ce code source.
*/


namespace Studoo\Api\EcoleDirecte\Exception;

class ErrorHttpStatusException extends \Exception
{
protected $message = "Erreur HTTP status";
}
17 changes: 17 additions & 0 deletions src/Exception/InvalidModelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* Ce fichier fait partie du Studoo
*
* @author Benoit Foujols
*
* Pour les informations complètes sur les droits d'auteur et la licence,
* veuillez consulter le fichier LICENSE qui a été distribué avec ce code source.
*/


namespace Studoo\Api\EcoleDirecte\Exception;

class InvalidModelException extends \Exception
{
protected $message = "Invalid model API";
}
17 changes: 17 additions & 0 deletions src/Exception/NotDataResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* Ce fichier fait partie du Studoo
*
* @author Benoit Foujols
*
* Pour les informations complètes sur les droits d'auteur et la licence,
* veuillez consulter le fichier LICENSE qui a été distribué avec ce code source.
*/


namespace Studoo\Api\EcoleDirecte\Exception;

class NotDataResponseException extends \Exception
{
protected $message = "Pas de donnée dans la réponse";
}
10 changes: 5 additions & 5 deletions src/Query/DispacherQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Studoo\Api\EcoleDirecte\Query;

use Studoo\Api\EcoleDirecte\Exception\InvalidModelException;

/**
* Cette classe permet de dispatcher les requêtes
* @package Studoo\Api\EcoleDirecte\Query
Expand All @@ -20,7 +22,7 @@ trait DispacherQuery
* Permet de dispatcher les requêtes et de retourner l'objet
* @param string $model Nom du model de l'api
* @return object Retourne l'objet de la requête
* @throws \Exception
* @throws InvalidModelException
*/
public function dispacherForModel(string $model) : object
{
Expand All @@ -30,10 +32,8 @@ public function dispacherForModel(string $model) : object
];

if (array_key_exists($model, $api)) {
return new $api[$model];
} else {
// TODO Personnaliser Exception
throw new \Exception("Model not found");
return new $api[$model]();
}
throw new InvalidModelException();
}
}
9 changes: 5 additions & 4 deletions src/Query/LoginQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

namespace Studoo\Api\EcoleDirecte\Query;

use Exception;
use Studoo\Api\EcoleDirecte\Core\BuildEntity;
use Studoo\Api\EcoleDirecte\Entity\Login;
use Studoo\Api\EcoleDirecte\Exception\NotDataResponseException;

/**
* Traitement de la requête de connexion par requête API EcoleDirecte
Expand All @@ -33,17 +34,17 @@ public function __construct()
* Retourne l'entité de la requête API
* @param array $data
* @return object
* @throws NotDataResponseException
*/
public function buildEntity(array $data): object
{
$login = new Login();
$login->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;
Expand Down
12 changes: 4 additions & 8 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?php
/*
* Ce fichier fait partie du ecole-directe-api.
* Ce fichier fait partie du Studoo.
*
* (c) redbull
* (c) Benoit Foujols
*
* Pour les informations complètes sur les droits d'auteur et la licence,
* veuillez consulter le fichier LICENSE qui a été distribué avec ce code source.
*/


namespace Studoo\Api\EcoleDirecte\Query;

use Psr\Http\Message\ResponseInterface;
use Studoo\Api\EcoleDirecte\Core\BuildEntiy;

class Query
{
use BuildEntiy;

protected string $methode;

protected string $path;
Expand Down Expand Up @@ -48,13 +44,13 @@ public function getPath(): string
public function setParamToPath(array $pathID): Query
{
foreach ($pathID as $search => $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
Expand Down
16 changes: 9 additions & 7 deletions src/Query/RunQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand All @@ -52,17 +52,19 @@ public function __construct(string $model, array $config)
* @return object
* @throws GuzzleException
* @throws JsonException
* @throws ErrorHttpStatusException
*/
public function run(
array $body = [],
array $headers = [
'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 = "{}";

Expand All @@ -76,7 +78,7 @@ public function run(
);

if ($response->getStatusCode() !== 200) {
throw new Exception('Error');
throw new ErrorHttpStatusException();
}

$this->apiModel->setrawSource($response);
Expand Down
8 changes: 5 additions & 3 deletions src/Query/ViescolaireQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Service/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Studoo\Api\EcoleDirecte\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions tests/Query/LoginQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down

0 comments on commit d067885

Please sign in to comment.