Completed
Pull Request — master (#66)
by Sergey
03:28
created

Conversations::last()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace seregazhuk\PinterestBot\Api\Providers;
3
4
use seregazhuk\PinterestBot\Api\Request;
5
use seregazhuk\PinterestBot\Exceptions\InvalidRequestException;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
8
class Conversations extends Provider
9
{
10
    protected $loginRequired = ['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
     * @return bool
19
     *
20
     * @throws InvalidRequestException
21
     */
22
    public function sendMessage($userId = [], $text, $pinId = null)
23
    {
24
        $userId = is_array($userId) ?: array($userId);
25
26
        return $this->callSendMessage($userId, $text, $pinId);
0 ignored issues
show
Bug introduced by
It seems like $userId defined by is_array($userId) ?: array($userId) on line 24 can also be of type boolean; however, seregazhuk\PinterestBot\...ions::callSendMessage() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
    }
28
29
    /**
30
     * Send email
31
     *
32
     * @param array $emails
33
     * @param string $text
34
     * @param int|null $pinId
35
     * @return bool
36
     * @throws InvalidRequestException
37
     */
38
    public function sendEmail($emails = [], $text, $pinId = null)
39
    {
40
        $emails = is_array($emails) ?: array($emails);
41
42
        return $this->callSendMessage([], $text, $pinId, $emails);
0 ignored issues
show
Bug introduced by
It seems like $emails defined by is_array($emails) ?: array($emails) on line 40 can also be of type boolean; however, seregazhuk\PinterestBot\...ions::callSendMessage() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
    }
44
45
    /**
46
     * Get last user conversations
47
     *
48
     * @return array|bool
49
     */
50
    public function last()
51
    {
52
        $response = $this->request->exec(
53
            UrlHelper::RESOURCE_GET_LAST_CONVERSATIONS.'?'.Request::createQuery()
54
        );
55
56
        return $this->response->getData($response);
57
    }
58
59
    /**
60
     * @param array $userId
61
     * @param string $text
62
     * @param int $pinId
63
     * @param array $emails
64
     * @return mixed
65
     *
66
     * @throws InvalidRequestException
67
     */
68
    protected function callSendMessage($userId = [], $text, $pinId, $emails = [])
69
    {
70
        if (empty($userId) && empty($emails)) {
71
            throw new InvalidRequestException('You must specify user_ids or emails to send message.');
72
        }
73
74
        $requestOptions = array(
75
            'pin'      => $pinId,
76
            'text'     => $text,
77
            'emails'   => $emails,
78
            'user_ids' => $userId,
79
        );
80
81
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_SEND_MESSAGE);
82
    }
83
}
84