BoardInvites   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 146
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A makeInviteCall() 0 8 1
A sendInviteByEmail() 0 9 1
A deleteInvite() 0 10 1
A sendInviteByUserId() 0 8 1
A sendInvite() 0 9 2
A ignoreInvite() 0 3 1
A invites() 0 10 2
A acceptInvite() 0 3 1
A invitesFor() 0 4 1
A requiresLoginForBoardInvites() 0 10 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\Pagination;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\ProvidersContainer;
8
9
/**
10
 * @property ProvidersContainer container
11
 */
12
trait BoardInvites
13
{
14
    use HandlesRequest;
15
16
    /**
17
     * @return string[]
18
     */
19
    protected function requiresLoginForBoardInvites()
20
    {
21
        return [
22
            'sendInvite',
23
            'sendInviteByEmail',
24
            'sendInviteByUserId',
25
            'ignoreInvite',
26
            'deleteInvite',
27
            'acceptInvite',
28
            'invites',
29
        ];
30
    }
31
32
    /**
33
     * Get boards invites
34
     * @return array
35
     */
36
    public function invites()
37
    {
38
        $data = [
39
            'current_user'  => true,
40
            'field_set_key' => 'news',
41
        ];
42
43
        $invites = $this->get(UrlBuilder::RESOURCE_BOARDS_INVITES, $data);
44
45
        return !$invites ? [] : $invites;
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $invites ? array() : $invites also could return the type true which is incompatible with the documented return type array.
Loading history...
46
    }
47
48
    /**
49
     * Get invites for a specified board
50
     *
51
     * @param string $boardId
52
     * @param int $limit
53
     * @return Pagination
54
     */
55
    public function invitesFor($boardId, $limit = Pagination::DEFAULT_LIMIT)
56
    {
57
        return $this->paginate(
0 ignored issues
show
Bug introduced by
It seems like paginate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        return $this->/** @scrutinizer ignore-call */ paginate(
Loading history...
58
            UrlBuilder::RESOURCE_BOARDS_INVITES, ['board_id' => (string) $boardId], $limit
59
        );
60
    }
61
62
    /**
63
     * @param string $boardId
64
     * @param string|array $emails
65
     * @return bool
66
     */
67
    public function sendInviteByEmail($boardId, $emails)
68
    {
69
        $emails = (array)$emails;
70
        $data = [
71
            'board_id' => $boardId,
72
            'emails'   => $emails,
73
        ];
74
75
        return $this->post(UrlBuilder::RESOURCE_CREATE_EMAIL_INVITE, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg...TE_EMAIL_INVITE, $data) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
76
    }
77
78
    /**
79
     * @param string $boardId
80
     * @param string|array $users
81
     * @return bool
82
     */
83
    public function sendInvite($boardId, $users)
84
    {
85
        $users = (array)$users;
86
87
        $isEmail = filter_var($users[0], FILTER_VALIDATE_EMAIL);
88
89
        return $isEmail ?
90
            $this->sendInviteByEmail($boardId, $users) :
91
            $this->sendInviteByUserId($boardId, $users);
92
    }
93
94
    /**
95
     * @param string $boardId
96
     * @param string|array $userIds
97
     * @return bool
98
     */
99
    public function sendInviteByUserId($boardId, $userIds)
100
    {
101
        $data = [
102
            'board_id'         => $boardId,
103
            'invited_user_ids' => (array)$userIds,
104
        ];
105
106
        return $this->post(UrlBuilder::RESOURCE_CREATE_USER_ID_INVITE, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg..._USER_ID_INVITE, $data) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
107
    }
108
109
    /**
110
     * @param string $boardId
111
     * @param string $userId
112
     * @param bool $ban
113
     * @return bool
114
     */
115
    public function deleteInvite($boardId, $userId, $ban = false)
116
    {
117
        $data = [
118
            'ban'             => $ban,
119
            'board_id'        => $boardId,
120
            'field_set_key'   => 'boardEdit',
121
            'invited_user_id' => $userId,
122
        ];
123
124
        return $this->post(UrlBuilder::RESOURCE_DELETE_INVITE, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg...E_DELETE_INVITE, $data) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
125
    }
126
127
    /**
128
     * @param string $boardId
129
     * @return bool
130
     */
131
    public function ignoreInvite($boardId)
132
    {
133
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_DELETE_INVITE);
134
    }
135
136
    /**
137
     * @param string $boardId
138
     * @return bool
139
     */
140
    public function acceptInvite($boardId)
141
    {
142
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_ACCEPT_INVITE);
143
    }
144
145
    /**
146
     * @param string $boardId
147
     * @param string $endpoint
148
     * @return bool
149
     */
150
    protected function makeInviteCall($boardId, $endpoint)
151
    {
152
        $data = [
153
            'board_id'        => $boardId,
154
            'invited_user_id' => $this->container->user->id(),
155
        ];
156
157
        return $this->post($endpoint, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post($endpoint, $data) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
158
    }
159
}
160