-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from chuckbe/refactor
Refactor
- Loading branch information
Showing
21 changed files
with
3,203 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace Chuckbe\Chuckcms\Chuck\Matomo; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
|
||
/** | ||
* A wrapper class that provides request options for the HTTP client. | ||
*/ | ||
class HttpClient implements HttpClientInterface | ||
{ | ||
/** | ||
* The Guzzle HTTP client. | ||
* | ||
* @var \GuzzleHttp\ClientInterface | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* The PSR7 request factory. | ||
* | ||
* @var \Chuckbe\Chuckcms\Chuck\Matomo\RequestFactoryInterface | ||
*/ | ||
protected $requestFactory; | ||
|
||
/** | ||
* The parameters to pass to the request. | ||
* | ||
* @var array | ||
*/ | ||
protected $requestParams = []; | ||
|
||
/** | ||
* The request method. | ||
* | ||
* @var string | ||
*/ | ||
protected $method = 'GET'; | ||
|
||
/** | ||
* The request url. | ||
* | ||
* @var string; | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* Constructs a new HttpClient object. | ||
* | ||
* @param \GuzzleHttp\ClientInterface $httpClient | ||
* The Guzzle HTTP client. | ||
* @param \Chuckbe\Chuckcms\Chuck\Matomo\RequestFactoryInterface $requestFactory | ||
* The PSR7 request factory. | ||
*/ | ||
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->requestFactory = $requestFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setRequestParameters(array $requestParams) | ||
{ | ||
$this->requestParams = $requestParams; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRequestParameters() | ||
{ | ||
return $this->requestParams; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMethod() | ||
{ | ||
return $this->method; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setMethod($method) | ||
{ | ||
// Currently, only GET and POST requests are supported. | ||
if (!in_array($method, ['GET', 'POST'], true)) { | ||
throw new \InvalidArgumentException( | ||
'Only GET and POST requests are allowed.' | ||
); | ||
} | ||
$this->method = $method; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUrl() | ||
{ | ||
return $this->url; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUrl($url) | ||
{ | ||
if (filter_var($url, FILTER_VALIDATE_URL) === false) { | ||
throw new \InvalidArgumentException('Invalid URL.'); | ||
} | ||
|
||
$this->url = $url; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequest() | ||
{ | ||
if (empty($this->getUrl())) { | ||
throw new \Exception('Request url is not set.'); | ||
} | ||
|
||
$request = $this->requestFactory->getRequest($this->getMethod(), $this->getUrl()); | ||
$param_type = $this->getMethod() === 'GET' ? 'query' : 'form_params'; | ||
$options = [$param_type => $this->getRequestParameters()]; | ||
|
||
return $this->httpClient->send($request, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Chuckbe\Chuckcms\Chuck\Matomo; | ||
|
||
/** | ||
* Interface for objects that wrap an HTTP client. | ||
*/ | ||
interface HttpClientInterface | ||
{ | ||
/** | ||
* Sets the request parameters. | ||
* | ||
* @param array $requestParams | ||
* The request parameters array. | ||
* | ||
* @return \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient | ||
* The object itself for chain calls. | ||
*/ | ||
public function setRequestParameters(array $requestParams); | ||
|
||
/** | ||
* Returns the request parameters. | ||
* | ||
* @return array | ||
* The request parameters. | ||
*/ | ||
public function getRequestParameters(); | ||
|
||
/** | ||
* Returns the request method. | ||
* | ||
* @return string | ||
* The request method. | ||
*/ | ||
public function getMethod(); | ||
|
||
/** | ||
* Sets the request method. | ||
* | ||
* @param string $method | ||
* The request method. | ||
* | ||
* @return \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient | ||
* The object itself for chain calls. | ||
* | ||
* @throws \InvalidArgumentException | ||
* Thrown if the method passed is not supported. | ||
*/ | ||
public function setMethod($method); | ||
|
||
/** | ||
* Returns the request url. | ||
* | ||
* @return string | ||
* The request url. | ||
*/ | ||
public function getUrl(); | ||
|
||
/** | ||
* Sets the url of the request. | ||
* | ||
* @param string $url | ||
* The url of the request. | ||
* | ||
* @return \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient | ||
* The object itself for chain calls. | ||
* | ||
* @throws \InvalidArgumentException | ||
* Thrown when the url passed is not a valid url. | ||
*/ | ||
public function setUrl($url); | ||
|
||
/** | ||
* Sends the request and returns the results. | ||
* | ||
* @return \GuzzleHttp\Psr7\Response | ||
* A response that the request generated. | ||
* | ||
* @throws \Exception | ||
* Thrown when the url is not set yet. | ||
*/ | ||
public function sendRequest(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Chuckbe\Chuckcms\Chuck\Matomo; | ||
|
||
/** | ||
* Default implementation of a query for the Matomo reporting API. | ||
*/ | ||
class Query implements QueryInterface | ||
{ | ||
/** | ||
* An HTTP client that encapsulates a GuzzleHttp client. | ||
* | ||
* @var \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* Associative array of query parameters, keyed by parameter name. | ||
* | ||
* @var array | ||
*/ | ||
protected $parameters; | ||
|
||
/** | ||
* Constructs a new Query object. | ||
* | ||
* @param string $url | ||
* The URL of the Matomo server. | ||
* @param \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient $httpClient | ||
* The HTTP client wrapper. | ||
*/ | ||
public function __construct($url, HttpClient $httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->httpClient->setUrl($url); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParameters(array $parameters) | ||
{ | ||
foreach ($parameters as $name => $value) { | ||
$this->setParameter($name, $value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParameter($name, $value) | ||
{ | ||
$this->parameters[$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameters() | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameter($name) | ||
{ | ||
if (!array_key_exists($name, $this->parameters)) { | ||
throw new \InvalidArgumentException("Parameter '$name' is not set."); | ||
} | ||
return $this->parameters[$name]; | ||
} | ||
|
||
/** | ||
* Prepares the query for execution. | ||
*/ | ||
protected function prepareExecute() | ||
{ | ||
// Set the format to JSON. | ||
$this->setParameter('format', 'json'); | ||
|
||
// Use the reporting API. | ||
$this->setParameter('module', 'API'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute() | ||
{ | ||
$this->prepareExecute(); | ||
$response = $this->httpClient->setRequestParameters($this->parameters)->sendRequest(); | ||
return new QueryResult($response); | ||
} | ||
} |
Oops, something went wrong.