| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Chuckbe\Chuckcms\Chuck\Matomo;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Client;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class QueryFactory implements QueryFactoryInterface
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * Associative array of default parameters, keyed by parameter name.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $defaultParameters;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * The URL of the Matomo server.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $url;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * The HTTP client.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $httpClient;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Constructs a new QueryFactory.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $url
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      *   The URL of the Matomo server.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param \GuzzleHttp\Client $httpClient
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *   The Guzzle HTTP client.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct($url, Client $httpClient)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->url = $url;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->httpClient = new HttpClient($httpClient, new RequestFactory());
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public static function create($url)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return new static($url, new Client());
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function setUrl($url)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->url = $url;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function set($name, $value)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->defaultParameters[$name] = $value;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function has($name)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return array_key_exists($name, $this->defaultParameters);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |      */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     public function remove($name)
 | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         unset($this->defaultParameters[$name]);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function getQuery($method)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $query = new Query($this->url, $this->httpClient);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!empty($this->defaultParameters)) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $query->setParameters($this->defaultParameters);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $query->setParameter('method', $method);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $query;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Returns the HTTP client wrapper.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      *   The HTTP client wrapper.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getHttpClient()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->httpClient;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |     }
 | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | } |