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