mailgun-php/src/Mailgun.php

152 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/*
* 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\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
2016-07-24 14:40:50 +03:00
{
/**
* @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
{
2018-08-05 00:28:57 +03:00
$httpClientConfigurator = (new HttpClientConfigurator())
->setApiKey($apiKey)
->setEndpoint($endpoint);
return new self($httpClientConfigurator);
}
/**
* @return ResponseInterface|null
*/
public function getLastResponse()
{
return $this->responseHistory->getLastResponse();
}
2013-08-13 23:26:34 +04:00
/**
* @return Api\Stats
*/
public function stats()
{
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
}
2016-10-27 09:34:27 +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
*/
public function domains()
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
}
/**
* @return Api\Tag
*/
public function tags()
{
return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
}
/**
* @return Api\Event
*/
public function events()
{
return new Api\Event($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
/**
* @return 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
*/
public function routes()
{
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
}
/**
* @return Api\Webhook
*/
public function webhooks()
{
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
}
/**
* @return Api\Message
*/
public function messages()
{
return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
}
/**
* @return Api\Suppression
*/
public function suppressions()
{
return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
}
}