| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace NotificationChannels\RocketChat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Notifications\Notification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | final class RocketChatWebhookChannel | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** @var \NotificationChannels\RocketChat\RocketChat The HTTP client instance. */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $rocketChat; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * Create a new RocketChat channel instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param  \NotificationChannels\RocketChat\RocketChat $rocketChat | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @return void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 4 |  |     public function __construct(RocketChat $rocketChat) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 4 |  |         $this->rocketChat = $rocketChat; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Send the given notification. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param  mixed  $notifiable | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param  \Illuminate\Notifications\Notification $notification | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @throws \NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |     public function send($notifiable, Notification $notification): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** @var \NotificationChannels\RocketChat\RocketChatMessage $message */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |         $message = $notification->toRocketChat($notifiable); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 4 |  |         $to = $message->getChannel() ?: $notifiable->routeNotificationFor('RocketChat'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |         $to = $to ?: $this->rocketChat->getDefaultChannel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |         if ($to === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             throw CouldNotSendNotification::missingTo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 4 |  |         $from = $message->getFrom() ?: $this->rocketChat->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         if (! $from) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             throw CouldNotSendNotification::missingFrom(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |             $this->sendMessage($to, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         } catch (ClientException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         } catch (Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |             throw CouldNotSendNotification::couldNotCommunicateWithRocketChat($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param  string  $to | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param  \NotificationChannels\RocketChat\RocketChatMessage  $message | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |     private function sendMessage(string $to, RocketChatMessage $message): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         $this->rocketChat->sendMessage($to, $message->toArray()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 | 1 |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.