Completed
Push — master ( abdd5c...4a1452 )
by Sergey
08:14 queued 05:47
created

BoardInvites::sendInviteByEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 10
loc 10
rs 9.4285
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 View Code Duplication
    public function ignoreInvite($boardId)
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...
112
    {
113
        $data = [
114
            'board_id'        => $boardId,
115
            '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...
116
        ];
117
118
        return $this->post($data, UrlBuilder::RESOURCE_DELETE_INVITE);
119
    }
120
121
    /**
122
     * @param string $boardId
123
     * @return bool
124
     */
125 View Code Duplication
    public function acceptInvite($boardId)
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...
126
    {
127
        $data = [
128
            'board_id'         => $boardId,
129
            'invited_user_id' => $this->container->user->id(),
130
        ];
131
132
        return $this->post($data, UrlBuilder::RESOURCE_ACCEPT_INVITE);
133
    }
134
}