aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/PeerReview/ReviewsCreate.php
blob: 414f2b441e14f2a6916010d61368a9f3f4c94262 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

namespace JsonApi\Routes\Courseware\PeerReview;

use Courseware\PeerReview;
use Courseware\PeerReviewProcess;
use InvalidArgumentException;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\JsonApiController;
use JsonApi\Routes\Courseware\Authority;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\Courseware\PeerReview as PeerReviewSchema;
use JsonApi\Schemas\Courseware\PeerReviewProcess as PeerReviewProcessSchema;
use JsonApi\Schemas\StatusGroup as StatusGroupSchema;
use JsonApi\Schemas\User as UserSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Statusgruppen;
use User;

/**
 * Create a PeerReview.
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class ReviewsCreate extends JsonApiController
{
    use ValidationTrait;

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     *
     * @param array $args
     *
     * @return Response
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $json = $this->validate($request);
        $process = $this->getProcessFromJson($json);
        $user = $this->getUser($request);

        if (!Authority::canCreatePeerReviews($user, $process)) {
            throw new AuthorizationFailedException();
        }

        $resource = $this->create($json);

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

    /**
     * @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 (PeerReviewSchema::TYPE !== self::arrayGet($json, 'data.type')) {
            return 'Invalid `type` of document“s `data`.';
        }
        if (self::arrayHas($json, 'data.id')) {
            return 'New document must not have an `id`.';
        }

        // process
        if (!self::arrayHas($json, 'data.relationships.process')) {
            return 'Missing `process` relationship.';
        }
        if (!$this->getProcessFromJson($json)) {
            return 'Invalid `process` relationship.';
        }

        // submitter
        if (!self::arrayHas($json, 'data.relationships.submitter')) {
            return 'Missing `submitter` relationship.';
        }
        if (!$this->getSubmitterFromJson($json)) {
            return 'Invalid `submitter` relationship.';
        }

        // reviewer
        if (!self::arrayHas($json, 'data.relationships.reviewer')) {
            return 'Missing `reviewer` relationship.';
        }
        if (!$this->getReviewerFromJson($json)) {
            return 'Invalid `reviewer` relationship.';
        }
    }

    private function create(array $json): PeerReview
    {
        $process = $this->getProcessFromJson($json);
        $reviewer = $this->getReviewerFromJson($json);
        $submitter = $this->getSubmitterFromJson($json);

        $task = $process['task_group']->findTaskBySolver($submitter);
        $reviewerType = $this->getReviewerType($reviewer);

        $review = PeerReview::create([
            'process_id' => $process->id,
            'task_id' => $task->id,
            'submitter_id' => $submitter->id,
            'reviewer_id' => $reviewer->id,
            'reviewer_type' => $reviewerType,
        ]);

        return $review;
    }

    /**
     * @return User|Statusgruppen|null
     */
    private function getActorFromJson(array $json, string $relation)
    {
        $relationship = 'data.relationships.' . $relation;
        if (
            !(
                $this->validateResourceObject($json, $relationship, UserSchema::TYPE)
                || $this->validateResourceObject($json, $relationship, StatusGroupSchema::TYPE)
            )
        ) {
            return null;
        }
        $resourceId = self::arrayGet($json, $relationship . '.data.id');

        switch (self::arrayGet($json, $relationship . '.data.type')) {
            case UserSchema::TYPE:
                return User::find($resourceId);
            case StatusGroupSchema::TYPE:
                return Statusgruppen::find($resourceId);
        }

        throw new InvalidArgumentException();
    }

    private function getProcessFromJson(array $json): ?PeerReviewProcess
    {
        if (!$this->validateResourceObject($json, 'data.relationships.process', PeerReviewProcessSchema::TYPE)) {
            return null;
        }
        $resourceId = self::arrayGet($json, 'data.relationships.process.data.id');

        return PeerReviewProcess::find($resourceId);
    }

    /**
     * @return User|Statusgruppen|null
     */
    private function getReviewerFromJson(array $json)
    {
        return $this->getActorFromJson($json, 'reviewer');
    }

    private function getReviewerType($reviewer): string
    {
        if ($reviewer instanceof User) {
            return 'autor';
        }
        if ($reviewer instanceof Statusgruppen) {
            return 'group';
        }

        throw new InvalidArgumentException();
    }

    /**
     * @return User|Statusgruppen|null
     */
    private function getSubmitterFromJson(array $json)
    {
        return $this->getActorFromJson($json, 'submitter');
    }
}