Completed
Pull Request — master (#342)
by Sergey
02:24
created

Inbox::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\Pagination;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
8
use seregazhuk\PinterestBot\Exceptions\InvalidRequest;
9
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
10
11
class Inbox extends Provider
12
{
13
    use SendsMessages;
14
15
    /**
16
     * @var array
17
     */
18
    protected $loginRequiredFor = [
19
        'news',
20
        'sendEmail',
21
        'sendMessage',
22
        'notifications',
23
        'contactRequests',
24
        'conversations',
25
    ];
26
27
    /**
28
     * @param int $limit
29
     * @return Pagination
30
     */
31
    public function news($limit = Pagination::DEFAULT_LIMIT)
32
    {
33
        $data = ['allow_stale' => true];
34
35
        return $this->paginate(UrlBuilder::RESOURCE_GET_LATEST_NEWS, $data, $limit);
36
    }
37
38
    /**
39
     * @param int $limit
40
     * @return Pagination
41
     */
42
    public function notifications($limit = Pagination::DEFAULT_LIMIT)
43
    {
44
        return $this->paginate(UrlBuilder::RESOURCE_GET_NOTIFICATIONS, [], $limit);
45
    }
46
47
    /**
48
     * Get last user conversations.
49
     *
50
     * @param int $limit
51
     * @return Pagination
52
     */
53
    public function conversations($limit = Pagination::DEFAULT_LIMIT)
54
    {
55
        return $this->paginate(UrlBuilder::RESOURCE_GET_LAST_CONVERSATIONS, [], $limit);
56
    }
57
58
    /**
59
     * @param int $conversationId
60
     * @param int $limit
61
     * @return Pagination
62
     */
63
    public function getMessages($conversationId, $limit = Pagination::DEFAULT_LIMIT)
64
    {
65
        return $this->paginate(
66
            UrlBuilder::RESOURCE_GET_CONVERSATION_MESSAGES,
67
            ['conversation_id' => (string)$conversationId],
68
            $limit
69
        );
70
    }
71
72
    /**
73
     * Send message to a user.
74
     *
75
     * @param array|string $userIds
76
     * @param string $text
77
     * @param int|null $pinId
78
     * @return bool
79
     * @throws InvalidRequest
80
     */
81
    public function sendMessage($userIds, $text, $pinId = null)
82
    {
83
        return $this->send($pinId, $text, $userIds, []);
84
    }
85
86
    /**
87
     * Send email.
88
     *
89
     * @param array|string $emails
90
     * @param string $text
91
     * @param int|null $pinId
92
     * @return bool
93
     * @throws InvalidRequest
94
     */
95
    public function sendEmail($emails, $text, $pinId = null)
96
    {
97
        return $this->send($pinId, $text, [], $emails);
98
    }
99
100
    /**
101
     * Get current contact requests.
102
     *
103
     * @return array
104
     */
105
    public function contactRequests()
106
    {
107
        $requests = $this->get(UrlBuilder::RESOURCE_CONTACTS_REQUESTS);
108
109
        return !$requests ? [] : $requests;
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $requests ? array() : $requests also could return the type true which is incompatible with the documented return type array.
Loading history...
110
    }
111
112
    /**
113
     * @param string $requestId
114
     * @return bool
115
     */
116
    public function acceptContactRequest($requestId)
117
    {
118
        return $this->makeContactRequestCall(
119
            $requestId, UrlBuilder::RESOURCE_CONTACT_REQUEST_ACCEPT
120
        );
121
    }
122
123
    /**
124
     * @param string $requestId
125
     * @return bool
126
     */
127
    public function ignoreContactRequest($requestId)
128
    {
129
        return $this->makeContactRequestCall(
130
            $requestId, UrlBuilder::RESOURCE_CONTACT_REQUEST_IGNORE
131
        );
132
    }
133
134
    /**
135
     * @param string $requestId
136
     * @param $endpoint
137
     * @return bool
138
     */
139
    protected function makeContactRequestCall($requestId, $endpoint)
140
    {
141
        $data = [
142
            'contact_request' => [
143
                'type' => 'contactrequest',
144
                'id'   => $requestId,
145
            ],
146
        ];
147
148
        return $this->post($endpoint, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post($endpoint, $data) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
149
    }
150
}
151