Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Emmolo committed Aug 1, 2024
2 parents fbbda92 + deda18d commit 4cbe210
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/CloudflareAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@

namespace The3LabsTeam\PhpCloudflareAnalytics;

class CloudflareAnalytics {

class CloudflareAnalytics
{
public string $api_token;

public string $zoneTag;

public string $endpoint;

public function __construct(string $zoneTag) {
public function __construct(string $zoneTag)
{
$this->api_token = env('CLOUDFLARE_API_TOKEN');
$this->zoneTag = $zoneTag;
$this->endpoint = "https://api.cloudflare.com/client/v4/graphql";
$this->endpoint = 'https://api.cloudflare.com/client/v4/graphql';
}

// ================== UTILITY ================== //

protected function graphQLQuery($query) {
protected function graphQLQuery($query)
{
$ch = curl_init($this->endpoint);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['query' => $query]),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->api_token
]
'Authorization: Bearer '.$this->api_token,
],
]);

$response = curl_exec($ch);
Expand Down Expand Up @@ -57,13 +61,13 @@ protected function sumTotal($response, $zonesType, $param, $paramType)
//TODO: Merge all the functions in one function with parameters
/**
* Get the total views between two dates - Returns the total views
* @param $startDate
* @param $endDate
* @param $param - 'sum' | 'uniq'
* @param $paramType - sum : 'request', 'pageViews', 'cachedBytes', 'cachedRequests', 'threats' | uniq: 'uniques'
*
* @param $param - 'sum' | 'uniq'
* @param $paramType - sum : 'request', 'pageViews', 'cachedBytes', 'cachedRequests', 'threats' | uniq: 'uniques'
* @return int|mixed
*/
public function getBetweenDates($startDate, $endDate, $param = 'sum', $paramType = 'pageViews') {
public function getBetweenDates($startDate, $endDate, $param = 'sum', $paramType = 'pageViews')
{
$query = <<<GRAPHQL
query {
viewer {
Expand Down Expand Up @@ -92,12 +96,13 @@ public function getBetweenDates($startDate, $endDate, $param = 'sum', $paramType
GRAPHQL;

$response = $this->graphQLQuery($query);

return $this->sumTotal($response, 'httpRequests1dGroups', $param, $paramType);
}

/**
* Get the total views between two dates - Return the total views
* @param $sub
*
* @return array
*/
public function getBetweenHours($sub, $param, $paramType)
Expand Down Expand Up @@ -145,6 +150,7 @@ public function getBetweenHours($sub, $param, $paramType)

/**
* Get the total views last 6 hours - Returns the total views
*
* @return int|mixed
*/
public function getLast6Hours($param, $paramType)
Expand All @@ -154,15 +160,17 @@ public function getLast6Hours($param, $paramType)

/**
* Get the total views last 24 hours - Returns the total views
*
* @return int|mixed
*/
public function getLast24Hours($param, $paramType)
{
return $this->getBetweenHours(sub: '-24 hours', param: $param, paramType: $paramType);
return $this->getBetweenHours(sub: '-24 hours', param: $param, paramType: $paramType);
}

/**
* Get the total views last 7 days - Returns the total views
*
* @return int|mixed
*/
public function getLast7Days($param, $paramType)
Expand All @@ -176,6 +184,7 @@ public function getLast7Days($param, $paramType)

/**
* Get the total views last month - Returns the total views
*
* @return int|mixed
*/
public function getLastMonth($param, $paramType)
Expand Down

0 comments on commit 4cbe210

Please sign in to comment.