aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Feedback/FeedbackEntriesCreate.php
blob: 41efd01eb5a6286e6d52f1d16bf5be957894a83c (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
<?php

namespace JsonApi\Routes\Feedback;

use FeedbackElement;
use FeedbackEntry;
use InvalidArgumentException;
use User;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\FeedbackElement as FeedbackElementSchema;
use JsonApi\Schemas\FeedbackEntry as FeedbackEntrySchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Create a FeedbackEntry.
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class FeedbackEntriesCreate extends JsonApiController
{
    use RatingHelper;
    use ValidationTrait;

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

        if (!Authority::canCreateFeedbackEntry($user, $element)) {
            throw new AuthorizationFailedException();
        }

        $feedbackEntry = $this->create($user, $json);

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

    /**
     * @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 (FeedbackEntrySchema::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`.';
        }

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

        $required = ['rating'];
        foreach ($required as $attribute) {
            if (!self::arrayHas($json, 'data.attributes.' . $attribute)) {
                return 'Missing `' . $attribute . '` attribute.';
            }
        }
    }

    private function getElementFromJson(array $json): ?FeedbackElement
    {
        $relationship = FeedbackEntrySchema::REL_FEEDBACK;
        if (!$this->validateResourceObject($json, 'data.relationships.' . $relationship, FeedbackElementSchema::TYPE)) {
            return null;
        }
        $resourceId = self::arrayGet($json, 'data.relationships.' . $relationship . '.data.id');

        return FeedbackElement::find($resourceId);
    }

    private function create(User $user, array $json): FeedbackEntry
    {
        $element = $this->getElementFromJson($json);
        $entry = \FeedbackEntry::build([
            'feedback_id' => $element->getId(),
            'user_id' => $user->id,
            'rating' => $this->getRating($element, (int) self::arrayGet($json, 'data.attributes.rating')),
        ]);

        if ($element['commentable']) {
            $entry['comment'] = self::arrayGet($json, 'data.attributes.comment', '');
        }
        if ($element['anonymous_entries']) {
            $entry['anonymous'] = (int) self::arrayGet($json, 'data.attributes.anonymous', '0');
        }

        $entry->store();

        return $entry;
    }
}