aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/TaskGroupsUpdate.php
blob: 8662b71ea80330fc240022ae3f5b6df3595c09a2 (plain)
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
92
93
94
95
96
97
98
99
<?php

namespace JsonApi\Routes\Courseware;

use Courseware\TaskGroup;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use JsonApi\Routes\TimestampTrait;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\Courseware\TaskGroup as TaskGroupSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use User;

/**
 * Updates one TaskGroup.
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class TaskGroupsUpdate extends JsonApiController
{
    use TimestampTrait;
    use ValidationTrait;

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     * @param array $args
     * @return Response
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        /** @var ?\Courseware\TaskGroup $resource */
        $resource = TaskGroup::find($args['id']);
        if (!$resource) {
            throw new RecordNotFoundException();
        }
        $json = $this->validate($request, $resource);
        $user = $this->getUser($request);
        if (!Authority::canUpdateTaskGroup($user, $resource)) {
            throw new AuthorizationFailedException();
        }

        $process = $this->update($resource, $json);

        return $this->getContentResponse($process);
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     *
     * @param array $json
     * @param mixed $data
     *
     * @return string|void
     */
    protected function validateResourceDocument($json, $data)
    {
        if (!self::arrayHas($json, 'data')) {
            return 'Missing `data` member at document“s top level.';
        }
        if (TaskGroupSchema::TYPE !== self::arrayGet($json, 'data.type')) {
            return 'Invalid `type` of document“s `data`.';
        }

        if (!self::arrayHas($json, 'data.attributes.start-date')) {
            return 'Missing `start-date` attribute.';
        }
        $startDate = self::arrayGet($json, 'data.attributes.start-date');
        if (!self::isValidTimestamp($startDate)) {
            return '`start-date` is not an ISO 8601 timestamp.';
        }

        if (!self::arrayHas($json, 'data.attributes.end-date')) {
            return 'Missing `end-date` attribute.';
        }
        $endDate = self::arrayGet($json, 'data.attributes.end-date');
        if (!self::isValidTimestamp($endDate)) {
            return '`end-date` is not an ISO 8601 timestamp.';
        }

        if (self::fromISO8601($startDate) > self::fromISO8601($endDate)) {
            return '`start-date` is later than `end-date`';
        }
    }

    private function update(TaskGroup $taskGroup, array $json): TaskGroup
    {
        $startDate = self::fromISO8601(self::arrayGet($json, 'data.attributes.start-date'));
        $endDate = self::fromISO8601(self::arrayGet($json, 'data.attributes.end-date'));

        $taskGroup->start_date = $startDate->getTimestamp();
        $taskGroup->end_date = $endDate->getTimestamp();

        $taskGroup->store();

        return $taskGroup;
    }
}