InstanceListAction.php 2.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

/*
 * This file is part of Anis Server.
 *
 * (c) Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace App\Action;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use App\Entity\Instance;

final class InstanceListAction extends AbstractAction
{
    /**
     * `GET`  Returns a list of all instances listed in the metamodel
     * `POST` Add a new instance
     *
     * @param  ServerRequestInterface $request  PSR-7 This object represents the HTTP request
     * @param  ResponseInterface      $response PSR-7 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
    {
        if ($request->getMethod() === OPTIONS) {
            return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        }

        if ($request->getMethod() === GET) {
            // Retrieve user with email adress
            $instances = $this->em->getRepository('App\Entity\Instance')->findAll();
            $payload = json_encode($instances);
        }

        if ($request->getMethod() === POST) {
            $parsedBody = $request->getParsedBody();

            // To work this action needs user information to update
François Agneray's avatar
François Agneray committed
48
            foreach (array('name', 'label') as $a) {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                if ($this->isEmptyField($a, $parsedBody)) {
                    throw new HttpBadRequestException(
                        $request,
                        'Param ' . $a . ' needed to add a new instance'
                    );
                }
            }

            $instance = $this->postInstance($parsedBody);
            $payload = json_encode($instance);
            $response = $response->withStatus(201);
        }

        $response->getBody()->write($payload);
        return $response;
    }

    /**
     * Add a new instance into the metamodel
     *
     * @param array $parsedBody Contains the values ​​of the new instance sent by the user
     */
    private function postInstance(array $parsedBody): Instance
    {
        $instance = new Instance($parsedBody['name'], $parsedBody['label']);
François Agneray's avatar
François Agneray committed
74
75
76
        if (!$this->isEmptyField('client_url', $parsedBody)) {
            $instance->setClientUrl($parsedBody['client_url']);
        }
77
78
79
80
81
82
83

        $this->em->persist($instance);
        $this->em->flush();

        return $instance;
    }
}