| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace seregazhuk\Favro\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use seregazhuk\Favro\Api\Endpoints\Cards; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use seregazhuk\Favro\Api\Endpoints\Comments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use seregazhuk\Favro\Api\Endpoints\TaskLists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use seregazhuk\Favro\Api\Endpoints\Tasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use seregazhuk\Favro\Api\Endpoints\Users; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use seregazhuk\Favro\Api\Endpoints\Columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use seregazhuk\Favro\Api\Endpoints\Widgets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use seregazhuk\Favro\Api\Endpoints\Endpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use seregazhuk\Favro\Api\Endpoints\Collections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use seregazhuk\Favro\Api\Endpoints\Organizations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use seregazhuk\Favro\Contracts\HttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use seregazhuk\Favro\Exceptions\WrongEndpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use seregazhuk\Favro\Exceptions\WrongOrganizationName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class Api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property Organizations $organizations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @property Users $users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property Collections $collections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @property Widgets $widgets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @property Columns $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @property Cards $cards | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @property Tasks $tasks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @property TaskLists $tasklists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @property Comments $comments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class Api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $organizationId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var HttpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var Endpoint[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $endpoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function __construct(HttpClient $http, Endpoint ...$endpoints) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->http = $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($endpoints as $endpoint) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->endpoints[$endpoint->endpoint()] = $endpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Magic method to access different endpoints. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws WrongEndpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function __get($alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->resolveEndpoint($alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->withOrganization($this->organizationId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $organizationName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @throws WrongOrganizationName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function useOrganization($organizationName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if($organization = $this->getOrganizationByName($organizationName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->organizationId = $organization['organizationId']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $organizationName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @throws WrongOrganizationName | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     private function getOrganizationByName($organizationName) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $organizations = $this->organizations->getAll(); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($organizations['entities'] as $entity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             if ($entity['name'] === $organizationName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 return $entity; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         throw new WrongOrganizationName("Organization $organizationName not found!"); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return Endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws WrongEndpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function resolveEndpoint($alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $alias = strtolower($alias); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if(isset($this->endpoints[$alias])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return $this->endpoints[$alias]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         throw new WrongEndpoint("There is no endpoint called $alias."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function getRateInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $responseHeaders = $this->http->getResponseHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'reset'     => $responseHeaders['X-RateLimit-Reset'][0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'limit'     => $responseHeaders['X-RateLimit-Limit'][0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |