Completed
Push — master ( 12fee2...e341a4 )
by Tobias
63:38 queued 38:40
created

Mailgun::verifyWebhookSignature()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
cc 6
nc 6
nop 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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $configurator->createConfiguredClient() of type object<Http\Client\Common\PluginClient> is incompatible with the declared type object<Http\Client\Common\HttpMethodsClient> of property $httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
    /**
81
     * @return Api\Stats
82
     */
83
    public function stats()
84
    {
85
        return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
86
    }
87
88
    /**
89
     * @return Api\Attachment
90
     */
91
    public function attachment()
92
    {
93
        return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
94
    }
95
96
    /**
97
     * @return Api\Domain
98
     */
99
    public function domains()
100
    {
101
        return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
102
    }
103
104
    /**
105
     * @return Api\Tag
106
     */
107
    public function tags()
108
    {
109
        return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
110
    }
111
112
    /**
113
     * @return Api\Event
114
     */
115
    public function events()
116
    {
117
        return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
118
    }
119
120
    /**
121
     * @return Api\Route
122
     */
123
    public function routes()
124
    {
125
        return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
126
    }
127
128
    /**
129
     * @return Api\Webhook
130
     */
131
    public function webhooks()
132
    {
133
        return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
134
    }
135
136
    /**
137
     * @return Api\Message
138
     */
139
    public function messages()
140
    {
141
        return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
142
    }
143
144
    /**
145
     * @return Api\Suppression
146
     */
147
    public function suppressions()
148
    {
149
        return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
150
    }
151
}
152