Completed
Push — master ( b3280e...273759 )
by Janusz
02:35
created

HttpClient::sendRequest()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 8.439
cc 5
eloc 16
nc 5
nop 3
crap 5
1
<?php
2
3
namespace Ittoolspl\Smslabs;
4
5
use GuzzleHttp\Client;
6
use Ittoolspl\Smslabs\Exception\InvalidResponseException;
7
8
class HttpClient
9
{
10
    const API_URL = 'https://api.smslabs.net.pl/apiSms';
11
12
    /**
13
     * @var string
14
     */
15
    private $appKey = null;
16
17
    /**
18
     * @var string
19
     */
20
    private $secretKey = null;
21
22
    /**
23
     * @var Client
24
     */
25
    private $client = null;
26
27
    /**
28
     * HttpClient constructor.
29
     * @param string $appKey
30
     * @param string $secretKey
31
     */
32 27
    public function __construct($appKey, $secretKey)
33
    {
34 27
        $this->appKey    = $appKey;
35 27
        $this->secretKey = $secretKey;
36 27
    }
37
38
    /**
39
     * @param string $url
40
     * @param array $data
41
     * @param string $method
42
     * @return array
43
     * @throws InvalidResponseException
44
     */
45 5
    public function sendRequest($url, $data = null, $method = 'GET')
46
    {
47 5
        if ($this->validateHttpMethod($method) === false) {
48 1
            throw new \InvalidArgumentException('Invalid method');
49
        }
50
51 4
        $response = $this->getClient()->request($method, self::API_URL.$url, [
52 4
            'auth' => [$this->appKey, $this->secretKey],
53 4
            'form_params' => $data,
54 4
        ]);
55
56 4
        if ($response->getStatusCode() != 200) {
57 1
            throw new InvalidResponseException('Invalid HTTP code');
58
        }
59
60 3
        $bodyJson = (string)$response->getBody();
61
62
        try {
63 3
            $bodyArr = \GuzzleHttp\json_decode($bodyJson, true);
64 3
        } catch (\InvalidArgumentException $e) {
65 2
            throw new InvalidResponseException('Invalid JSON data');
66
        }
67
68 2
        if (!array_key_exists('data', $bodyArr)) {
69 1
            throw new InvalidResponseException('Missing data property in response');
70
        }
71
72 1
        return $bodyArr['data'];
73
    }
74
75
    /**
76
     * @param string $method
77
     * @return bool
78
     */
79 5
    private function validateHttpMethod($method)
80
    {
81 5
        return in_array($method, ['POST', 'PUT', 'GET', 'DELETE']);
82
    }
83
84
    /**
85
     * @return Client
86
     */
87 5
    public function getClient()
88
    {
89 5
        if ($this->client === null) {
90 1
            $this->client = new Client();
91 1
        }
92
93 5
        return $this->client;
94
    }
95
96
    /**
97
     * @param Client $client
98
     */
99 4
    public function setClient(Client $client)
100
    {
101 4
        $this->client = $client;
102 4
    }
103
104
    /**
105
     * @return string
106
     */
107 3
    public function getAppKey()
108
    {
109 3
        return $this->appKey;
110
    }
111
112
    /**
113
     * @param string $appKey
114
     * @return HttpClient $this
115
     */
116 1
    public function setAppKey($appKey)
117
    {
118 1
        $this->appKey = $appKey;
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * @return string
125
     */
126 3
    public function getSecretKey()
127
    {
128 3
        return $this->secretKey;
129
    }
130
131
    /**
132
     * @param string $secretKey
133
     * @return HttpClient $this
134
     */
135 1
    public function setSecretKey($secretKey)
136
    {
137 1
        $this->secretKey = $secretKey;
138
139 1
        return $this;
140
    }
141
}
142