mailgun-php/src/Mailgun.php
Tobias Nyholm b725ab728e
Adding PHP7 type annotations (#523)
* Adding PHP7 type annotations

* cs

* Moved HttpClientConfigurator and RequestBuilder to Mailgun\HttpClient namespace

* fixing tests

* Rebased and fixed tests

* minors

* cs

* Bugfixes

* Typo
2019-01-09 20:18:58 +01:00

130 lines
3.2 KiB
PHP

<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun;
use Http\Client\Common\HttpMethodsClient;
use Mailgun\HttpClient\Plugin\History;
use Mailgun\Hydrator\ModelHydrator;
use Mailgun\Hydrator\Hydrator;
use Psr\Http\Message\ResponseInterface;
/**
* This class is the base class for the Mailgun SDK.
*/
final class Mailgun
{
/**
* @var null|string
*/
private $apiKey;
/**
* @var HttpMethodsClient
*/
private $httpClient;
/**
* @var Hydrator
*/
private $hydrator;
/**
* @var RequestBuilder
*/
private $requestBuilder;
/**
* This is a object that holds the last response from the API.
*
* @var History
*/
private $responseHistory;
public function __construct(
HttpClientConfigurator $configurator,
Hydrator $hydrator = null,
RequestBuilder $requestBuilder = null
) {
$this->requestBuilder = $requestBuilder ?: new RequestBuilder();
$this->hydrator = $hydrator ?: new ModelHydrator();
$this->httpClient = $configurator->createConfiguredClient();
$this->apiKey = $configurator->getApiKey();
$this->responseHistory = $configurator->getResponseHistory();
}
public static function create(string $apiKey, string $endpoint = 'https://api.mailgun.net'): self
{
$httpClientConfigurator = (new HttpClientConfigurator())
->setApiKey($apiKey)
->setEndpoint($endpoint);
return new self($httpClientConfigurator);
}
/**
* @return ResponseInterface|null
*/
public function getLastResponse()
{
return $this->responseHistory->getLastResponse();
}
public function stats(): Api\Stats
{
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function attachment(): Api\Attachment
{
return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function domains(): Api\Domain
{
return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function tags(): Api\Tag
{
return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function events(): Api\Event
{
return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function routes(): Api\Route
{
return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function webhooks(): Api\Webhook
{
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
}
public function messages(): Api\Message
{
return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function ips(): Api\Ip
{
return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function suppressions(): Api\Suppression
{
return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
}
}