2017-05-16 15:20:33 +02:00
|
|
|
<?php
|
2013-08-07 16:22:19 -07:00
|
|
|
|
2016-09-18 09:56:14 +02:00
|
|
|
/*
|
2017-11-22 00:37:04 -08:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2016-09-18 09:56:14 +02:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
2016-12-06 12:12:52 -06:00
|
|
|
* of the MIT license. See the LICENSE file for details.
|
2016-09-18 09:56:14 +02:00
|
|
|
*/
|
|
|
|
|
2013-08-07 16:22:19 -07:00
|
|
|
namespace Mailgun;
|
|
|
|
|
2016-10-24 19:01:32 +02:00
|
|
|
use Http\Client\Common\HttpMethodsClient;
|
2017-03-26 10:17:10 +02:00
|
|
|
use Mailgun\HttpClient\Plugin\History;
|
2017-03-22 07:44:08 +01:00
|
|
|
use Mailgun\Hydrator\ModelHydrator;
|
|
|
|
use Mailgun\Hydrator\Hydrator;
|
2017-03-26 10:17:10 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2013-08-07 16:22:19 -07:00
|
|
|
|
2014-11-20 12:41:25 -06:00
|
|
|
/**
|
|
|
|
* This class is the base class for the Mailgun SDK.
|
|
|
|
*/
|
2019-01-06 10:07:06 +01:00
|
|
|
final class Mailgun
|
2016-07-24 12:40:50 +01:00
|
|
|
{
|
2014-11-20 12:41:25 -06:00
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
2019-01-06 10:07:06 +01:00
|
|
|
private $apiKey;
|
2014-11-20 12:41:25 -06:00
|
|
|
|
|
|
|
/**
|
2016-10-24 19:01:32 +02:00
|
|
|
* @var HttpMethodsClient
|
|
|
|
*/
|
|
|
|
private $httpClient;
|
|
|
|
|
|
|
|
/**
|
2017-03-22 07:44:08 +01:00
|
|
|
* @var Hydrator
|
2016-10-24 19:01:32 +02:00
|
|
|
*/
|
2017-03-22 07:44:08 +01:00
|
|
|
private $hydrator;
|
2016-10-24 19:01:32 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-23 21:55:05 +01:00
|
|
|
* @var RequestBuilder
|
2016-10-24 19:01:32 +02:00
|
|
|
*/
|
2016-11-23 21:55:05 +01:00
|
|
|
private $requestBuilder;
|
2016-10-24 19:01:32 +02:00
|
|
|
|
2017-03-26 10:17:10 +02:00
|
|
|
/**
|
|
|
|
* This is a object that holds the last response from the API.
|
|
|
|
*
|
|
|
|
* @var History
|
|
|
|
*/
|
2019-01-06 10:07:06 +01:00
|
|
|
private $responseHistory;
|
2017-03-26 10:17:10 +02:00
|
|
|
|
2017-05-16 15:20:33 +02:00
|
|
|
public function __construct(
|
2019-01-06 10:07:06 +01:00
|
|
|
HttpClientConfigurator $configurator,
|
2017-03-22 07:44:08 +01:00
|
|
|
Hydrator $hydrator = null,
|
2016-11-23 21:55:05 +01:00
|
|
|
RequestBuilder $requestBuilder = null
|
2015-10-12 19:23:58 +02:00
|
|
|
) {
|
2016-11-23 21:55:05 +01:00
|
|
|
$this->requestBuilder = $requestBuilder ?: new RequestBuilder();
|
2017-03-22 07:44:08 +01:00
|
|
|
$this->hydrator = $hydrator ?: new ModelHydrator();
|
2017-03-25 13:48:03 +01:00
|
|
|
|
2019-01-06 10:07:06 +01:00
|
|
|
$this->httpClient = $configurator->createConfiguredClient();
|
|
|
|
$this->apiKey = $configurator->getApiKey();
|
|
|
|
$this->responseHistory = $configurator->getResponseHistory();
|
2017-03-25 13:48:03 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 10:07:06 +01:00
|
|
|
public static function create(string $apiKey, string $endpoint = 'https://api.mailgun.net'): self
|
2017-03-25 13:48:03 +01:00
|
|
|
{
|
2018-08-04 23:28:57 +02:00
|
|
|
$httpClientConfigurator = (new HttpClientConfigurator())
|
|
|
|
->setApiKey($apiKey)
|
|
|
|
->setEndpoint($endpoint);
|
2017-03-25 13:48:03 +01:00
|
|
|
|
2019-01-06 10:07:06 +01:00
|
|
|
return new self($httpClientConfigurator);
|
2014-11-19 14:29:08 -08:00
|
|
|
}
|
2017-03-26 10:17:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ResponseInterface|null
|
|
|
|
*/
|
|
|
|
public function getLastResponse()
|
|
|
|
{
|
|
|
|
return $this->responseHistory->getLastResponse();
|
|
|
|
}
|
2013-08-13 12:26:34 -07:00
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function stats(): Api\Stats
|
2016-10-24 19:01:32 +02:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-10-24 19:01:32 +02:00
|
|
|
}
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function attachment(): Api\Attachment
|
2018-08-09 18:59:38 +02:00
|
|
|
{
|
|
|
|
return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function domains(): Api\Domain
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
2016-12-07 19:03:50 +01:00
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function tags(): Api\Tag
|
2017-02-21 08:22:57 +01:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2017-02-21 08:22:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function events(): Api\Event
|
2016-12-07 19:03:50 +01:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-12-07 19:03:50 +01:00
|
|
|
}
|
2016-12-07 23:29:08 +01:00
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function routes(): Api\Route
|
2016-12-09 22:15:06 +00:00
|
|
|
{
|
2017-03-26 16:11:33 +02:00
|
|
|
return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-12-09 22:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function webhooks(): Api\Webhook
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
2017-04-08 01:29:20 +02:00
|
|
|
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function messages(): Api\Message
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|
2017-02-20 13:57:54 -06:00
|
|
|
|
2019-01-09 20:18:58 +01:00
|
|
|
public function ips(): Api\Ip
|
|
|
|
{
|
|
|
|
return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function suppressions(): Api\Suppression
|
2017-02-20 13:57:54 -06:00
|
|
|
{
|
2017-03-22 07:44:08 +01:00
|
|
|
return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2017-02-20 13:57:54 -06:00
|
|
|
}
|
2013-08-07 16:22:19 -07:00
|
|
|
}
|