Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

BoardInvites::acceptInvite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Api\ProvidersContainer;
7
8
/**
9
 * @property ProvidersContainer container
10
 */
11
trait BoardInvites
12
{
13
    use HandlesRequest;
14
15
    /**
16
     * @return string[]
17
     */
18
    protected function requiresLoginForBoardInvites()
19
    {
20
        return [
21
            'sendInvite',
22
            'sendInviteByEmail',
23
            'sendInviteByUserId',
24
            'ignoreInvite',
25
            'deleteInvite',
26
            'acceptInvite',
27
            'invites',
28
        ];
29
    }
30
31
    /**
32
     * Get boards invites
33
     * @return array
34
     */
35
    public function invites()
36
    {
37
        $data = [
38
            'current_user'  => true,
39
            'field_set_key' => 'news',
40
        ];
41
42
        $invites = $this->get(UrlBuilder::RESOURCE_BOARDS_INVITES, $data);
43
44
        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...
45
    }
46
47
    /**
48
     * @param string $boardId
49
     * @param string|array $emails
50
     * @return bool
51
     */
52
    public function sendInviteByEmail($boardId, $emails)
53
    {
54
        $emails = (array)$emails;
55
        $data = [
56
            'board_id' => $boardId,
57
            'emails'   => $emails,
58
        ];
59
60
        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...
61
    }
62
63
    /**
64
     * @param string $boardId
65
     * @param string|array $users
66
     * @return bool
67
     */
68
    public function sendInvite($boardId, $users)
69
    {
70
        $users = (array)$users;
71
72
        $isEmail = filter_var($users[0], FILTER_VALIDATE_EMAIL);
73
74
        return $isEmail ?
75
            $this->sendInviteByEmail($boardId, $users) :
76
            $this->sendInviteByUserId($boardId, $users);
77
    }
78
79
    /**
80
     * @param string $boardId
81
     * @param string|array $userIds
82
     * @return bool
83
     */
84
    public function sendInviteByUserId($boardId, $userIds)
85
    {
86
        $data = [
87
            'board_id'         => $boardId,
88
            'invited_user_ids' => (array)$userIds,
89
        ];
90
91
        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...
92
    }
93
94
    /**
95
     * @param string $boardId
96
     * @param string $userId
97
     * @param bool $ban
98
     * @return bool
99
     */
100
    public function deleteInvite($boardId, $userId, $ban = false)
101
    {
102
        $data = [
103
            'ban'             => $ban,
104
            'board_id'        => $boardId,
105
            'field_set_key'   => 'boardEdit',
106
            'invited_user_id' => $userId,
107
        ];
108
109
        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...
110
    }
111
112
    /**
113
     * @param string $boardId
114
     * @return bool
115
     */
116
    public function ignoreInvite($boardId)
117
    {
118
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_DELETE_INVITE);
119
    }
120
121
    /**
122
     * @param string $boardId
123
     * @return bool
124
     */
125
    public function acceptInvite($boardId)
126
    {
127
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_ACCEPT_INVITE);
128
    }
129
130
    /**
131
     * @param string $boardId
132
     * @param string $endpoint
133
     * @return bool
134
     */
135
    protected function makeInviteCall($boardId, $endpoint)
136
    {
137
        $data = [
138
            'board_id'        => $boardId,
139
            'invited_user_id' => $this->container->user->id(),
140
        ];
141
142
        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...
143
    }
144
}
145