mailgun-php/src/Mailgun.php

142 lines
3.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* 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.
*/
namespace Mailgun;
use Http\Client\Common\PluginClient;
2019-01-09 22:52:17 +03:00
use Mailgun\HttpClient\HttpClientConfigurator;
use Mailgun\HttpClient\Plugin\History;
2019-01-09 22:52:17 +03:00
use Mailgun\HttpClient\RequestBuilder;
use Mailgun\Hydrator\ModelHydrator;
use Mailgun\Hydrator\Hydrator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Client\ClientInterface;
/**
* This class is the base class for the Mailgun SDK.
*/
class Mailgun
2016-07-24 14:40:50 +03:00
{
/**
* @var string|null
*/
private $apiKey;
/**
* @var ClientInterface|PluginClient
*/
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
{
2018-08-05 00:28:57 +03:00
$httpClientConfigurator = (new HttpClientConfigurator())
->setApiKey($apiKey)
->setEndpoint($endpoint);
return new self($httpClientConfigurator);
}
2019-04-19 09:56:41 +03:00
public function getLastResponse(): ?ResponseInterface
{
return $this->responseHistory->getLastResponse();
}
2013-08-13 23:26:34 +04:00
public function stats(): Api\Stats
{
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
}
2016-10-27 09:34:27 +03:00
public function attachment(): Api\Attachment
{
return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
}
public function domains(): Api\Domain
2016-10-27 09:34:27 +03:00
{
return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
2016-10-27 09:34:27 +03:00
}
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
Routes API (#249) * Add initial (empty) Routes PHP Unit Test file We still need to provide the automated tests * Add initial Routes file * Describe API Methods * Inherit method from TestCase - adding @inheritdoc annotation * Add new DTOs to map API responses to known objects * Add new Response to manage the Routes list * Implement method to retrieve a list of Routes * Add new Response to manage a single Route resource * Implement method to retrieve a single Route * Set ShowResponse as final * Add new Response to manage the Create process * Implement method to create a new Route * Fix missing annotation * Add new Response to manage the Delete Route process * Implement method to delete a Route based on the ID * Add new Response to manage the Update Route process This response is based on Domain API docs due there are no examples on Routes API docs. We may need to update the response. * Implement method to update a Route based on the ID * Require a $limit value greater than 0 * Require a $skip value greater than or equal to 0 * Set UpdateResponse as final * Add new (empty) public methods to test the Routes API * Provide method to get the Routes API from Mailgun Client * Add missed annotation * Update ShowResponse to return an instance of ApiResponse instead of the DTO * Update annotation * Fix annotation * Update array $actions to provide an empty array by default * Update parameters to make sure the last arg always is a DateTime (or null) * Use empty() * Remove DTO suffix * Move DTOs to the parent folder/namespace * Fix annotations
2016-12-10 01:15:06 +03:00
{
return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
Routes API (#249) * Add initial (empty) Routes PHP Unit Test file We still need to provide the automated tests * Add initial Routes file * Describe API Methods * Inherit method from TestCase - adding @inheritdoc annotation * Add new DTOs to map API responses to known objects * Add new Response to manage the Routes list * Implement method to retrieve a list of Routes * Add new Response to manage a single Route resource * Implement method to retrieve a single Route * Set ShowResponse as final * Add new Response to manage the Create process * Implement method to create a new Route * Fix missing annotation * Add new Response to manage the Delete Route process * Implement method to delete a Route based on the ID * Add new Response to manage the Update Route process This response is based on Domain API docs due there are no examples on Routes API docs. We may need to update the response. * Implement method to update a Route based on the ID * Require a $limit value greater than 0 * Require a $skip value greater than or equal to 0 * Set UpdateResponse as final * Add new (empty) public methods to test the Routes API * Provide method to get the Routes API from Mailgun Client * Add missed annotation * Update ShowResponse to return an instance of ApiResponse instead of the DTO * Update annotation * Fix annotation * Update array $actions to provide an empty array by default * Update parameters to make sure the last arg always is a DateTime (or null) * Use empty() * Remove DTO suffix * Move DTOs to the parent folder/namespace * Fix annotations
2016-12-10 01:15:06 +03:00
}
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);
}
2019-01-10 01:28:44 +03:00
public function emailValidation(): Api\EmailValidation
{
return new Api\EmailValidation($this->httpClient, $this->requestBuilder, $this->hydrator);
}
2019-05-16 19:03:13 +03:00
public function mailingList(): Api\MailingList
{
return new Api\MailingList($this->httpClient, $this->requestBuilder, $this->hydrator);
}
}