| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TarfinLabs\Netgsm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\Exception\GuzzleException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use TarfinLabs\Netgsm\Balance\NetgsmAvailableCredit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use TarfinLabs\Netgsm\Balance\NetgsmPackages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use TarfinLabs\Netgsm\Iys\NetgsmIys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use TarfinLabs\Netgsm\Report\AbstractNetgsmReport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use TarfinLabs\Netgsm\Sms\AbstractNetgsmMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Netgsm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Netgsm constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param  Client  $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param  array  $credentials | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param  array  $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __construct(Client $client, array $credentials = [], array $defaults = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->credentials = $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->defaults = $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param  AbstractNetgsmMessage  $netgsmMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws CouldNotSendNotification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws Exceptions\IncorrectPhoneNumberFormatException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function sendSms(AbstractNetgsmMessage $netgsmMessage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $netgsmMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 ->setClient($this->client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ->setCredentials($this->credentials) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ->setDefaults($this->defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 ->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return $netgsmMessage->getJobId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } catch (ClientException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw CouldNotSendNotification::NetgsmRespondedWithAnError($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Get sms status report between given dates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param  AbstractNetgsmReport  $report | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param $startDate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $endDate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param  array  $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws Exceptions\ReportException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     public function getReports( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         AbstractNetgsmReport $report, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $startDate = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $endDate = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         array $filters = [] | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     ): Collection { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $report = $report->setClient($this->client); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         if ($startDate) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $report = $report->setStartDate($startDate); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         if ($endDate) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             $report = $report->setStartDate($startDate); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $report->setCredentials($this->credentials); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         if (count($filters) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             foreach ($filters as $filter => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 if (! method_exists($report, 'set'.$filter)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                     continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 call_user_func([$report, 'set'.$filter], $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         return $report->getReports(); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Returns the remaining credits amount (TL) on the netgsm account. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @throws Exceptions\NetgsmException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function getCredit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $creditService = new NetgsmAvailableCredit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $creditService->setClient($this->client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $creditService->setCredentials($this->credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $creditService->getCredit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Returns the available package list and their balances on the netgsm account. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws Exceptions\NetgsmException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function getAvailablePackages(): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $packageService = new NetgsmPackages(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $packageService->setClient($this->client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $packageService->setCredentials($this->credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return collect($packageService->getPackages()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return NetgsmIys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function iys(): NetgsmIys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $iysService = new NetgsmIys(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $iysService->setClient($this->client)->setCredentials($this->credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $iysService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |