2017-05-16 16:20:33 +03:00
|
|
|
<?php
|
2013-08-08 03:22:19 +04:00
|
|
|
|
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;
|
|
|
|
|
2016-10-24 20:01:32 +03:00
|
|
|
use Http\Client\Common\HttpMethodsClient;
|
2017-03-26 11:17:10 +03:00
|
|
|
use Mailgun\HttpClient\Plugin\History;
|
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;
|
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
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
2019-01-06 12:07:06 +03:00
|
|
|
private $apiKey;
|
2014-11-20 21:41:25 +03:00
|
|
|
|
|
|
|
/**
|
2016-10-24 20:01:32 +03:00
|
|
|
* @var HttpMethodsClient
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2016-10-24 20:01:32 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Stats
|
|
|
|
*/
|
2016-11-24 00:21:15 +03:00
|
|
|
public function 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
|
|
|
|
2018-08-09 19:59:38 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Attachment
|
|
|
|
*/
|
|
|
|
public function attachment()
|
|
|
|
{
|
|
|
|
return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:34:27 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Domain
|
|
|
|
*/
|
2016-11-24 00:21:15 +03:00
|
|
|
public function domains()
|
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
|
|
|
|
2017-02-21 10:22:57 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Tag
|
|
|
|
*/
|
2017-03-22 09:44:08 +03:00
|
|
|
public function tags()
|
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
|
|
|
}
|
|
|
|
|
2016-12-07 21:03:50 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Event
|
|
|
|
*/
|
2016-12-08 01:29:08 +03:00
|
|
|
public function events()
|
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
|
|
|
|
2016-12-10 01:15:06 +03:00
|
|
|
/**
|
2017-03-26 17:11:33 +03:00
|
|
|
* @return Api\Route
|
2016-12-10 01:15:06 +03:00
|
|
|
*/
|
|
|
|
public function routes()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-12-08 01:29:08 +03:00
|
|
|
/**
|
|
|
|
* @return Api\Webhook
|
|
|
|
*/
|
|
|
|
public function webhooks()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Api\Message
|
|
|
|
*/
|
|
|
|
public function messages()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/**
|
2017-03-22 09:44:08 +03:00
|
|
|
* @return Api\Suppression
|
2017-02-20 22:57:54 +03:00
|
|
|
*/
|
|
|
|
public function suppressions()
|
|
|
|
{
|
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
|
|
|
}
|
2013-08-08 03:22:19 +04:00
|
|
|
}
|