GroupAction.php 3.52 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
48
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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 Slim\Exception\HttpNotFoundException;
use App\Entity\Group;

final class GroupAction extends AbstractAction
{
    /**
     * `GET` Returns the Group found
     * `PUT` Full update the Group and returns the new version
     * `DELETE` Delete the Group found and return a confirmation message
     *
     * @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, PUT, DELETE, OPTIONS');
        }

        // Search the correct group with primary key
        $group = $this->em->find('App\Entity\Group', $args['id']);

        // If group is not found 404
        if (is_null($group)) {
            throw new HttpNotFoundException(
                $request,
                'Group with id ' . $args['id'] . ' is not found'
            );
        }

        if ($request->getMethod() === GET) {
            $payload = json_encode($group);
        }

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

            // If mandatories empty fields 400
            foreach (array('label') as $a) {
                if ($this->isEmptyField($a, $parsedBody)) {
                    throw new HttpBadRequestException(
                        $request,
                        'Param ' . $a . ' needed to edit the group'
                    );
                }
            }

            $this->editGroup($group, $parsedBody);
            $payload = json_encode($group);
        }

        if ($request->getMethod() === DELETE) {
            $id = $group->getId();
            $this->em->remove($group);
            $this->em->flush();
            $payload = json_encode(array('message' => 'Group with id ' . $id . ' is removed!'));
        }

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

    /**
     * Update group object with setters
     *
     * @param Group $group The group to update
     * @param array $parsedBody Contains the new values ​​of the group sent by the user
     */
    private function editGroup(Group $group, array $parsedBody): void
    {
        $group->setLabel($parsedBody['label']);
92
        $group->setDatasets($this->getDatasets($parsedBody['datasets']));
93
94
        $this->em->flush();
    }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    private function getDatasets(array $listOfDatasetsNames)
    {
        if (count($listOfDatasetsNames) < 1) {
            return array();
        }

        $in = implode(',', array_map(function ($d) {
            return "'" . $d . "'";
        }, $listOfDatasetsNames));

        $dql = 'SELECT d FROM App\Entity\Dataset d WHERE d.name IN (' . $in . ')';
        $query = $this->em->createQuery($dql);
        return $query->getResult();
    }
110
}