Inbox::news()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
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, ['conversation_id' => (string)$conversationId], $limit
67
        );
68
    }
69
70
    /**
71
     * Send message to a user.
72
     *
73
     * @param array|string $userIds
74
     * @param string $text
75
     * @param int|null $pinId
76
     * @return bool
77
     * @throws InvalidRequest
78
     */
79
    public function sendMessage($userIds, $text, $pinId = null)
80
    {
81
        return $this->send($pinId, $text, $userIds, []);
82
    }
83
84
    /**
85
     * Send email.
86
     *
87
     * @param array|string $emails
88
     * @param string $text
89
     * @param int|null $pinId
90
     * @return bool
91
     * @throws InvalidRequest
92
     */
93
    public function sendEmail($emails, $text, $pinId = null)
94
    {
95
        return $this->send($pinId, $text, [], $emails);
96
    }
97
98
    /**
99
     * Get current contact requests.
100
     *
101
     * @return array
102
     */
103
    public function contactRequests()
104
    {
105
        $requests = $this->get(UrlBuilder::RESOURCE_CONTACTS_REQUESTS);
106
107
        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...
108
    }
109
110
    /**
111
     * @param string $requestId
112
     * @return bool
113
     */
114
    public function acceptContactRequest($requestId)
115
    {
116
        return $this->makeContactRequestCall(
117
            $requestId, UrlBuilder::RESOURCE_CONTACT_REQUEST_ACCEPT
118
        );
119
    }
120
121
    /**
122
     * @param string $requestId
123
     * @return bool
124
     */
125
    public function ignoreContactRequest($requestId)
126
    {
127
        return $this->makeContactRequestCall(
128
            $requestId, UrlBuilder::RESOURCE_CONTACT_REQUEST_IGNORE
129
        );
130
    }
131
132
    /**
133
     * @param string $newsId
134
     * @param int $limit
135
     * @return Pagination
136
     */
137
    public function newsHubDetails($newsId, $limit = Pagination::DEFAULT_LIMIT)
138
    {
139
        return $this->paginate(
140
            UrlBuilder::RESOURCE_GET_NEWS_HUB_DETAILS,
141
            ['news_id' => $newsId],
142
            $limit
143
        );
144
    }
145
146
    /**
147
     * @param string $requestId
148
     * @param $endpoint
149
     * @return bool
150
     */
151
    protected function makeContactRequestCall($requestId, $endpoint)
152
    {
153
        $data = [
154
            'contact_request' => [
155
                'type' => 'contactrequest',
156
                'id'   => $requestId,
157
            ],
158
        ];
159
160
        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...
161
    }
162
}
163