Skip to content

Commit

Permalink
De 1255 api stats endpoint (#912)
Browse files Browse the repository at this point in the history
* Add dependabot. Change php-cs-fixer

* Domains. V4

* Merge branch 'master' of github.com:mailgun/mailgun-php

# Conflicts:
#	src/Api/DomainV4.php

* API STATS Endpoint. In Progress

* Added stats api methods

* fix code
  • Loading branch information
oleksandr-mykhailenko authored Jul 15, 2024
1 parent 38af3ad commit 7a98b6f
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 4 deletions.
80 changes: 76 additions & 4 deletions src/Api/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,37 @@

namespace Mailgun\Api;

use Exception;
use Mailgun\Assert;
use Mailgun\Model\Stats\AggregateCountriesResponse;
use Mailgun\Model\Stats\AggregateDevicesResponse;
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 +51,62 @@ 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
* @throws Exception
*/
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);
}

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

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

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

return $this->hydrateResponse($response, AggregateCountriesResponse::class);
}
}
58 changes: 58 additions & 0 deletions src/Model/Stats/AggregateCountriesResponse.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 AggregateCountriesResponse implements ApiResponse
{
private array $countries = [];

private function __construct()
{
}

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

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

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


}
57 changes: 57 additions & 0 deletions src/Model/Stats/AggregateDevicesResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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 AggregateDevicesResponse implements ApiResponse
{
private array $devices = [];

private function __construct()
{
}

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

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

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

}
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;
}

}
Loading

0 comments on commit 7a98b6f

Please sign in to comment.