Completed
Push — master ( 4a1452...eedec2 )
by Sergey
11:49 queued 08:26
created

BoardInvites::makeInviteCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
7
trait BoardInvites
8
{
9
    use HandlesRequest;
10
11
    /**
12
     * @return array
13
     */
14
    protected function requiresLoginForBoardInvites() {
15
        return [
16
            'sendInvite',
17
            'sendInviteByEmail',
18
            'sendInviteByUserId',
19
            'deleteInvite',
20
            'acceptInvite',
21
            'invites',
22
        ];
23
    }
24
25
    /**
26
     * Get boards invites
27
     * @return array
28
     */
29
    public function invites()
30
    {
31
        $data = [
32
            'current_user' => true,
33
            'field_set_key' => 'news',
34
        ];
35
36
        $invites = $this->get($data, UrlBuilder::RESOURCE_BOARDS_INVITES);
37
38
        return !$invites ? [] : $invites;
39
    }
40
41
    /**
42
     * @param string $boardId
43
     * @param string|array $emails
44
     * @return bool
45
     */
46 View Code Duplication
    public function sendInviteByEmail($boardId, $emails)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $emails = is_array($emails) ? $emails : [$emails];
49
        $data = [
50
            "board_id" => $boardId,
51
            "emails" => $emails,
52
        ];
53
54
        return $this->post($data, UrlBuilder::RESOURCE_CREATE_EMAIL_INVITE);
55
    }
56
57
    /**
58
     * @param string $boardId
59
     * @param string|array $users
60
     * @return bool
61
     */
62
    public function sendInvite($boardId, $users)
63
    {
64
        $users = is_array($users) ? $users : [$users];
65
66
        $isEmail = filter_var($users[0], FILTER_VALIDATE_EMAIL);
67
68
        return $isEmail ?
69
            $this->sendInviteByEmail($boardId, $users) :
70
            $this->sendInviteByUserId($boardId, $users);
71
    }
72
73
    /**
74
     * @param string $boardId
75
     * @param string|array $userIds
76
     * @return bool
77
     */
78 View Code Duplication
    public function sendInviteByUserId($boardId, $userIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $userIds = is_array($userIds) ? $userIds : [$userIds];
81
        $data = [
82
            "board_id"         => $boardId,
83
            "invited_user_ids" => $userIds,
84
        ];
85
86
        return $this->post($data, UrlBuilder::RESOURCE_CREATE_USER_ID_INVITE);
87
    }
88
89
    /**
90
     * @param string $boardId
91
     * @param string $userId
92
     * @param bool $ban
93
     * @return bool
94
     */
95
    public function deleteInvite($boardId, $userId, $ban = false)
96
    {
97
        $data = [
98
            'ban'             => $ban,
99
            'board_id'        => $boardId,
100
            'field_set_key'   => 'boardEdit',
101
            'invited_user_id' => $userId,
102
        ];
103
104
        return $this->post($data, UrlBuilder::RESOURCE_DELETE_INVITE);
105
    }
106
107
    /**
108
     * @param string $boardId
109
     * @return bool
110
     */
111
    public function ignoreInvite($boardId)
112
    {
113
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_DELETE_INVITE);
114
    }
115
116
    /**
117
     * @param string $boardId
118
     * @return bool
119
     */
120
    public function acceptInvite($boardId)
121
    {
122
        return $this->makeInviteCall($boardId, UrlBuilder::RESOURCE_ACCEPT_INVITE);
123
    }
124
125
    /**
126
     * @param string $boardId
127
     * @param string $endpoint
128
     * @return bool
129
     */
130
    protected function makeInviteCall($boardId, $endpoint)
131
    {
132
        $data = [
133
            'board_id'        => $boardId,
134
            'invited_user_id' => $this->container->user->id(),
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
        ];
136
137
        return $this->post($data, $endpoint);
138
    }
139
}