| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * AnimeDb package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author    Peter Gribanov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright Copyright (c) 2011, Peter Gribanov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license   http://opensource.org/licenses/GPL-3.0 GPL v3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace AnimeDb\Bundle\AniDbBrowserBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use AnimeDb\Bundle\AniDbBrowserBundle\Util\ResponseRepair; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use GuzzleHttp\Client as HttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Browser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var HttpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var ResponseRepair | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $repair; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $api_host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $api_prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $api_protover; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private $app_version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private $app_client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private $app_code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param HttpClient     $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param ResponseRepair $repair | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string         $api_host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string         $api_prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param int            $api_protover | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param int            $app_version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string         $app_client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string         $app_code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         HttpClient $client, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ResponseRepair $repair, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $api_host, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $api_prefix, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $api_protover, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $app_version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $app_client, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $app_code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         $this->repair = $repair; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->api_host = $api_host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $this->api_prefix = $api_prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $this->api_protover = $api_protover; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         $this->app_version = $app_version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         $this->app_client = $app_client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         $this->app_code = $app_code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param array  $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |     public function get($request, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |         $options = $this->buildOptions($request, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $response = $this->client->request('GET', $this->api_host.$this->api_prefix, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         $content = $this->repair->repair($response->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param array  $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 109 | 1 |  |     private function buildOptions($request, array $options = []) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 111 | 1 |  |         $options['request'] = $request; | 
            
                                                        
            
                                    
            
            
                | 112 | 1 |  |         $options['protover'] = $this->api_protover; | 
            
                                                        
            
                                    
            
            
                | 113 | 1 |  |         $options['clientver'] = $this->app_version; | 
            
                                                        
            
                                    
            
            
                | 114 | 1 |  |         $options['client'] = $this->app_client; | 
            
                                                        
            
                                    
            
            
                | 115 | 1 |  |         $options['headers'] = isset($options['headers']) ? $options['headers'] : []; | 
            
                                                        
            
                                    
            
            
                | 116 | 1 |  |         $options['headers']['User-Agent'] = $this->app_code; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 118 | 1 |  |         return $options; | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |