aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Users/Rel/Contacts.php
blob: 8006920d985250ffe2cf468c8260a26989a27128 (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
<?php

namespace JsonApi\Routes\Users\Rel;

use Psr\Http\Message\ServerRequestInterface as Request;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Routes\Users\Authority;
use JsonApi\Routes\RelationshipsController;

class Contacts extends RelationshipsController
{
    protected $allowedPagingParameters = ['offset', 'limit'];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function fetchRelationship(Request $request, $related)
    {
        $contacts = $related->contacts;
        $total = count($contacts);
        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedIdentifiersResponse(
            $contacts->limit($offset, $limit),
            $total,
            $this->getRelationshipLinks($related)
        );
    }

    protected function addToRelationship(Request $request, $related)
    {
        $json = $this->validate($request);

        foreach ($this->validateContacts($related, $json) as $contactId) {
            if (!\Contact::countBySQL('owner_id = ? AND user_id = ?', [$related->id, $contactId])) {
                \Contact::create(['owner_id' => $related->id, 'user_id' => $contactId]);
            }
        }

        return $this->getCodeResponse(204);
    }

    protected function removeFromRelationship(Request $request, $related)
    {
        $json = $this->validate($request);
        $contacts = $this->validateContacts($related, $json);
        $this->removeContacts($related, $contacts);

        return $this->getCodeResponse(204);
    }

    protected function replaceRelationship(Request $request, $related)
    {
        $json = $this->validate($request);
        $contacts = $this->validateContacts($related, $json);
        $this->replaceContacts($related, $contacts);

        return $this->getCodeResponse(204);
    }

    protected function findRelated(array $args)
    {
        if (!$user = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        return $user;
    }

    protected function authorize(Request $request, $resource)
    {
        return Authority::canEditUser($this->getUser($request), $resource);
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function getRelationshipSelfLink($resource, $schema, $userData)
    {
        return $schema->getRelationshipSelfLink($resource, 'contacts');
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function getRelationshipRelatedLink($resource, $schema, $userData)
    {
        return $schema->getRelationshipRelatedLink($resource, 'contacts');
    }

    protected function validateResourceDocument($json, $data)
    {
        if (!self::arrayHas($json, 'data')) {
            return 'Missing `data` member at document´s top level.';
        }

        $data = self::arrayGet($json, 'data');

        if (!is_array($data)) {
            return 'Document´s ´data´ must be an array.';
        }

        foreach ($data as $item) {
            if (self::arrayGet($item, 'type') !== \JsonApi\Schemas\User::TYPE) {
                return 'Wrong `type` in document´s `data`.';
            }

            if (!self::arrayGet($item, 'id')) {
                return 'Missing `id` of document´s `data`.';
            }
        }

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

    private function validateContacts(\User $user, $json)
    {
        $contacts = [];

        foreach (self::arrayGet($json, 'data') as $contactResource) {
            if (!$contact = \User::find($contactResource['id'])) {
                throw new RecordNotFoundException();
            }

            if (!Authority::canShowUser($user, $contact)) {
                throw new RecordNotFoundException();
            }

            $contacts[] = $contact->id;
        }

        return $contacts;
    }

    private function addContact(\User $user, $contact_id)
    {
        if (!\Contact::countBySQL('owner_id = ? AND user_id = ?', [$user->id, $contact_id])
        ) {
            \Contact::create(['owner_id' => $user->id, 'user_id' => $contact_id]);
        }
    }

    private function removeContacts(\User $user, array $contactIds)
    {
        $user->contacts->unsetBy('user_id', $contactIds);
        $user->store();
    }

    private function replaceContacts(\User $user, array $new_ids)
    {
        $old_ids = $user->contacts->pluck('user_id');

        $this->removeContacts($user, array_diff($old_ids, $new_ids));
        $diff = array_diff($new_ids, $old_ids);
        array_walk(
            $diff,
            function ($contactId) use ($user) {
                $this->addContact($user, $contactId);
            }
        );
    }
}