Completed
Pull Request — master (#131)
by Sergey
03:10
created

Conversations::guardAgainstEmptyData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlHelper;
6
use seregazhuk\PinterestBot\Exceptions\InvalidRequestException;
7
8
class Conversations extends Provider
9
{
10
    protected $loginRequiredFor = ['last', 'sendMessage', 'sendEmail'];
11
12
    /**
13
     * Send message to a user.
14
     *
15
     * @param array|int $userId
16
     * @param string $text
17
     * @param int|null $pinId
18
     *
19
     * @throws InvalidRequestException
20
     *
21
     * @return bool
22
     */
23
    public function sendMessage($userId, $text, $pinId = null)
24
    {
25
        $userId = is_array($userId) ? $userId : [$userId];
26
27
        return $this->callSendMessage($userId, $text, $pinId);
28
    }
29
30
    /**
31
     * Send email.
32
     *
33
     * @param array|int $emails
34
     * @param string $text
35
     * @param int|null $pinId
36
     *
37
     * @throws InvalidRequestException
38
     *
39
     * @return bool
40
     */
41
    public function sendEmail($emails, $text, $pinId = null)
42
    {
43
        $emails = is_array($emails) ? $emails : [$emails];
44
45
        return $this->callSendMessage([], $text, $pinId, $emails);
46
    }
47
48
    /**
49
     * Get last user conversations.
50
     *
51
     * @return array|bool
52
     */
53
    public function last()
54
    {
55
        return $this->execGetRequest([], UrlHelper::RESOURCE_GET_LAST_CONVERSATIONS);
56
    }
57
58
    /**
59
     * @param array|int $userId
60
     * @param string $text
61
     * @param int $pinId
62
     * @param array $emails
63
     *
64
     * @throws InvalidRequestException
65
     *
66
     * @return bool
67
     */
68
    protected function callSendMessage($userId, $text, $pinId, array $emails = [])
69
    {
70
        $this->guardAgainstEmptyData($userId, $emails);
71
72
        $requestOptions = [
73
            'pin'      => $pinId,
74
            'text'     => $text,
75
            'emails'   => $emails,
76
            'user_ids' => $userId,
77
        ];
78
79
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_SEND_MESSAGE);
80
    }
81
82
    /**
83
     * @param $userId
84
     * @param array $emails
85
     * @throws InvalidRequestException
86
     */
87
    protected function guardAgainstEmptyData($userId, array $emails)
88
    {
89
        if (empty($userId) && empty($emails)) {
90
            throw new InvalidRequestException('You must specify user_ids or emails to send message.');
91
        }
92
    }
93
}
94