Completed
Push — master ( cbb135...332992 )
by David
02:34
created

Mailgun::mailingList()   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\Api\MailingList;
16
use Mailgun\HttpClient\HttpClientConfigurator;
17
use Mailgun\HttpClient\Plugin\History;
18
use Mailgun\HttpClient\RequestBuilder;
19
use Mailgun\Hydrator\ModelHydrator;
20
use Mailgun\Hydrator\Hydrator;
21
use Psr\Http\Message\ResponseInterface;
22
23
/**
24
 * This class is the base class for the Mailgun SDK.
25
 */
26
final class Mailgun
27
{
28
    /**
29
     * @var string|null
30
     */
31
    private $apiKey;
32
33
    /**
34
     * @var HttpClient
35
     */
36
    private $httpClient;
37
38
    /**
39
     * @var Hydrator
40
     */
41
    private $hydrator;
42
43
    /**
44
     * @var RequestBuilder
45
     */
46
    private $requestBuilder;
47
48
    /**
49
     * This is a object that holds the last response from the API.
50
     *
51
     * @var History
52
     */
53
    private $responseHistory;
54
55
    public function __construct(
56
        HttpClientConfigurator $configurator,
57
        Hydrator $hydrator = null,
58
        RequestBuilder $requestBuilder = null
59
    ) {
60
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
61
        $this->hydrator = $hydrator ?: new ModelHydrator();
62
63
        $this->httpClient = $configurator->createConfiguredClient();
64
        $this->apiKey = $configurator->getApiKey();
65
        $this->responseHistory = $configurator->getResponseHistory();
66
    }
67
68
    public static function create(string $apiKey, string $endpoint = 'https://api.mailgun.net'): self
69
    {
70
        $httpClientConfigurator = (new HttpClientConfigurator())
71
            ->setApiKey($apiKey)
72
            ->setEndpoint($endpoint);
73
74
        return new self($httpClientConfigurator);
75
    }
76
77
    /**
78
     * @return ResponseInterface|null
79
     */
80
    public function getLastResponse()
81
    {
82
        return $this->responseHistory->getLastResponse();
83
    }
84
85
    public function stats(): Api\Stats
86
    {
87
        return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
88
    }
89
90
    public function attachment(): Api\Attachment
91
    {
92
        return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
93
    }
94
95
    public function domains(): Api\Domain
96
    {
97
        return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
98
    }
99
100
    public function tags(): Api\Tag
101
    {
102
        return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
103
    }
104
105
    public function events(): Api\Event
106
    {
107
        return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
108
    }
109
110
    public function routes(): Api\Route
111
    {
112
        return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
113
    }
114
115
    public function webhooks(): Api\Webhook
116
    {
117
        return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
118
    }
119
120
    public function messages(): Api\Message
121
    {
122
        return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
123
    }
124
125
    public function mailingList(): MailingList
126
    {
127
        return new MailingList($this->httpClient, $this->requestBuilder, $this->hydrator);
128
    }
129
130
    public function ips(): Api\Ip
131
    {
132
        return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
133
    }
134
135
    public function suppressions(): Api\Suppression
136
    {
137
        return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
138
    }
139
140
    public function emailValidation(): Api\EmailValidation
141
    {
142
        return new Api\EmailValidation($this->httpClient, $this->requestBuilder, $this->hydrator);
143
    }
144
}
145