Completed
Push — master ( a1ca1a...6a943d )
by David
01:56
created

Mailgun::emailValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2013 Mailgun
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license. See the LICENSE file for details.
10
 */
11
12
namespace Mailgun;
13
14
use Http\Client\HttpClient;
15
use Mailgun\HttpClient\HttpClientConfigurator;
16
use Mailgun\HttpClient\Plugin\History;
17
use Mailgun\HttpClient\RequestBuilder;
18
use Mailgun\Hydrator\ModelHydrator;
19
use Mailgun\Hydrator\Hydrator;
20
use Psr\Http\Message\ResponseInterface;
21
22
/**
23
 * This class is the base class for the Mailgun SDK.
24
 */
25
final class Mailgun
26
{
27
    /**
28
     * @var string|null
29
     */
30
    private $apiKey;
31
32
    /**
33
     * @var HttpClient
34
     */
35
    private $httpClient;
36
37
    /**
38
     * @var Hydrator
39
     */
40
    private $hydrator;
41
42
    /**
43
     * @var RequestBuilder
44
     */
45
    private $requestBuilder;
46
47
    /**
48
     * This is a object that holds the last response from the API.
49
     *
50
     * @var History
51
     */
52
    private $responseHistory;
53
54
    public function __construct(
55
        HttpClientConfigurator $configurator,
56
        Hydrator $hydrator = null,
57
        RequestBuilder $requestBuilder = null
58
    ) {
59
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
60
        $this->hydrator = $hydrator ?: new ModelHydrator();
61
62
        $this->httpClient = $configurator->createConfiguredClient();
63
        $this->apiKey = $configurator->getApiKey();
64
        $this->responseHistory = $configurator->getResponseHistory();
65
    }
66
67
    public static function create(string $apiKey, string $endpoint = 'https://api.mailgun.net'): self
68
    {
69
        $httpClientConfigurator = (new HttpClientConfigurator())
70
            ->setApiKey($apiKey)
71
            ->setEndpoint($endpoint);
72
73
        return new self($httpClientConfigurator);
74
    }
75
76
    /**
77
     * @return ResponseInterface|null
78
     */
79
    public function getLastResponse()
80
    {
81
        return $this->responseHistory->getLastResponse();
82
    }
83
84
    public function stats(): Api\Stats
85
    {
86
        return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
87
    }
88
89
    public function attachment(): Api\Attachment
90
    {
91
        return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
92
    }
93
94
    public function domains(): Api\Domain
95
    {
96
        return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
97
    }
98
99
    public function tags(): Api\Tag
100
    {
101
        return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
102
    }
103
104
    public function events(): Api\Event
105
    {
106
        return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
107
    }
108
109
    public function routes(): Api\Route
110
    {
111
        return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
112
    }
113
114
    public function webhooks(): Api\Webhook
115
    {
116
        return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
117
    }
118
119
    public function messages(): Api\Message
120
    {
121
        return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
122
    }
123
124
    public function ips(): Api\Ip
125
    {
126
        return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
127
    }
128
129
    public function suppressions(): Api\Suppression
130
    {
131
        return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
132
    }
133
134
    public function emailValidation(): Api\EmailValidation
135
    {
136
        return new Api\EmailValidation($this->httpClient, $this->requestBuilder, $this->hydrator);
137
    }
138
}
139