aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/ContainersUpdate.php
blob: 219395d2209930a09441e6894fc5cf729a0903cb (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
<?php

namespace JsonApi\Routes\Courseware;

use Courseware\Container;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Errors\UnprocessableEntityException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\Courseware\Container as ContainerSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Update one Container.
 */
class ContainersUpdate extends JsonApiController
{
    use EditBlockAwareTrait;
    use ValidationTrait;

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!($resource = Container::find($args['id']))) {
            throw new RecordNotFoundException();
        }
        $json = $this->validate($request, $resource);
        if (!Authority::canUpdateContainer($user = $this->getUser($request), $resource)) {
            throw new AuthorizationFailedException();
        }
        $resource = $this->updateContainer($user, $resource, $json);

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

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    protected function validateResourceDocument($json, $data)
    {
        if (!self::arrayHas($json, 'data')) {
            return 'Missing `data` member at document“s top level.';
        }

        if (ContainerSchema::TYPE !== self::arrayGet($json, 'data.type')) {
            return 'Wrong `type` member of document“s `data`.';
        }

        if (!self::arrayHas($json, 'data.id')) {
            return 'Document must have an `id`.';
        }

        // TODO: Validate everything
    }

    private function updateContainer(\User $user, Container $resource, array $json): Container
    {
        return $this->updateLockedResource($user, $resource, function ($user, $resource) use ($json) {
            if ($payload = self::arrayGet($json, 'data.attributes.payload')) {
                if (!$resource->type->validatePayload((object) $payload)) {
                    throw new UnprocessableEntityException('Invalid payload for this `container-type`.');
                }
                $resource->type->setPayload($payload);
            }

            if (self::arrayHas($json, 'data.relationships.structural-element.data.id')) {
                $resource->structural_element_id = self::arrayGet(
                    $json,
                    'data.relationships.structural-element.data.id'
                );
            }

            $resource->position = $json['data']['attributes']['position'];

            $resource->editor_id = $user->id;
            $resource->store();

            return $resource;
        });
    }
}