api.ampere/src/Controller/CompteurController.php

135 lines
3.8 KiB
PHP

<?php
namespace Src\Controller;
use Src\TableGateways\CompteurGateway;
class CompteurController {
private $db;
private $requestMethod;
private $CompteurId;
private $compteurGateway;
public function __construct($db, $requestMethod, $CompteurId)
{
$this->db = $db;
$this->requestMethod = $requestMethod;
$this->CompteurId = $CompteurId;
$this->compteurGateway = new compteurGateway($db);
}
public function processRequest()
{
switch ($this->requestMethod) {
case 'GET':
if ($this->CompteurId) {
$response = $this->getCompteur($this->CompteurId);
} else {
$response = $this->getAllCompteurs();
};
break;
case 'POST':
$response = $this->createCompteurFromRequest();
break;
case 'PUT':
$response = $this->updateCompteurFromRequest($this->CompteurId);
break;
case 'DELETE':
$response = $this->deleteCompteur($this->CompteurId);
break;
default:
$response = $this->notFoundResponse();
break;
}
header($response['status_code_header']);
if ($response['body']) {
echo $response['body'];
}
}
private function getAllCompteurs()
{
$result = $this->compteurGateway->findAll();
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = json_encode($result);
return $response;
}
private function getCompteur($id)
{
$result = $this->compteurGateway->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 createCompteurFromRequest()
{
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validateCompteur($input)) {
return $this->unprocessableEntityResponse();
}
$this->compteurGateway->insert($input);
$response['status_code_header'] = 'HTTP/1.1 201 Created';
$response['body'] = null;
return $response;
}
private function updateCompteurFromRequest($id)
{
$result = $this->compteurGateway->find($id);
if (! $result) {
return $this->notFoundResponse();
}
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validateCompteur($input)) {
return $this->unprocessableEntityResponse();
}
$this->compteurGateway->update($id, $input);
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = null;
return $response;
}
private function deleteCompteur($id)
{
$result = $this->compteurGateway->find($id);
if (! $result) {
return $this->notFoundResponse();
}
$this->compteurGateway->delete($id);
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = null;
return $response;
}
private function validateCompteur($input)
{
if (! isset($input['libelle'])) {
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;
}
}