Skip to content

Commit

Permalink
Merge pull request #18 from chuckbe/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
KarelBrijs committed Jan 21, 2022
2 parents d13d8d9 + 2ce42e8 commit dbf2b1b
Show file tree
Hide file tree
Showing 21 changed files with 3,203 additions and 82 deletions.
12 changes: 12 additions & 0 deletions src/Chuck/Accessors/Chuck.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public static function routes()
Route::group(['middleware' => 'auth'], function () {
// Dashboard Routes...
Route::get('/dashboard', '\Chuckbe\Chuckcms\Controllers\DashboardController@index')->name('dashboard');
// Matomo Routes
Route::post('/dashboard/matomo/api', '\Chuckbe\Chuckcms\Controllers\MatomoController@ReportingApi')->name('dashboard.api');
Route::post('/dashboard/matomo/api/livecounter', '\Chuckbe\Chuckcms\Controllers\MatomoController@getLiveCounter')->name('dashboard.apilivecounter');
Route::post('/dashboard/matomo/api/overview', '\Chuckbe\Chuckcms\Controllers\MatomoController@getVisitsData')->name('dashboard.apioverview');
Route::post('/dashboard/matomo/api/overview/referrers', '\Chuckbe\Chuckcms\Controllers\MatomoController@getReferrers')->name('dashboard.apioverview.referrers');
Route::get('/dashboard/matomo', '\Chuckbe\Chuckcms\Controllers\MatomoController@index')->name('dashboard.matomo');
Route::post('/dashboard/matomo/changerange', '\Chuckbe\Chuckcms\Controllers\MatomoController@matomo');
Route::get('/dashboard/matomo/livevisit', '\Chuckbe\Chuckcms\Controllers\MatomoController@counter')->name('dashboard.livevisits');
Route::post('/dashboard/matomo/submitmatomo', '\Chuckbe\Chuckcms\Controllers\MatomoController@submit')->name('dashboard.matomosubmit');
Route::post('/reportingApi/visitorsummary', '\Chuckbe\Chuckcms\Controllers\MatomoController@visitorSummary');
Route::get('/reportingApi', '\Chuckbe\Chuckcms\Controllers\MatomoController@reportingApi');

// Dashboard Pages Routes...
Route::group(['middleware' => ['permission:show pages']], function () {
Route::get('/dashboard/pages', '\Chuckbe\Chuckcms\Controllers\PageController@index')->name('dashboard.pages');
Expand Down
140 changes: 140 additions & 0 deletions src/Chuck/Matomo/HttpClient.php
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);
}
}
83 changes: 83 additions & 0 deletions src/Chuck/Matomo/HttpClientInterface.php
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();
}
100 changes: 100 additions & 0 deletions src/Chuck/Matomo/Query.php
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);
}
}

0 comments on commit dbf2b1b

Please sign in to comment.