MailxpertClient::getAPIUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sources.
4
 * Date: 14/08/15
5
 */
6
7
namespace Mailxpert;
8
9
use Mailxpert\HttpClients\MailxpertCurlHttpClient;
10
use Mailxpert\HttpClients\MailxpertHttpClientInterface;
11
use Mailxpert\HttpClients\MailxpertStreamHttpClient;
12
13
/**
14
 * Class MailxpertClient
15
 * @package Mailxpert
16
 */
17
class MailxpertClient
18
{
19
    /**
20
     * @const string Production API URL.
21
     */
22
    const BASE_API_URL = 'https://api.mailxpert.ch';
23
24
    /**
25
     * @const string API Version
26
     */
27
    const API_VERSION = 'v2.0';
28
29
    /**
30
     * @const int The timeout in seconds for a normal request.
31
     */
32
    const DEFAULT_REQUEST_TIMEOUT = 60;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $apiBaseUrl;
38
39
    /**
40
     * @var MailxpertHttpClientInterface
41
     */
42
    protected $httpClientHandler;
43
44
    /**
45
     * @param MailxpertHttpClientInterface|null $httpClientHandler
46
     * @param string|null                       $apiBaseUrl
47
     */
48
    public function __construct(MailxpertHttpClientInterface $httpClientHandler = null, $apiBaseUrl = null)
49
    {
50
        $this->httpClientHandler = $httpClientHandler ?: $this->detectHttpClientHandler();
51
52
        if (is_null($apiBaseUrl)) {
53
            $apiBaseUrl = static::BASE_API_URL;
54
        }
55
56
        $this->apiBaseUrl = $apiBaseUrl;
57
    }
58
59
    /**
60
     * @param MailxpertHttpClientInterface $httpClientHandler
61
     */
62
    public function setHttpClientHandler(MailxpertHttpClientInterface $httpClientHandler)
63
    {
64
        $this->httpClientHandler = $httpClientHandler;
65
    }
66
67
    /**
68
     * @return MailxpertHttpClientInterface
69
     */
70
    public function getHttpClientHandler()
71
    {
72
        return $this->httpClientHandler;
73
    }
74
75
    /**
76
     * @return MailxpertCurlHttpClient|MailxpertStreamHttpClient
77
     */
78
    public function detectHttpClientHandler()
79
    {
80
        return function_exists('curl_init') ? new MailxpertCurlHttpClient() : new MailxpertStreamHttpClient();
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getAPIUrl()
87
    {
88
        return $this->apiBaseUrl.'/'.static::API_VERSION;
89
    }
90
91
    /**
92
     * @param MailxpertRequest $request
93
     *
94
     * @return MailxpertResponse
95
     * @throws Exceptions\MailxpertSDKException
96
     */
97
    public function sendRequest(MailxpertRequest $request)
98
    {
99
        list($url, $method, $headers, $body) = $this->prepareRequestMessage($request);
100
101
        $timeOut = static::DEFAULT_REQUEST_TIMEOUT;
102
103
        $rawResponse = $this->getHttpClientHandler()->send($url, $method, $body, $headers, $timeOut);
104
105
        $returnResponse = new MailxpertResponse(
106
            $request,
107
            $rawResponse->getBody(),
108
            $rawResponse->getHttpResponseCode(),
109
            $rawResponse->getHeaders()
110
        );
111
112
        if ($returnResponse->isError()) {
113
            throw $returnResponse->getThrownException();
114
        }
115
116
        return $returnResponse;
117
    }
118
119
    private function prepareRequestMessage(MailxpertRequest $request)
120
    {
121
        $url = $request->getEndpoint();
122
123
        if (strpos($url, 'http') !== 0) {
124
            $url = strpos($url, '/') === 0 ? $url : '/'.$url;
125
            $url = $this->getAPIUrl().$url;
126
        }
127
128
        if ($request->getParams()) {
129
            $url .= '?'.http_build_query($request->getParams(), null, '&');
130
        }
131
132
        $body = $request->getBody();
133
134
        return [
135
            $url,
136
            $request->getMethod(),
137
            $request->getHeaders(),
138
            $body,
139
        ];
140
    }
141
}
142