| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Carpenstar\ByBitAPI\Core\Endpoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Carpenstar\ByBitAPI\Core\Enums\EnumHttpMethods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carpenstar\ByBitAPI\Core\Exceptions\ApiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Carpenstar\ByBitAPI\Core\Exceptions\SDKException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Carpenstar\ByBitAPI\Core\Request\GetRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Carpenstar\ByBitAPI\Core\Request\PostRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | abstract class Endpoint implements IEndpointInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected string $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected string $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected IParametersInterface $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     abstract protected function getResponseClassnameByCondition(array &$apiData = null): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     abstract protected function getRequestClassname(): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     abstract protected function getEndpointUrl(): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param IParametersInterface|null $requestParameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @throws ApiException|SDKException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function bindRequestParameters(?IParametersInterface $requestParameters): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             throw new SDKException(get_class($requestParameters) . " must be instance of " . $this->getRequestClassname()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->parameters = $requestParameters ?? new StubQueryBag(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return IResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |     public function execute(): IResponseInterface | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         switch (static::HTTP_METHOD) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |             case EnumHttpMethods::GET: | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 $curl = GetRequest::getInstance(static::IS_NEED_AUTHORIZATION); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             case EnumHttpMethods::POST: | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                 $curl = PostRequest::getInstance(static::IS_NEED_AUTHORIZATION); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |             default: | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                 throw new \Exception("Http Method not detected"); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         $apiData = $curl->exec($this->getEndpointUrl(), $this->parameters->array()); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         return (new CurlResponseHandler())->build($apiData,  $this->getResponseClassnameByCondition($apiData)); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  | } |