Completed
Push — master ( 67107a...4cfe5c )
by Vladymyr
02:45
created

CurlHttpClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 62
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 21 3
A get() 0 12 2
A checkExceptions() 0 6 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @link https://github.com/codenix-sv/bittrex-api
5
 * @copyright Copyright (c) 2017 codenix-sv
6
 * @license https://github.com/codenix-sv/bittrex-api/blob/master/LICENSE
7
 */
8
9
namespace codenixsv\Bittrex\Http;
10
11
use codenixsv\Bittrex\Exceptions\CurlException;
12
13
/**
14
 * Class CurlHttpClient
15
 * @package codenixsv\Bittrex\Http
16
 */
17
class CurlHttpClient implements HttpClient
18
{
19
    /**
20
     * @param $url
21
     * @param array $headers
22
     * @return mixed
23
     * @throws CurlException
24
     */
25
    public function get($url, array $headers = [])
26
    {
27
        $ch = curl_init($url);
28
        if (!empty($headers)) {
29
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
30
        }
31
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32
        $response = curl_exec($ch);
33
34
        $this->checkExceptions($ch);
35
36
        return $response;
37
    }
38
39
    /**
40
     * @param $url
41
     * @param array $parameters
42
     * @param array $headers
43
     * @return mixed
44
     * @throws CurlException
45
     */
46
    public function post($url, array $parameters = [], array $headers = [])
47
    {
48
        $ch = curl_init($url);
49
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50
        curl_setopt($ch, CURLOPT_POST, true);
51
52
        if (!empty($headers)) {
53
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
54
        }
55
56
        if (!empty($parameters)) {
57
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
58
        }
59
60
        $response = curl_exec($ch);
61
62
        $this->checkExceptions($ch);
63
64
        curl_close($ch);
65
66
        return $response;
67
    }
68
69
    /**
70
     * @param $ch
71
     * @throws CurlException
72
     */
73
    private function checkExceptions($ch)
74
    {
75
        if (curl_errno($ch)) {
76
            $errorMessage = curl_error($ch);
77
            curl_close($ch);
78
            throw new CurlException($errorMessage);
79
        }
80
    }
81
}
82