| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Glorand\Drip\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Glorand\Drip\Api\Response\ApiResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Glorand\Drip\Models\Subscriber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Subscribers extends Api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param int   $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @param int   $perPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      *                       status - Default - active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      *                       tags  - Optional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      *                       subscribed_before - Optional. A ISO-8601 datetime. Eg. "2017-01-01T00:00:00Z" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      *                       subscribed_after - Optional. A ISO-8601 datetime. Eg. "2016-01-01T00:00:00Z" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return \Glorand\Drip\Api\Response\ApiResponse; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 1 |  |     public function list(int $page = 1, int $perPage = 100, array $params = []): ApiResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |         return $this->sendGet( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |             ':account_id:/subscribers', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |             array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 1 |  |                     'page'     => $page, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 1 |  |                     'per_page' => $perPage, | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |                 $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param \Glorand\Drip\Models\Subscriber $subscriber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     public function store(Subscriber $subscriber): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $response = $this->sendPost( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             ':account_id:/subscribers', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             $subscriber->toDrip() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         return $response->isSuccess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array $subscribers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |     public function batchStore(array $subscribers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         foreach ($subscribers as $subscriber) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             if (is_array($subscriber)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |                 $data[] = $subscriber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             } elseif ($subscriber instanceof Subscriber) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |                 $data[] = $subscriber->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $postData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'batches' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                     'subscribers' => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $response = $this->sendPost( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             ':account_id:/subscribers/batches', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         return $response->isSuccess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     public function batchUnsubscribe(array $subscribers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         foreach ($subscribers as $subscriber) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             if (is_array($subscriber)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                 $data[] = $subscriber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             } elseif ($subscriber instanceof Subscriber) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |                 $data[] = $subscriber->getEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $postData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'batches' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |                     'subscribers' => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         $response = $this->sendPost( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             ':account_id:/unsubscribes/batches', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         return $response->isSuccess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |