|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
|
6
|
|
|
use seregazhuk\PinterestBot\Exceptions\InvalidRequest; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait SendsMessages |
|
10
|
|
|
* |
|
11
|
|
|
* @property $messageEntityName |
|
12
|
|
|
* |
|
13
|
|
|
* @package seregazhuk\PinterestBot\Api\Traits |
|
14
|
|
|
*/ |
|
15
|
|
|
trait SendsMessages |
|
16
|
|
|
{ |
|
17
|
|
|
use HandlesRequest; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array|int $userIds |
|
21
|
|
|
* @param array|string $emails |
|
22
|
|
|
* |
|
23
|
|
|
* @param array $data |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function callSendMessage($userIds, $emails, array $data) |
|
27
|
|
|
{ |
|
28
|
|
|
$userIds = is_array($userIds) ? $userIds : [$userIds]; |
|
29
|
|
|
$emails = is_array($emails) ? $emails : [$emails]; |
|
30
|
|
|
|
|
31
|
|
|
$this->guardAgainstEmptyData($userIds, $emails); |
|
32
|
|
|
|
|
33
|
|
|
$requestOptions = array_merge([ |
|
34
|
|
|
'emails' => $emails, |
|
35
|
|
|
'user_ids' => $userIds, |
|
36
|
|
|
], |
|
37
|
|
|
$data); |
|
38
|
|
|
|
|
39
|
|
|
return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_SEND_MESSAGE); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $text |
|
44
|
|
|
* @param string $entityId |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function buildMessageData($text = null, $entityId = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$entityName = $this->getMessageEntityName(); |
|
50
|
|
|
|
|
51
|
|
|
return [ |
|
52
|
|
|
$entityName => $entityId, |
|
53
|
|
|
'text' => $text, |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Send item with message or by email. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $entityId |
|
61
|
|
|
* @param string $text |
|
62
|
|
|
* @param array|string $userIds |
|
63
|
|
|
* @param array|string $emails |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function send($entityId, $text, $userIds, $emails) |
|
67
|
|
|
{ |
|
68
|
|
|
$messageData = $this->buildMessageData($text, $entityId); |
|
69
|
|
|
|
|
70
|
|
|
return $this->callSendMessage($userIds, $emails, $messageData); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Send item with messages. |
|
75
|
|
|
* @codeCoverageIgnore |
|
76
|
|
|
* @param int $entityId |
|
77
|
|
|
* @param string $text |
|
78
|
|
|
* @param array|string $userIds |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function sendWithMessage($entityId, $text, $userIds) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->send($entityId, $text, $userIds, []); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Send entity with emails. |
|
88
|
|
|
* |
|
89
|
|
|
* @codeCoverageIgnore |
|
90
|
|
|
* @param int $entityId |
|
91
|
|
|
* @param string $text |
|
92
|
|
|
* @param array|string $emails |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function sendWithEmail($entityId, $text, $emails) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->send($entityId, $text, [], $emails); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getMessageEntityName() |
|
104
|
|
|
{ |
|
105
|
|
|
return property_exists($this, 'messageEntityName') ? $this->messageEntityName : ''; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param $userId |
|
110
|
|
|
* @param array $emails |
|
111
|
|
|
* @throws InvalidRequest |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function guardAgainstEmptyData($userId, array $emails) |
|
114
|
|
|
{ |
|
115
|
|
|
if (empty($userId) && empty($emails)) { |
|
116
|
|
|
throw new InvalidRequest('You must specify user_ids or emails to send message.'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.