2017-05-16 15:20:33 +02:00
|
|
|
<?php
|
2013-08-07 16:22:19 -07:00
|
|
|
|
2019-01-09 20:32:09 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
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;
|
|
|
|
|
2019-02-02 08:30:04 +01:00
|
|
|
use Http\Client\Common\PluginClient;
|
2019-01-09 20:52:17 +01:00
|
|
|
use Mailgun\HttpClient\HttpClientConfigurator;
|
2017-03-26 10:17:10 +02:00
|
|
|
use Mailgun\HttpClient\Plugin\History;
|
2019-01-09 20:52:17 +01:00
|
|
|
use Mailgun\HttpClient\RequestBuilder;
|
2017-03-22 07:44:08 +01:00
|
|
|
use Mailgun\Hydrator\Hydrator;
|
2020-03-30 19:18:19 -03:00
|
|
|
use Mailgun\Hydrator\ModelHydrator;
|
2019-02-02 08:30:04 +01:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
2020-03-30 19:18:19 -03: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-04-09 20:37:32 +02:00
|
|
|
class Mailgun
|
2016-07-24 12:40:50 +01:00
|
|
|
{
|
2014-11-20 12:41:25 -06:00
|
|
|
/**
|
2019-01-10 20:29:58 +01:00
|
|
|
* @var string|null
|
2014-11-20 12:41:25 -06:00
|
|
|
*/
|
2019-01-06 10:07:06 +01:00
|
|
|
private $apiKey;
|
2014-11-20 12:41:25 -06:00
|
|
|
|
|
|
|
/**
|
2019-02-02 08:30:04 +01:00
|
|
|
* @var ClientInterface|PluginClient
|
2016-10-24 19:01:32 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2019-04-19 08:56:41 +02:00
|
|
|
public function getLastResponse(): ?ResponseInterface
|
2017-03-26 10:17:10 +02:00
|
|
|
{
|
|
|
|
return $this->responseHistory->getLastResponse();
|
|
|
|
}
|
2013-08-13 12:26:34 -07: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-05-16 20:54:46 +02:00
|
|
|
public function emailValidation(): Api\EmailValidation
|
2017-02-21 08:22:57 +01:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\EmailValidation($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-05-16 20:54:46 +02:00
|
|
|
public function ips(): Api\Ip
|
2016-12-09 22:15:06 +00:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2016-12-09 22:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 20:54:46 +02:00
|
|
|
public function mailingList(): Api\MailingList
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\MailingList($this->httpClient, $this->requestBuilder, $this->hydrator);
|
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-05-16 20:54:46 +02:00
|
|
|
public function routes(): Api\Route
|
2019-01-09 20:18:58 +01:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2019-01-09 20:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-01-09 22:28:44 +00:00
|
|
|
|
2019-05-16 20:54:46 +02:00
|
|
|
public function stats(): Api\Stats
|
2019-01-09 22:28:44 +00:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
|
2019-01-09 22:28:44 +00:00
|
|
|
}
|
2019-05-16 18:03:13 +02:00
|
|
|
|
2019-05-16 20:54:46 +02:00
|
|
|
public function tags(): Api\Tag
|
2019-05-16 18:03:13 +02:00
|
|
|
{
|
2019-05-16 20:54:46 +02:00
|
|
|
return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function webhooks(): Api\Webhook
|
|
|
|
{
|
|
|
|
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
|
2019-05-16 18:03:13 +02:00
|
|
|
}
|
2013-08-07 16:22:19 -07:00
|
|
|
}
|