Completed
Push — master ( 4a903b...b725ab )
by Tobias
03:23 queued 53s
created

Mailgun   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 12
dl 0
loc 109
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

13 Methods

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