44
55use Exception ;
66use GuzzleHttp \Client as HttpClient ;
7+ use GuzzleHttp \Exception \GuzzleException ;
78use GuzzleHttp \Exception \RequestException ;
89use NotificationChannels \Pushover \Exceptions \CouldNotSendNotification ;
910use NotificationChannels \Pushover \Exceptions \ServiceCommunicationError ;
11+ use Psr \Http \Message \ResponseInterface ;
1012
1113class Pushover
1214{
@@ -22,27 +24,27 @@ class Pushover
2224 *
2325 * @var string
2426 */
25- protected $ pushoverApiUrl = 'https://api.pushover.net/1/messages.json ' ;
27+ protected string $ pushoverApiUrl = 'https://api.pushover.net/1/messages.json ' ;
2628
2729 /**
2830 * The HTTP client instance.
2931 *
30- * @var \GuzzleHttp\Client
32+ * @var HttpClient
3133 */
32- protected $ http ;
34+ protected HttpClient $ http ;
3335
3436 /**
3537 * Pushover App Token.
3638 *
3739 * @var string
3840 */
39- protected $ token ;
41+ protected string $ token ;
4042
4143 /**
42- * @param HttpClient $http
43- * @param string $token
44+ * @param HttpClient $http
45+ * @param string $token
4446 */
45- public function __construct (HttpClient $ http , $ token )
47+ public function __construct (HttpClient $ http , string $ token )
4648 {
4749 $ this ->http = $ http ;
4850
@@ -54,21 +56,23 @@ public function __construct(HttpClient $http, $token)
5456 *
5557 * @link https://pushover.net/api
5658 *
57- * @param array $params
59+ * @param array $params
5860 * @param mixed $notifiable
59- * @return \Psr\Http\Message\ ResponseInterface
61+ * @return ResponseInterface
6062 *
6163 * @throws CouldNotSendNotification
64+ * @throws ServiceCommunicationError
65+ * @throws GuzzleException
6266 */
63- public function send ($ params , $ notifiable )
67+ public function send (array $ params , mixed $ notifiable ): ResponseInterface
6468 {
6569 try {
6670 $ multipart = [];
6771
6872 foreach ($ this ->paramsWithToken ($ params ) as $ name => $ contents ) {
6973 if ($ name !== 'image ' ) {
7074 $ multipart [] = [
71- 'name ' => $ name ,
75+ 'name ' => $ name ,
7276 'contents ' => $ contents ,
7377 ];
7478 } else {
@@ -80,6 +84,7 @@ public function send($params, $notifiable)
8084 }
8185 }
8286
87+ //dd($multipart);
8388 return $ this ->http ->post (
8489 $ this ->pushoverApiUrl ,
8590 [
@@ -101,10 +106,10 @@ public function send($params, $notifiable)
101106 /**
102107 * Merge token into parameters array, unless it has been set on the PushoverReceiver.
103108 *
104- * @param array $params
109+ * @param array $params
105110 * @return array
106111 */
107- protected function paramsWithToken ($ params )
112+ protected function paramsWithToken (array $ params ): array
108113 {
109114 return array_merge ([
110115 'token ' => $ this ->token ,
@@ -119,9 +124,14 @@ protected function paramsWithToken($params)
119124 *
120125 * @param $file
121126 * @return array|null
127+ * @throws GuzzleException
122128 */
123129 private function getImageData ($ file ): ?array
124130 {
131+ if (empty ($ file )) {
132+ return null ;
133+ }
134+
125135 try {
126136 // check if $file is not too big
127137 if (is_file ($ file ) && is_readable ($ file )) {
@@ -141,7 +151,7 @@ private function getImageData($file): ?array
141151 }
142152
143153 // some servers may not return the "Content-Length" header
144- $ fileSizeChecked = (bool ) $ contentLength ;
154+ $ fileSizeChecked = (bool )$ contentLength ;
145155 }
146156
147157 // check if $file is an image
@@ -153,7 +163,7 @@ private function getImageData($file): ?array
153163
154164 $ contents = file_get_contents ($ file );
155165 // if not checked before, finally check the file size after reading it
156- if (! $ fileSizeChecked && strlen ($ contents ) > self ::IMAGE_SIZE_LIMIT ) {
166+ if (!$ fileSizeChecked && strlen ($ contents ) > self ::IMAGE_SIZE_LIMIT ) {
157167 return null ;
158168 }
159169 } catch (Exception $ exception ) {
@@ -162,10 +172,10 @@ private function getImageData($file): ?array
162172
163173 return [
164174 // name of the field holding the image must be 'attachment' (https://pushover.net/api#attachments)
165- 'name ' => 'attachment ' ,
175+ 'name ' => 'attachment ' ,
166176 'contents ' => $ contents ,
167177 'filename ' => basename ($ file ),
168- 'headers ' => [
178+ 'headers ' => [
169179 'Content-Type ' => $ contentType ,
170180 ],
171181 ];
0 commit comments