141 lines
3.9 KiB
PHP
141 lines
3.9 KiB
PHP
<?php
|
|
namespace Src\Controller;
|
|
|
|
use Src\TableGateways\ReleveGateway;
|
|
|
|
class ReleveController {
|
|
|
|
private $db;
|
|
private $requestMethod;
|
|
private $ReleveId;
|
|
|
|
private $releveGateway;
|
|
|
|
public function __construct($db, $requestMethod, $ReleveId)
|
|
{
|
|
$this->db = $db;
|
|
$this->requestMethod = $requestMethod;
|
|
$this->ReleveId = $ReleveId;
|
|
|
|
$this->releveGateway = new releveGateway($db);
|
|
}
|
|
|
|
public function processRequest()
|
|
{
|
|
switch ($this->requestMethod) {
|
|
case 'GET':
|
|
if ($this->ReleveId) {
|
|
$response = $this->getReleve($this->ReleveId);
|
|
} else {
|
|
$response = $this->getAllReleves();
|
|
};
|
|
break;
|
|
case 'POST':
|
|
$response = $this->createReleveFromRequest();
|
|
break;
|
|
case 'PUT':
|
|
$response = $this->updateReleveFromRequest($this->ReleveId);
|
|
break;
|
|
case 'DELETE':
|
|
$response = $this->deleteReleve($this->ReleveId);
|
|
break;
|
|
default:
|
|
$response = $this->unprocessableEntityResponse();
|
|
break;
|
|
}
|
|
header($response['status_code_header']);
|
|
if ($response['body']) {
|
|
echo $response['body'];
|
|
}
|
|
}
|
|
|
|
private function getAllReleves()
|
|
{
|
|
$result = $this->releveGateway->findAll();
|
|
$response['status_code_header'] = 'HTTP/1.1 200 OK';
|
|
$response['body'] = json_encode($result);
|
|
return $response;
|
|
}
|
|
|
|
private function getReleve($id)
|
|
{
|
|
$result = $this->releveGateway->find($id);
|
|
if (! $result) {
|
|
return $this->notFoundResponse();
|
|
}
|
|
$response['status_code_header'] = 'HTTP/1.1 200 OK';
|
|
$response['body'] = json_encode($result);
|
|
return $response;
|
|
}
|
|
|
|
private function createReleveFromRequest()
|
|
{
|
|
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
|
|
if (! $this->validateReleve($input)) {
|
|
return $this->unprocessableEntityResponse();
|
|
}
|
|
$this->releveGateway->insert($input);
|
|
$response['status_code_header'] = 'HTTP/1.1 201 Created';
|
|
$response['body'] = null;
|
|
return $response;
|
|
}
|
|
|
|
private function updateReleveFromRequest($id)
|
|
{
|
|
$result = $this->releveGateway->find($id);
|
|
if (! $result) {
|
|
return $this->notFoundResponse();
|
|
}
|
|
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
|
|
if (! $this->validateReleve($input)) {
|
|
return $this->unprocessableEntityResponse();
|
|
}
|
|
$this->releveGateway->update($id, $input);
|
|
$response['status_code_header'] = 'HTTP/1.1 200 OK';
|
|
$response['body'] = null;
|
|
return $response;
|
|
}
|
|
|
|
private function deleteReleve($id)
|
|
{
|
|
$result = $this->releveGateway->find($id);
|
|
if (! $result) {
|
|
return $this->notFoundResponse();
|
|
}
|
|
$this->releveGateway->delete($id);
|
|
$response['status_code_header'] = 'HTTP/1.1 200 OK';
|
|
$response['body'] = null;
|
|
return $response;
|
|
}
|
|
|
|
private function validateReleve($input)
|
|
{
|
|
if (! isset($input['num'])) {
|
|
return false;
|
|
}
|
|
if (! isset($input['dateHeure_releve'])) {
|
|
return false;
|
|
}
|
|
if (! isset($input['valeur_compteur_1'])) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private function unprocessableEntityResponse()
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Entity';
|
|
$response['body'] = json_encode([
|
|
'error' => 'Invalid input'
|
|
]);
|
|
return $response;
|
|
}
|
|
|
|
private function notFoundResponse()
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 404 Not Found';
|
|
$response['body'] = null;
|
|
return $response;
|
|
}
|
|
}
|