Skip to content

Commit

Permalink
API STATS Endpoint. In Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-mykhailenko committed Jul 14, 2024
1 parent 4e98711 commit d04a2f9
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/Api/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@

namespace Mailgun\Api;

use Exception;
use Mailgun\Assert;
use Mailgun\Model\Stats\AggregateResponse;
use Mailgun\Model\Stats\TotalResponse;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @see https://documentation.mailgun.com/en/latest/api-stats.html
*
* @author Tobias Nyholm <[email protected]>
*/
class Stats extends HttpApi
{
public const EVENT_ACCEPTED = 'accepted';
public const EVENT_DELIVERED = 'delivered';
public const EVENT_FAILED = 'failed';
public const EVENT_OPENED = 'opened';
public const EVENT_CLICKED = 'clicked';
public const EVENT_UNSUBSCRIBED = 'unsubscribed';
public const EVENT_COMPLAINED = 'complained';
public const EVENT_STORED = 'stored';

/**
* @param string $domain
* @param array $params
* @param array $requestHeaders
* @param string $domain
* @param array $params
* @param array $requestHeaders
* @return TotalResponse|array
* @throws ClientExceptionInterface
* @throws Exception
*/
public function total(string $domain, array $params = [], array $requestHeaders = [])
{
Expand All @@ -37,4 +49,33 @@ public function total(string $domain, array $params = [], array $requestHeaders

return $this->hydrateResponse($response, TotalResponse::class);
}

/**
* @param array $params
* @param array $requestHeaders
* @return TotalResponse|array
* @throws ClientExceptionInterface
* @throws Exception
*/
public function totalAccount(array $params = [], array $requestHeaders = [])
{
Assert::keyExists($params, 'event', 'You must specify an event');

$response = $this->httpGet('/v3/stats/total', $params, $requestHeaders);

return $this->hydrateResponse($response, TotalResponse::class);
}

/**
* @param string $domain
* @param array $requestHeaders
* @return AggregateResponse
* @throws ClientExceptionInterface
*/
public function aggregateCountsByESP(string $domain, array $requestHeaders = []): AggregateResponse
{
$response = $this->httpGet(sprintf('/v3/%s/aggregates/providers', rawurlencode($domain)), [], $requestHeaders);

return $this->hydrateResponse($response, AggregateResponse::class);
}
}
58 changes: 58 additions & 0 deletions src/Model/Stats/AggregateResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Mailgun\Model\Stats;

use Mailgun\Model\ApiResponse;

final class AggregateResponse implements ApiResponse
{
private array $providers = [];

private function __construct()
{
}

/**
* @param array $data
* @return self
* @throws \Exception
*/
public static function create(array $data): self
{
$providers = [];
foreach ($data['providers'] as $domain => $provider) {
$providers[] = AggregateResponseItem::create($provider + ['domain' => $domain]);
}
$model = new self();
$model->setProviders($providers);

return $model;
}

/**
* @return array|AggregateResponseItem[]
*/
public function getProviders(): array
{
return $this->providers;
}

/**
* @param array $providers
* @return void
*/
public function setProviders(array $providers): void
{
$this->providers = $providers;
}

}
205 changes: 205 additions & 0 deletions src/Model/Stats/AggregateResponseItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Mailgun\Model\Stats;

use Mailgun\Model\ApiResponse;

final class AggregateResponseItem implements ApiResponse
{
private int $accepted;
private int $clicked;
private int $complained;
private int $delivered;
private int $opened;
private int $uniqueClicked;
private int $uniqueOpened;
private int $unsubscribed;
private string $domain;


private function __construct()
{
}

/**
* @param array $data
* @return self
*/
public static function create(array $data): self
{
$model = new self();
$model->setClicked($data['clicked'] ?? 0);
$model->setComplained($data['complained'] ?? 0);
$model->setDelivered($data['delivered'] ?? 0);
$model->setOpened($data['opened'] ?? 0);
$model->setUniqueClicked($data['unique_clicked'] ?? 0);
$model->setUniqueOpened($data['unique_opened'] ?? 0);
$model->setUnsubscribed($data['unsubscribed'] ?? 0);
$model->setAccepted($data['accepted'] ?? 0);
$model->setDomain($data['domain'] ?? '');

return $model;
}

/**
* @return int
*/
public function getAccepted(): int
{
return $this->accepted;
}

/**
* @param int $accepted
* @return void
*/
public function setAccepted(int $accepted): void
{
$this->accepted = $accepted;
}

/**
* @return int
*/
public function getClicked(): int
{
return $this->clicked;
}

/**
* @param int $clicked
* @return void
*/
public function setClicked(int $clicked): void
{
$this->clicked = $clicked;
}

/**
* @return int
*/
public function getComplained(): int
{
return $this->complained;
}

/**
* @param int $complained
* @return void
*/
public function setComplained(int $complained): void
{
$this->complained = $complained;
}

/**
* @return int
*/
public function getDelivered(): int
{
return $this->delivered;
}

/**
* @param int $delivered
* @return void
*/
public function setDelivered(int $delivered): void
{
$this->delivered = $delivered;
}

/**
* @return int
*/
public function getOpened(): int
{
return $this->opened;
}

/**
* @param int $opened
* @return void
*/
public function setOpened(int $opened): void
{
$this->opened = $opened;
}

/**
* @return int
*/
public function getUniqueClicked(): int
{
return $this->uniqueClicked;
}

/**
* @param int $uniqueClicked
* @return void
*/
public function setUniqueClicked(int $uniqueClicked): void
{
$this->uniqueClicked = $uniqueClicked;
}

/**
* @return int
*/
public function getUniqueOpened(): int
{
return $this->uniqueOpened;
}

/**
* @param int $uniqueOpened
* @return void
*/
public function setUniqueOpened(int $uniqueOpened): void
{
$this->uniqueOpened = $uniqueOpened;
}

/**
* @return int
*/
public function getUnsubscribed(): int
{
return $this->unsubscribed;
}

/**
* @param int $unsubscribed
* @return void
*/
public function setUnsubscribed(int $unsubscribed): void
{
$this->unsubscribed = $unsubscribed;
}

/**
* @return string
*/
public function getDomain(): string
{
return $this->domain;
}

/**
* @param string $domain
* @return void
*/
public function setDomain(string $domain): void
{
$this->domain = $domain;
}
}
14 changes: 14 additions & 0 deletions src/Model/Stats/TotalResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ private function __construct()
{
}

/**
* @param array $data
* @return self
* @throws \Exception
*/
public static function create(array $data): self
{
$stats = [];
Expand All @@ -45,16 +50,25 @@ public static function create(array $data): self
return $model;
}

/**
* @return \DateTimeImmutable|null
*/
public function getStart(): ?\DateTimeImmutable
{
return $this->start;
}

/**
* @return \DateTimeImmutable|null
*/
public function getEnd(): ?\DateTimeImmutable
{
return $this->end;
}

/**
* @return string|null
*/
public function getResolution(): ?string
{
return $this->resolution;
Expand Down

0 comments on commit d04a2f9

Please sign in to comment.