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 string $messageEntityName |
||
12 | * |
||
13 | * @package seregazhuk\PinterestBot\Api\Traits |
||
14 | */ |
||
15 | trait SendsMessages |
||
16 | { |
||
17 | use HandlesRequest; |
||
18 | |||
19 | /** |
||
20 | * @return array |
||
21 | */ |
||
22 | protected function requiresLoginForSendsMessages() |
||
23 | { |
||
24 | return [ |
||
25 | 'send', |
||
26 | 'sendWithMessage', |
||
27 | 'sendWithEmail', |
||
28 | ]; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param array|string $userIds |
||
33 | * @param array|string $emails |
||
34 | * |
||
35 | * @param array $data |
||
36 | * @return bool |
||
37 | * @throws InvalidRequest |
||
38 | */ |
||
39 | protected function callSendMessage($userIds, $emails, array $data) |
||
40 | { |
||
41 | $userIds = (array)$userIds; |
||
42 | $emails = (array)$emails; |
||
43 | |||
44 | $this->guardAgainstEmptyData($userIds, $emails); |
||
45 | |||
46 | $requestOptions = array_merge( |
||
47 | [ |
||
48 | 'emails' => $emails, |
||
49 | 'user_ids' => $userIds, |
||
50 | ], |
||
51 | $data |
||
52 | ); |
||
53 | |||
54 | return $this->post(UrlBuilder::RESOURCE_SEND_MESSAGE, $requestOptions); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $text |
||
59 | * @param string $entityId |
||
60 | * @return array |
||
61 | */ |
||
62 | protected function buildMessageData($text = null, $entityId = null) |
||
63 | { |
||
64 | $entityName = $this->getMessageEntityName(); |
||
65 | |||
66 | return [ |
||
67 | $entityName => $entityId, |
||
68 | 'text' => $text, |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Send item with message or by email. |
||
74 | * |
||
75 | * @param string $entityId |
||
76 | * @param string $text |
||
77 | * @param array|string $userIds |
||
78 | * @param array|string $emails |
||
79 | * @return bool |
||
80 | * @throws InvalidRequest |
||
81 | */ |
||
82 | public function send($entityId, $text, $userIds, $emails) |
||
83 | { |
||
84 | $messageData = $this->buildMessageData($text, $entityId); |
||
85 | |||
86 | return $this->callSendMessage($userIds, $emails, $messageData); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Send item with messages. |
||
91 | * @param int $entityId |
||
92 | * @param string $text |
||
93 | * @param array|string $userIds |
||
94 | * @return bool |
||
95 | * @throws InvalidRequest |
||
96 | */ |
||
97 | public function sendWithMessage($entityId, $text, $userIds) |
||
98 | { |
||
99 | return $this->send($entityId, $text, $userIds, []); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Send entity with emails. |
||
104 | * |
||
105 | * @param int $entityId |
||
106 | * @param string $text |
||
107 | * @param array|string $emails |
||
108 | * @return bool |
||
109 | * @throws InvalidRequest |
||
110 | */ |
||
111 | public function sendWithEmail($entityId, $text, $emails) |
||
112 | { |
||
113 | return $this->send($entityId, $text, [], $emails); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | protected function getMessageEntityName() |
||
120 | { |
||
121 | return property_exists($this, 'messageEntityName') ? $this->messageEntityName : ''; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param array $userIds |
||
126 | * @param array $emails |
||
127 | * @throws InvalidRequest |
||
128 | */ |
||
129 | protected function guardAgainstEmptyData(array $userIds, array $emails) |
||
130 | { |
||
131 | if (empty($userIds) && empty($emails)) { |
||
132 | throw new InvalidRequest('You must specify user_ids or emails to send message.'); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 |