DatasetAction.php 5.88 KB
Newer Older
1
<?php declare(strict_types=1);
François Agneray's avatar
François Agneray committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * 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\Meta;

use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

17
use App\Utils\ActionTrait;
18
19
20
use App\Utils\MetaEntityManagerFactory;
use App\Entity\Metamodel\Dataset;
use App\Entity\Metamodel\DatasetFamily;
François Agneray's avatar
François Agneray committed
21

François Agneray's avatar
François Agneray committed
22
23
24
/**
 * Route: /metadata/dataset/{name}
 * {name}: Dataset name
25
 *
François Agneray's avatar
François Agneray committed
26
27
28
29
30
 * This action is used to manage one dataset
 *
 * @author François Agneray <francois.agneray@lam.fr>
 * @package App\Action\Meta
 */
31
final class DatasetAction
François Agneray's avatar
François Agneray committed
32
{
33
34
    use ActionTrait;

François Agneray's avatar
François Agneray committed
35
36
37
38
39
    /**
     * The logger interface is the central access point to log anis information
     *
     * @var LoggerInterface
     */
40
    private $logger;
François Agneray's avatar
François Agneray committed
41
42

    /**
43
44
     * 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)
François Agneray's avatar
François Agneray committed
45
     *
46
     * @var MetaEntityManagerFactory
François Agneray's avatar
François Agneray committed
47
     */
48
    private $memf;
François Agneray's avatar
François Agneray committed
49
    
François Agneray's avatar
François Agneray committed
50
51
52
    /**
     * Create the classe before call __invoke to execute the action
     *
53
54
     * @param LoggerInterface          $logger
     * @param MetaEntityManagerFactory $memf
François Agneray's avatar
François Agneray committed
55
     */
56
    public function __construct(LoggerInterface $logger, MetaEntityManagerFactory $memf)
François Agneray's avatar
François Agneray committed
57
    {
58
        $this->logger = $logger;
59
        $this->memf = $memf;
François Agneray's avatar
François Agneray committed
60
61
    }

François Agneray's avatar
François Agneray committed
62
63
64
65
66
67
68
69
70
71
72
    /**
     * `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
     */
François Agneray's avatar
François Agneray committed
73
74
75
76
77
78
79
80
    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');
        }

81
82
83
        // Create the doctrine Entity Manager object for the instance metamodel database
        $this->memf->createMetaEntityManager($args['instance']);

François Agneray's avatar
François Agneray committed
84
        // Search the correct dataset with primary key
85
        $dataset = $this->memf->getMetaEntityManager()->find('App\Entity\Metamodel\Dataset', $args['name']);
François Agneray's avatar
François Agneray committed
86

François Agneray's avatar
François Agneray committed
87
        // If dataset is not found 404
François Agneray's avatar
François Agneray committed
88
89
90
91
        if (is_null($dataset)) {
            return $this->dispatchHttpError(
                $response,
                'Invalid request',
92
                'Dataset with name ' . $args['name'] . ' is not found'
François Agneray's avatar
François Agneray committed
93
94
95
96
            )->withStatus(404);
        }

        if ($request->isGet()) {
François Agneray's avatar
François Agneray committed
97
            $newResponse = $response->withJson($dataset);
François Agneray's avatar
François Agneray committed
98
99
100
101
102
        }
        
        if ($request->isPut()) {
            $parsedBody = $request->getParsedBody();

François Agneray's avatar
François Agneray committed
103
            // If mandatories empty fields 400
François Agneray's avatar
François Agneray committed
104
105
106
107
108
109
110
            $fields = array(
                'label',
                'description',
                'display',
                'count',
                'vo',
                'data_path',
François Agneray's avatar
François Agneray committed
111
                'selectable_row',
François Agneray's avatar
François Agneray committed
112
113
114
                'id_dataset_family'
            );
            foreach ($fields as $a) {
François Agneray's avatar
François Agneray committed
115
116
117
118
                if ($this->isEmptyField($a, $parsedBody)) {
                    return $this->dispatchHttpError(
                        $response,
                        'Invalid request',
119
                        'Param ' . $a . ' needed to edit the dataset'
François Agneray's avatar
François Agneray committed
120
121
122
123
                    );
                }
            }

François Agneray's avatar
François Agneray committed
124
            // Search the correct dataset family with primary key
125
126
127
128
            $family = $this->memf->getMetaEntityManager()->find(
                'App\Entity\Metamodel\DatasetFamily',
                $parsedBody['id_dataset_family']
            );
François Agneray's avatar
François Agneray committed
129
130

            // Dataset family is mandatory. If is null 404
François Agneray's avatar
François Agneray committed
131
132
133
134
            if (is_null($family)) {
                return $this->dispatchHttpError(
                    $response,
                    'Invalid request',
135
                    'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found'
François Agneray's avatar
François Agneray committed
136
137
138
139
                )->withStatus(404);
            }
            
            $this->editDataset($dataset, $parsedBody, $family);
François Agneray's avatar
François Agneray committed
140
            $newResponse = $response->withJson($dataset);
François Agneray's avatar
François Agneray committed
141
142
143
144
        }
        
        if ($request->isDelete()) {
            $name = $dataset->getName();
145
146
            $this->memf->getMetaEntityManager()->remove($dataset);
            $this->memf->getMetaEntityManager()->flush();
147
            $newResponse = $response->withJson(array('message' => 'Dataset with name ' . $name . ' is removed!'));
François Agneray's avatar
François Agneray committed
148
149
150
151
152
        }

        return $newResponse;
    }

François Agneray's avatar
François Agneray committed
153
154
155
156
157
158
159
    /**
     * 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
     */
François Agneray's avatar
François Agneray committed
160
161
162
163
164
165
166
167
168
    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']);
François Agneray's avatar
François Agneray committed
169
        $dataset->setSelectableRow($parsedBody['selectable_row']);
François Agneray's avatar
François Agneray committed
170

171
        $this->memf->getMetaEntityManager()->flush();
François Agneray's avatar
François Agneray committed
172
173
    }
}