Completed
Push — master ( f30985...ae9a54 )
by David
01:31
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\Common\PluginClient;
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
use Psr\Http\Client\ClientInterface;
22
23
/**
24
 * This class is the base class for the Mailgun SDK.
25
 */
26
class Mailgun
27
{
28
    /**
29
     * @var string|null
30
     */
31
    private $apiKey;
32
33
    /**
34
     * @var ClientInterface|PluginClient
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
    public function getLastResponse(): ?ResponseInterface
78
    {
79
        return $this->responseHistory->getLastResponse();
80
    }
81
82
    public function attachment(): Api\Attachment
83
    {
84
        return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
    }
86
87
    public function domains(): Api\Domain
88
    {
89
        return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
    }
91
92
    public function emailValidation(): Api\EmailValidation
93
    {
94
        return new Api\EmailValidation($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
    }
96
97
    public function events(): Api\Event
98
    {
99
        return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
100
    }
101
102
    public function ips(): Api\Ip
103
    {
104
        return new Api\Ip($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
105
    }
106
107
    public function mailingList(): Api\MailingList
108
    {
109
        return new Api\MailingList($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110
    }
111
112
    public function messages(): Api\Message
113
    {
114
        return new Api\Message($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
115
    }
116
117
    public function routes(): Api\Route
118
    {
119
        return new Api\Route($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
    }
121
122
    public function suppressions(): Api\Suppression
123
    {
124
        return new Api\Suppression($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\Suppression::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
125
    }
126
127
    public function stats(): Api\Stats
128
    {
129
        return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
    }
131
132
    public function tags(): Api\Tag
133
    {
134
        return new Api\Tag($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\HttpApi::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
135
    }
136
137
    public function webhooks(): Api\Webhook
138
    {
139
        return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Http\Client\Common\PluginClient>; however, Mailgun\Api\Webhook::__construct() does only seem to accept object<Psr\Http\Client\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
140
    }
141
}
142