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 /**
4244 * @param HttpClient $http
4345 * @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
@@ -55,19 +57,22 @@ public function __construct(HttpClient $http, $token)
5557 * @link https://pushover.net/api
5658 *
5759 * @param array $params
58- * @return \Psr\Http\Message\ResponseInterface
60+ * @param mixed $notifiable
61+ * @return ResponseInterface
5962 *
6063 * @throws CouldNotSendNotification
64+ * @throws ServiceCommunicationError
65+ * @throws GuzzleException
6166 */
62- public function send ($ params)
67+ public function send (array $ params, mixed $ notifiable ): ResponseInterface
6368 {
6469 try {
6570 $ multipart = [];
6671
6772 foreach ($ this ->paramsWithToken ($ params ) as $ name => $ contents ) {
6873 if ($ name !== 'image ' ) {
6974 $ multipart [] = [
70- 'name ' => $ name ,
75+ 'name ' => $ name ,
7176 'contents ' => $ contents ,
7277 ];
7378 } else {
@@ -79,6 +84,7 @@ public function send($params)
7984 }
8085 }
8186
87+ //dd($multipart);
8288 return $ this ->http ->post (
8389 $ this ->pushoverApiUrl ,
8490 [
@@ -103,7 +109,7 @@ public function send($params)
103109 * @param array $params
104110 * @return array
105111 */
106- protected function paramsWithToken ($ params )
112+ protected function paramsWithToken (array $ params ): array
107113 {
108114 return array_merge ([
109115 'token ' => $ this ->token ,
@@ -116,11 +122,17 @@ protected function paramsWithToken($params)
116122 * If there is any error (problem with reading the file, file size exceeds the limit, the file is not an image),
117123 * silently returns null and sends the message without image attachment.
118124 *
119- * @param $file
125+ * @param $file
120126 * @return array|null
127+ *
128+ * @throws GuzzleException
121129 */
122130 private function getImageData ($ file ): ?array
123131 {
132+ if (empty ($ file )) {
133+ return null ;
134+ }
135+
124136 try {
125137 // check if $file is not too big
126138 if (is_file ($ file ) && is_readable ($ file )) {
@@ -161,10 +173,10 @@ private function getImageData($file): ?array
161173
162174 return [
163175 // name of the field holding the image must be 'attachment' (https://pushover.net/api#attachments)
164- 'name ' => 'attachment ' ,
176+ 'name ' => 'attachment ' ,
165177 'contents ' => $ contents ,
166178 'filename ' => basename ($ file ),
167- 'headers ' => [
179+ 'headers ' => [
168180 'Content-Type ' => $ contentType ,
169181 ],
170182 ];
0 commit comments