| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cerbero\FluentApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BadMethodCallException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cerbero\FluentApi\Clients\AsyncClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cerbero\FluentApi\Clients\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cerbero\FluentApi\Clients\GuzzleAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cerbero\FluentApi\Inflectors\Psr4ResourceInflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cerbero\FluentApi\Inflectors\ResourceInflectorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Cerbero\FluentApi\Requests\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Abstract implementation of an API start point. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | abstract class AbstractApi extends VersionableRequestMaker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * The HTTP client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @author  Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var     Cerbero\FluentApi\Clients\ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * The resource inflector. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @author  Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var     Cerbero\FluentApi\Inflectors\ResourceInflectorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * The latest resource invoked. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var Cerbero\FluentApi\AbstractResource|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Set the dependencies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param    string|null    $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param    Cerbero\FluentApi\Clients\ClientInterface|null    $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param    Cerbero\FluentApi\Inflectors\ResourceInflectorInterface|null    $inflector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return    void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $version = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         ClientInterface $client = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         ResourceInflectorInterface $inflector = null | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $client    = $client    ?: $this->defaultClient(); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $inflector = $inflector ?: $this->defaultInflector(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->setVersion($version)->setClient($client)->setInflector($inflector); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Dynamically resolve resources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param    string    $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param    array    $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return    $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function __call($name, array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($this->resource && method_exists($this->resource, $name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->request = $this->getRequestFromPreviousResource($name, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->request = $this->getRequestByResolvingResource($name, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Retrieve the updated request from the previously resolved resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param    string    $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param    array    $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return    Cerbero\FluentApi\Requests\Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function getRequestFromPreviousResource($method, array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         call_user_func_array([$this->resource, $method], $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $this->resource->updateOptions($this->getRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Retrieve the request from the newly resolved resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param    string    $resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param    array    $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return    Cerbero\FluentApi\Requests\Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private function getRequestByResolvingResource($resource, array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $class = $this->inflectResource($resource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->resource = new $class(...$parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->resource->fillRequest($this->getRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Retrieve the base URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return    string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     abstract public function getUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Retrieve the resource to call by inflecting the given name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param    string    $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return    Cerbero\FluentApi\Resources\ResourceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     protected function inflectResource($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $target = $this->resource ?: $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $resource = $this->getInflector()->baseNamespace(get_class($target)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                                          ->version($target->getVersion()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                                          ->inflect($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if (class_exists($resource)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         throw new BadMethodCallException("The resource [$resource] does not exist."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Retrieve the resource inflector. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @return    Cerbero\FluentApi\Inflectors\ResourceInflectorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     public function getInflector() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return $this->inflector ?: $this->defaultInflector(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * Retrieve the default inflector. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @return    Cerbero\FluentApi\Inflectors\ResourceInflectorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     protected function defaultInflector() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return new Psr4ResourceInflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * Set the resource inflector. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param    Cerbero\FluentApi\Inflectors\ResourceInflectorInterface    $inflector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @return    $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function setInflector(ResourceInflectorInterface $inflector) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $this->inflector = $inflector; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * Perform a synchronous call to the eventual endpoint. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @return    Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     public function call() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $this->performCall(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * Perform an HTTP call by using the client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @param    Closure|null    $success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param    Closure|null    $failure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @return    mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     private function performCall(Closure $success = null, Closure $failure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $parameters = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             $this->request->verb(), $this->request->endpoint(), $this->request->options(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         if ($success) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             array_push($parameters, $success, $failure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         return call_user_func_array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |             [$this->getClient(), $success ? 'then' : 'call'], $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * Perform an asynchronous call to the eventual endpoint. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * @param    Closure    $success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      * @param    Closure|null    $failure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      * @return    mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     public function then(Closure $success, Closure $failure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         if (! $this->getClient() instanceof AsyncClientInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             throw new Exception("The set client can't perform asynchronous calls."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         return $this->performCall($success, $failure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |      * Retrieve the body of the response as a JSON object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |      * @return    \StdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     public function toJson() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         return $this->decodeBody(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      * Retrieve the decoded response body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |      * @param    boolean    $toArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * @return    \StdClass|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |     private function decodeBody($toArray = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         $body = $this->call()->getBody()->getContents(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         return json_decode($body, $toArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |      * Retrieve the body of the response as an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |      * @return    array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         return $this->decodeBody(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * Retrieve the request to pass through resources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      * @return    Cerbero\FluentApi\Requests\Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     public function getRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         return $this->request ?: new Request($this->getUrl()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * Set the HTTP client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * @param    Cerbero\FluentApi\Clients\ClientInterface    $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      * @return    $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     public function setClient(ClientInterface $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         $this->client = $client; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |      * Retrieve the HTTP client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |      * @return    Cerbero\FluentApi\Clients\ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |     public function getClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |         return $this->client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * Retrieve the default HTTP client to use if none is provided. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      * @author    Andrea Marco Sartori | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |      * @return    Cerbero\FluentApi\Clients\ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     protected function defaultClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         return new GuzzleAdapter(new Client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 306 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 307 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.