Completed
Push — master ( 314dd1...511805 )
by Sergey
05:05
created

Conversations::sendMessage()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 8.5806
cc 4
eloc 17
nc 2
nop 2
1
<?php
2
namespace seregazhuk\PinterestBot\Providers;
3
4
use seregazhuk\PinterestBot\Helpers\UrlHelper;
5
6
class Conversations extends Provider
7
{
8
    /**
9
     * Send message to a user
10
     *
11
     * @param $userId
12
     * @param $text
13
     *
14
     * @return bool
15
     */
16
    public function sendMessage($userId, $text)
17
    {
18
        $this->request->checkLoggedIn();
19
20
        $data = array(
21
            "options" => array(
22
                "user_ids" => array(
23
                    $userId,
24
                ),
25
                "emails" => array(),
26
                "text" => $text,
27
            ),
28
            "context" => new \stdClass(),
29
        );
30
31
        $dataJson = json_encode($data);
32
33
        $post = array(
34
            'data' => $dataJson,
35
        );
36
37
        $postString = UrlHelper::buildRequestString($post);
38
        $res        = $this->request->exec(UrlHelper::RESOURCE_SEND_MESSAGE, $postString);
39
40
        if ($res === null || !isset($res['resource_response']) || $res['resource_response']['error'] !== null) {
41
            return false;
42
        }
43
44
        return true;
45
    }
46
}
47