| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NotificationChannels\NetGsm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use NotificationChannels\NetGsm\Exceptions\CouldNotSendNotification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use NotificationChannels\NetGsm\Exceptions\InvalidConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class NetGsmClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const URI = 'http://api.netgsm.com.tr/xmlbulkhttppost.asp'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $userCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $secret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $msgHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param  Client  $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param  string  $userCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param  string  $secret | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param  string  $msgHeader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(Client $client, string $userCode, string $secret, string $msgHeader = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->userCode = $userCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->secret = $secret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->msgHeader = $msgHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Send the Message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param  NetGsmMessage  $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws CouldNotSendNotification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws InvalidConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function send(NetGsmMessage $message): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (empty($message->recipients)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             throw CouldNotSendNotification::emptyRecipients(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $msg = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             "<?xml version='1.0' encoding='utf-8'?>". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             '<mainbody>'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->prepareHeader($message). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->prepareBody($message). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             '</mainbody>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $response = $this->client->post(self::URI, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'body' => $msg, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'headers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     'Content-Type', 'text/xml; charset=utf-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $result = explode(' ', $response->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             if (! isset($result[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 throw CouldNotSendNotification::invalidResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if ($result[0] === '00' || $result[0] === '01' || $result[0] === '02') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 return $result[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } elseif ($result[0] === '20') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 throw CouldNotSendNotification::invalidMessageContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } elseif ($result[0] === '30') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 throw InvalidConfiguration::invalidCredentials(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } elseif ($result[0] === '40') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 throw CouldNotSendNotification::invalidHeader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } elseif ($result[0] === '70') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 throw CouldNotSendNotification::invalidRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 throw CouldNotSendNotification::unknownError(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } catch (InvalidConfiguration $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             throw $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } catch (CouldNotSendNotification $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             throw $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } catch (Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             throw CouldNotSendNotification::serviceRespondedWithAnError($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param  NetGsmMessage  $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     protected function prepareBody(NetGsmMessage $message): string | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         $recipients = implode("\n", array_map(function ($recipient) { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             return '<no>'.$recipient.'</no>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         }, $message->recipients)); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         return | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             '<body>'. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             '<msg><![CDATA['.$message->body.']]></msg>'. | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             $recipients. | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             '</body>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param  NetGsmMessage  $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     protected function prepareHeader(NetGsmMessage $message): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return sprintf('<header> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     <company dil=\'TR\'>NETGSM</company> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                     <usercode>%s</usercode> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                     <password>%s</password> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     <startdate></startdate> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     <stopdate></stopdate> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     <type>1:n</type> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     <msgheader>%s</msgheader> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 </header>', $this->userCode, $this->secret, $message->header ? $message->header : $this->msgHeader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |