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
|
<?php
namespace JsonApi\Routes\Feedback;
use FeedbackElement;
use FeedbackRange;
use User;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\FeedbackElement as FeedbackElementSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
/**
* Create a FeedbackElement.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
class FeedbackElementsCreate extends JsonApiController
{
use RangeTypeAware;
use ValidationTrait;
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param array $args
*
* @return Response
*/
public function __invoke(Request $request, Response $response, $args)
{
$this->preparePossibleRangeTypes();
$json = $this->validate($request);
$range = $this->getRangeFromJson($json);
$user = $this->getUser($request);
if (!Authority::canCreateFeedbackElement($user, $range)) {
throw new AuthorizationFailedException();
}
$feedbackElement = $this->create($user, $json);
return $this->getCreatedResponse($feedbackElement);
}
/**
* @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 (FeedbackElementSchema::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`.';
}
$required = ['question', 'description', 'mode', 'results-visible', 'is-commentable', 'anonymous-entries'];
foreach ($required as $attribute) {
if (!self::arrayHas($json, 'data.attributes.' . $attribute)) {
return 'Missing `' . $attribute . '` attribute.';
}
}
if (!self::arrayHas($json, 'data.relationships.range')) {
return 'Missing `range` relationship.';
}
if (!$this->getRangeFromJson($json)) {
return 'Invalid `range` relationship.';
}
}
private function getRangeFromJson(array $json): ?FeedbackRange
{
$rangeType = self::arrayGet($json, 'data.relationships.range.data.type');
$rangeId = self::arrayGet($json, 'data.relationships.range.data.id');
if (!isset($this->possibleRangeTypes[$rangeType])) {
return null;
}
$rangeClass = $this->possibleRangeTypes[$rangeType];
return $rangeClass::find($rangeId);
}
private function create(User $user, array $json): FeedbackElement
{
$range = $this->getRangeFromJson($json);
return \FeedbackElement::create([
'range_id' => $range->getId(),
'range_type' => get_class($range),
'user_id' => $user->id,
'question' => self::arrayGet($json, 'data.attributes.question'),
'description' => self::arrayGet($json, 'data.attributes.description'),
'mode' => self::arrayGet($json, 'data.attributes.mode'),
'results_visible' => (int) self::arrayGet($json, 'data.attributes.results-visible'),
'commentable' => (int) self::arrayGet($json, 'data.attributes.is-commentable'),
'anonymous_entries' => (int) self::arrayGet($json, 'data.attributes.anonymous-entries'),
// TODO:
'course_id' => $range->getRangeCourseId(),
]);
}
}
|