Commit e2d4e872 authored by François Agneray's avatar François Agneray
Browse files

Ajout des routes de gestion des instances

parent 7441415a
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Instance;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Admin\Instance;
final class InstanceAction
{
use ActionTrait;
private $logger;
private $em;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em)
{
$this->logger = $logger;
$this->em = $em;
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Instance action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
$instance = $this->em->find('App\Entity\Admin\Instance', $args['name']);
if (is_null($instance)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Instance with name ' . $args['name'] . ' is not found'
)->withStatus(404);
}
if ($request->isGet()) {
$newResponse = $response->withJson($select);
}
if ($request->isPut()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
foreach (array('name', 'label', 'path_proxy', 'dev_mode', 'driver', 'path', 'host', 'port', 'dbname', 'login', 'password') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $a . ' needed to add a new instance'
);
}
}
$this->editInstance($instance, $parsedBody);
$newResponse = $response->withJson($instance);
}
if ($request->isDelete()) {
$name = $instance->getName();
$this->em->remove($instance);
$this->em->flush();
$newResponse = $response->withJson(array('message' => 'Instance with name ' . $name . ' is removed!'));
}
return $newResponse;
}
private function editInstance(Instance $instance, array $parsedBody): void
{
$instance->setName($parsedBody['name']);
$instance->setLabel($parsedBody['label']);
$instance->setPathProxy($parsedBody['path_proxy']);
$instance->setDevMode($parsedBody['dev_mode']);
$instance->setDriver($parsedBody['driver']);
$instance->setPath($parsedBody['path']);
$instance->setHost($parsedBody['host']);
$instance->setPort($parsedBody['port']);
$instance->setDbName($parsedBody['dbname']);
$instance->setLogin($parsedBody['login']);
$instance->setPassword($parsedBody['password']);
$this->em->flush();
}
}
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Instance;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Admin\Instance;
final class InstanceListAction
{
use ActionTrait;
private $logger;
private $em;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em)
{
$this->logger = $logger;
$this->em = $em;
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Instance list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->isGet()) {
$instances = $this->em->getRepository('App\Entity\Admin\Instance')->findAll();
$newResponse = $response->withJson($instances);
}
if ($request->isPost()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
foreach (array('name', 'label', 'path_proxy', 'dev_mode', 'driver', 'path', 'host', 'port', 'dbname', 'login', 'password') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $a . ' needed to add a new instance'
);
}
}
$instance = $this->postInstance($parsedBody);
$newResponse = $response->withJson($instance, 201);
}
return $newResponse;
}
private function postInstance(array $parsedBody): Instance
{
$instance = new Instance($parsedBody['name']);
$instance->setLabel($parsedBody['label']);
$instance->setPathProxy($parsedBody['path_proxy']);
$instance->setDevMode($parsedBody['dev_mode']);
$instance->setDriver($parsedBody['driver']);
$instance->setPath($parsedBody['path']);
$instance->setHost($parsedBody['host']);
$instance->setPort($parsedBody['port']);
$instance->setDbName($parsedBody['dbname']);
$instance->setLogin($parsedBody['login']);
$instance->setPassword($parsedBody['password']);
$this->em->persist($instance);
$this->em->flush();
return $instance;
}
}
......@@ -326,11 +326,11 @@ class Instance implements \JsonSerializable
'dev_mode' => $this->getDevMode(),
'driver' => $this->getDriver(),
'path' => $this->getPath(),
'dbhost' => $this->getHost(),
'dbport' => $this->getPort(),
'host' => $this->getHost(),
'port' => $this->getPort(),
'dbname' => $this->getDbName(),
'dblogin' => $this->getLogin(),
'dbpassword' => $this->getPassword()
'login' => $this->getLogin(),
'password' => $this->getPassword()
];
}
}
......@@ -10,7 +10,7 @@
*/
namespace App\Utils;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Admin\Instance;
......@@ -25,14 +25,14 @@ class MetaEntityManagerFactory
/**
* Doctrine Entity Manager to the anis admin database
*
* @var EntityManager
* @var EntityManagerInterface
*/
private $adminEntityManager;
/**
* Create the classe with the adminEntityManager object
*/
public function __construct(EntityManager $adminEntityManager)
public function __construct(EntityManagerInterface $adminEntityManager)
{
$this->adminEntityManager = $adminEntityManager;
}
......@@ -43,7 +43,7 @@ class MetaEntityManagerFactory
* @param string $instanceName Instance name necessary to find the correct metamodel database
* @return EntityManager
*/
public function getMetaEntityManager(string $instanceName): EntityManager
public function getMetaEntityManager(string $instanceName): EntityManagerInterface
{
// Get instance information from anis admin database
$instance = $this->adminEntityManager->find('App\Entity\Admin\Instance', $instanceName);
......
......@@ -28,6 +28,9 @@ $app->group('/settings', function (App $app) {
$app->map(['OPTIONS', 'GET', 'PUT', 'DELETE'], '/option/{id}', App\Action\Settings\OptionAction::class);
});
$app->map(['OPTIONS', 'GET', 'POST'], '/instance', App\Action\Instance\InstanceListAction::class);
$app->map(['OPTIONS', 'GET', 'PUT', 'DELETE'], '/instance/{name}', App\Action\Instance\InstanceAction::class);
$app->group('/{instance}', function (App $app) {
$app->group('/metadata', function (App $app) {
$app->map(['OPTIONS', 'GET', 'POST'], '/database', App\Action\Meta\DatabaseListAction::class);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment