| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NotificationChannels\Clickatell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Clickatell\Api\ClickatellHttp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use NotificationChannels\Clickatell\Exceptions\CouldNotSendNotification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ClickatellClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     const SUCCESSFUL_SEND = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const AUTH_FAILED = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const INVALID_DEST_ADDRESS = 105; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const INVALID_API_ID = 108; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const CANNOT_ROUTE_MESSAGE = 114; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const DEST_MOBILE_BLOCKED = 121; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const DEST_MOBILE_OPTED_OUT = 122; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const MAX_MT_EXCEEDED = 130; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const NO_CREDIT_LEFT = 301; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const INTERNAL_ERROR = 901; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var ClickatellHttp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $clickatell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param ClickatellHttp $clickatellHttp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |     public function __construct(ClickatellHttp $clickatellHttp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 4 |  |         $this->clickatell = $clickatellHttp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array $to String or Array of numbers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $message | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |     public function send(array $to, $message) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |         $to = collect($to)->toArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         $response = $this->clickatell->sendMessage($to, $message); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |         $this->handleProviderResponses($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param array $responses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws CouldNotSendNotification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |     protected function handleProviderResponses(array $responses) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         collect($responses)->each(function (stdClass $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             $errorCode = (int) $response->errorCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             if ($errorCode != self::SUCCESSFUL_SEND) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                 throw CouldNotSendNotification::serviceRespondedWithAnError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |                     (string) $response->error, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     $errorCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     public function getFailedQueueCodes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |             self::AUTH_FAILED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             self::INVALID_DEST_ADDRESS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             self::INVALID_API_ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             self::CANNOT_ROUTE_MESSAGE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             self::DEST_MOBILE_BLOCKED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             self::DEST_MOBILE_OPTED_OUT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             self::MAX_MT_EXCEEDED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     public function getRetryQueueCodes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             self::NO_CREDIT_LEFT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |             self::INTERNAL_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |