* (c) Chrystel Moreau * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace App\Action\Meta; use Psr\Log\LoggerInterface; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use App\Utils\ActionTrait; use App\Utils\MetaEntityManagerFactory; use App\Entity\Metamodel\Dataset; use App\Entity\Metamodel\DatasetFamily; /** * Route: /metadata/dataset/{name} * {name}: Dataset name * * This action is used to manage one dataset * * @author François Agneray * @package App\Action\Meta */ final class DatasetAction { use ActionTrait; /** * The logger interface is the central access point to log anis information * * @var LoggerInterface */ private $logger; /** * The MetaEntityManagerFactory create the doctrine entity manager for the metamodel database. * The entity manager is the central access point to Doctrine ORM functionality (metadata database) * * @var MetaEntityManagerFactory */ private $memf; /** * Create the classe before call __invoke to execute the action * * @param LoggerInterface $logger * @param MetaEntityManagerFactory $memf */ public function __construct(LoggerInterface $logger, MetaEntityManagerFactory $memf) { $this->logger = $logger; $this->memf = $memf; } /** * `GET` Returns the dataset found * `PUT` Full update the dataset and returns the new version * `DELETE` Delete the dataset found and return a confirmation message * * @param ServerRequestInterface $request This object contains the HTTP request * @param ResponseInterface $response This object represents the HTTP response * @param string[] $args This table contains information transmitted in the URL (see routes.php) * * @return ResponseInterface */ public function __invoke(Request $request, Response $response, array $args): Response { $this->logger->info('Dataset action dispatched'); if ($request->isOptions()) { return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS'); } // Create the doctrine Entity Manager object for the instance metamodel database $this->memf->createMetaEntityManager($args['instance']); // Search the correct dataset with primary key $dataset = $this->memf->getMetaEntityManager()->find('App\Entity\Metamodel\Dataset', $args['name']); // If dataset is not found 404 if (is_null($dataset)) { return $this->dispatchHttpError( $response, 'Invalid request', 'Dataset with name ' . $args['name'] . ' is not found' )->withStatus(404); } if ($request->isGet()) { $newResponse = $response->withJson($dataset); } if ($request->isPut()) { $parsedBody = $request->getParsedBody(); // If mandatories empty fields 400 $fields = array( 'label', 'description', 'display', 'count', 'vo', 'data_path', 'selectable_row', 'id_dataset_family' ); foreach ($fields as $a) { if ($this->isEmptyField($a, $parsedBody)) { return $this->dispatchHttpError( $response, 'Invalid request', 'Param ' . $a . ' needed to edit the dataset' ); } } // Search the correct dataset family with primary key $family = $this->memf->getMetaEntityManager()->find( 'App\Entity\Metamodel\DatasetFamily', $parsedBody['id_dataset_family'] ); // Dataset family is mandatory. If is null 404 if (is_null($family)) { return $this->dispatchHttpError( $response, 'Invalid request', 'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found' )->withStatus(404); } $this->editDataset($dataset, $parsedBody, $family); $newResponse = $response->withJson($dataset); } if ($request->isDelete()) { $name = $dataset->getName(); $this->memf->getMetaEntityManager()->remove($dataset); $this->memf->getMetaEntityManager()->flush(); $newResponse = $response->withJson(array('message' => 'Dataset with name ' . $name . ' is removed!')); } return $newResponse; } /** * Update dataset object with setters * * @param Dataset $dataset The dataset to update * @param string[] $parsedBody Contains the new values ​​of the dataset sent by the user * @param DatasetFamily $family Contains the dataset family doctrine object */ private function editDataset(Dataset $dataset, array $parsedBody, DatasetFamily $family): void { $dataset->setLabel($parsedBody['label']); $dataset->setDescription($parsedBody['description']); $dataset->setDatasetFamily($family); $dataset->setDisplay($parsedBody['display']); $dataset->setCount($parsedBody['count']); $dataset->setVo($parsedBody['vo']); $dataset->setDataPath($parsedBody['data_path']); $dataset->setSelectableRow($parsedBody['selectable_row']); $this->memf->getMetaEntityManager()->flush(); } }