Skip to content

Commit 7a98b6f

Browse files
De 1255 api stats endpoint (#912)
* 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
1 parent 38af3ad commit 7a98b6f

6 files changed

+506
-4
lines changed

src/Api/Stats.php

+76-4
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,37 @@
1111

1212
namespace Mailgun\Api;
1313

14+
use Exception;
1415
use Mailgun\Assert;
16+
use Mailgun\Model\Stats\AggregateCountriesResponse;
17+
use Mailgun\Model\Stats\AggregateDevicesResponse;
18+
use Mailgun\Model\Stats\AggregateResponse;
1519
use Mailgun\Model\Stats\TotalResponse;
1620
use Psr\Http\Client\ClientExceptionInterface;
21+
use Psr\Http\Message\ResponseInterface;
1722

1823
/**
1924
* @see https://documentation.mailgun.com/en/latest/api-stats.html
20-
*
2125
* @author Tobias Nyholm <[email protected]>
2226
*/
2327
class Stats extends HttpApi
2428
{
29+
public const EVENT_ACCEPTED = 'accepted';
30+
public const EVENT_DELIVERED = 'delivered';
31+
public const EVENT_FAILED = 'failed';
32+
public const EVENT_OPENED = 'opened';
33+
public const EVENT_CLICKED = 'clicked';
34+
public const EVENT_UNSUBSCRIBED = 'unsubscribed';
35+
public const EVENT_COMPLAINED = 'complained';
36+
public const EVENT_STORED = 'stored';
37+
2538
/**
26-
* @param string $domain
27-
* @param array $params
28-
* @param array $requestHeaders
39+
* @param string $domain
40+
* @param array $params
41+
* @param array $requestHeaders
2942
* @return TotalResponse|array
3043
* @throws ClientExceptionInterface
44+
* @throws Exception
3145
*/
3246
public function total(string $domain, array $params = [], array $requestHeaders = [])
3347
{
@@ -37,4 +51,62 @@ public function total(string $domain, array $params = [], array $requestHeaders
3751

3852
return $this->hydrateResponse($response, TotalResponse::class);
3953
}
54+
55+
/**
56+
* @param array $params
57+
* @param array $requestHeaders
58+
* @return TotalResponse|array
59+
* @throws ClientExceptionInterface
60+
* @throws Exception
61+
*/
62+
public function totalAccount(array $params = [], array $requestHeaders = [])
63+
{
64+
Assert::keyExists($params, 'event', 'You must specify an event');
65+
66+
$response = $this->httpGet('/v3/stats/total', $params, $requestHeaders);
67+
68+
return $this->hydrateResponse($response, TotalResponse::class);
69+
}
70+
71+
/**
72+
* @param string $domain
73+
* @param array $requestHeaders
74+
* @return AggregateResponse
75+
* @throws ClientExceptionInterface
76+
* @throws Exception
77+
*/
78+
public function aggregateCountsByESP(string $domain, array $requestHeaders = []): AggregateResponse
79+
{
80+
$response = $this->httpGet(sprintf('/v3/%s/aggregates/providers', rawurlencode($domain)), [], $requestHeaders);
81+
82+
return $this->hydrateResponse($response, AggregateResponse::class);
83+
}
84+
85+
/**
86+
* @param string $domain
87+
* @param array $requestHeaders
88+
* @return AggregateDevicesResponse
89+
* @throws ClientExceptionInterface
90+
* @throws Exception
91+
*/
92+
public function aggregateByDevices(string $domain, array $requestHeaders = []): AggregateDevicesResponse
93+
{
94+
$response = $this->httpGet(sprintf('/v3/%s/aggregates/devices', rawurlencode($domain)), [], $requestHeaders);
95+
96+
return $this->hydrateResponse($response, AggregateDevicesResponse::class);
97+
}
98+
99+
/**
100+
* @param string $domain
101+
* @param array $requestHeaders
102+
* @return AggregateCountriesResponse
103+
* @throws ClientExceptionInterface
104+
* @throws Exception
105+
*/
106+
public function aggregateByCountry(string $domain, array $requestHeaders = []): AggregateCountriesResponse
107+
{
108+
$response = $this->httpGet(sprintf('/v3/%s/aggregates/countries', rawurlencode($domain)), [], $requestHeaders);
109+
110+
return $this->hydrateResponse($response, AggregateCountriesResponse::class);
111+
}
40112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright (C) 2013 Mailgun
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Mailgun\Model\Stats;
13+
14+
use Mailgun\Model\ApiResponse;
15+
16+
final class AggregateCountriesResponse implements ApiResponse
17+
{
18+
private array $countries = [];
19+
20+
private function __construct()
21+
{
22+
}
23+
24+
/**
25+
* @param array $data
26+
* @return self
27+
* @throws \Exception
28+
*/
29+
public static function create(array $data): self
30+
{
31+
$countries = [];
32+
foreach ($data['countries'] as $country => $provider) {
33+
$countries[] = AggregateResponseItem::create($provider + ['country' => $country]);
34+
}
35+
$model = new self();
36+
$model->setCountries($countries);
37+
return $model;
38+
}
39+
40+
/**
41+
* @return array|AggregateResponseItem[]
42+
*/
43+
public function getCountries(): array
44+
{
45+
return $this->countries;
46+
}
47+
48+
/**
49+
* @param array $countries
50+
* @return void
51+
*/
52+
public function setCountries(array $countries): void
53+
{
54+
$this->countries = $countries;
55+
}
56+
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright (C) 2013 Mailgun
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Mailgun\Model\Stats;
13+
14+
use Mailgun\Model\ApiResponse;
15+
16+
final class AggregateDevicesResponse implements ApiResponse
17+
{
18+
private array $devices = [];
19+
20+
private function __construct()
21+
{
22+
}
23+
24+
/**
25+
* @param array $data
26+
* @return self
27+
* @throws \Exception
28+
*/
29+
public static function create(array $data): self
30+
{
31+
$devices = [];
32+
foreach ($data['devices'] as $domain => $provider) {
33+
$devices[] = AggregateResponseItem::create($provider + ['device' => $domain]);
34+
}
35+
$model = new self();
36+
$model->setDevices($devices);
37+
return $model;
38+
}
39+
40+
/**
41+
* @return array|AggregateResponseItem[]
42+
*/
43+
public function getDevices(): array
44+
{
45+
return $this->devices;
46+
}
47+
48+
/**
49+
* @param array $devices
50+
* @return void
51+
*/
52+
public function setDevices(array $devices): void
53+
{
54+
$this->devices = $devices;
55+
}
56+
57+
}

src/Model/Stats/AggregateResponse.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright (C) 2013 Mailgun
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Mailgun\Model\Stats;
13+
14+
use Mailgun\Model\ApiResponse;
15+
16+
final class AggregateResponse implements ApiResponse
17+
{
18+
private array $providers = [];
19+
20+
private function __construct()
21+
{
22+
}
23+
24+
/**
25+
* @param array $data
26+
* @return self
27+
* @throws \Exception
28+
*/
29+
public static function create(array $data): self
30+
{
31+
$providers = [];
32+
foreach ($data['providers'] as $domain => $provider) {
33+
$providers[] = AggregateResponseItem::create($provider + ['domain' => $domain]);
34+
}
35+
$model = new self();
36+
$model->setProviders($providers);
37+
38+
return $model;
39+
}
40+
41+
/**
42+
* @return array|AggregateResponseItem[]
43+
*/
44+
public function getProviders(): array
45+
{
46+
return $this->providers;
47+
}
48+
49+
/**
50+
* @param array $providers
51+
* @return void
52+
*/
53+
public function setProviders(array $providers): void
54+
{
55+
$this->providers = $providers;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)