| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * MOJEPANSTWO-API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright © 2017 pudelek.org.pl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view source file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * that is bundled with this package in the file LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author  Marcin Pudełek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | declare (strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace mrcnpdlk\MojePanstwo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use mrcnpdlk\Psr16Cache\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Psr\SimpleCache\CacheInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class Client | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private $sApiUrl; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Cache handler | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @var CacheInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     private $oCache; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @var Adapter | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     private $oCacheAdapter; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Logger handler | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @var \Psr\Log\LoggerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     private $oLogger; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * Client constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $apiUrl | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 14 |  |     public function __construct(string $apiUrl = 'https://api-v3.mojepanstwo.pl/') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 14 |  |         $this->sApiUrl = $apiUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 14 |  |         $this->setLoggerInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 14 |  |         $this->setCacheInstance(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 | 14 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $url | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @throws \mrcnpdlk\MojePanstwo\Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 9 |  |     private function curlRequest(string $url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 9 |  |         $ch = curl_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |         curl_setopt($ch, CURLOPT_URL, $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 9 |  |         curl_setopt($ch, CURLOPT_HEADER, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 9 |  |         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 9 |  |         curl_setopt($ch, CURLOPT_TIMEOUT, 30); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         /** @noinspection CurlSslServerSpoofingInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 9 |  |         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 9 |  |         $output = curl_exec($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 9 |  |         if (curl_errno($ch)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             throw new Exception('Request Error: ' . curl_error($ch)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 9 |  |         $respCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 9 |  |         if ($respCode !== 200) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             throw new Exception('Request Error: ' . $respCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 8 |  |         curl_close($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 8 |  |         $resObj = json_decode($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 8 |  |         if (\JSON_ERROR_NONE !== json_last_error()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             throw new Exception('Unable to parse response body into JSON: ' . json_last_error()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 8 |  |         return $resObj; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * Get logger instance | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @return \Psr\Log\LoggerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |     public function getLogger(): LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 11 |  |         return $this->oLogger; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param string                                  $sPrefixedContext | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param string|null                             $id | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @param \mrcnpdlk\MojePanstwo\QueryBuilder|null $oParams | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @return \stdClass | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @throws \mrcnpdlk\MojePanstwo\Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 9 |  |     public function request(string $sPrefixedContext, string $id = null, QueryBuilder $oParams = null): \stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $tPath = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 9 |  |             rtrim($this->sApiUrl, '/'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 9 |  |             trim($sPrefixedContext, '/'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 9 |  |         if (null !== $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |             $tPath[] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 9 |  |         if (null !== $oParams) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 8 |  |             $sUserParams = $oParams->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 8 |  |             if ('' !== $sUserParams) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 6 |  |                 $tPath[] = '?' . $oParams->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |         $url = implode('/', $tPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 9 |  |         $this->getLogger()->debug(sprintf('REQ: %s', $url)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         /** @noinspection SummerTimeUnsafeTimeManipulationInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 9 |  |         return $this->oCacheAdapter->useCache( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 9 |  |             function () use ($url) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 9 |  |                 return $this->curlRequest($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 9 |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 9 |  |             [$url], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 9 |  |             60 * 60 * 24 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * Setting Cache Adapter | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 14 |  |     private function setCacheAdapter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 14 |  |         $this->oCacheAdapter = new Adapter($this->oCache, $this->oLogger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 14 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * Set Cache handler (PSR-16) | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      * @param \Psr\SimpleCache\CacheInterface|null $oCache | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * @return \mrcnpdlk\MojePanstwo\Client | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md PSR-16 | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 14 |  |     public function setCacheInstance(CacheInterface $oCache = null): Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 14 |  |         $this->oCache = $oCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 14 |  |         $this->setCacheAdapter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 14 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * Set Logger handler (PSR-3) | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * @param \Psr\Log\LoggerInterface|null $oLogger | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 14 |  |     public function setLoggerInstance(LoggerInterface $oLogger = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 14 |  |         $this->oLogger = $oLogger ?: new NullLogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 14 |  |         $this->setCacheAdapter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 14 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 183 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |  |