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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.