Completed
Push — master ( c30967...3bd52a )
by Andrii
03:06
created

Request::send()   A

Complexity

Conditions 3
Paths 17

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 14
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 15
nc 17
nop 1
crap 3.2098
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\curl;
12
13
use hiqdev\hiart\AbstractRequest;
14
use hiqdev\hiart\RequestErrorException;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Class Request represents request using cURL library.
19
 */
20
class Request extends AbstractRequest
21
{
22
    protected $responseClass = Response::class;
23
24
    /**
25
     * @var array default cURL options
26
     */
27
    public $defaultOptions = [
28
        CURLOPT_SSL_VERIFYPEER => false,
29
        CURLOPT_RETURNTRANSFER => true,
30
        CURLOPT_HEADER => true,
31
    ];
32
33
    /**
34
     * @param array $options
35
     * @throws RequestErrorException
36
     * @return array|mixed
37
     */
38 2
    public function send($options = [])
39
    {
40
        try {
41 2
            $this->build();
42
43 2
            $curl = curl_init($this->getFullUri());
44 2
            curl_setopt_array($curl, $this->prepareCurlOptions($options));
45 2
            $response = curl_exec($curl);
46 2
            $info = curl_getinfo($curl);
47 2
            $error = curl_error($curl);
48 2
            $errorCode = curl_errno($curl);
49 2
            curl_close($curl);
50
        } catch (RequestErrorException $e) {
51
            throw $e;
52
        } catch (\Exception $e) {
53
            throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e);
54
        }
55
56 2
        return new $this->responseClass($this, $response, $info, $error, $errorCode);
57
    }
58
59
    /**
60
     * @param array $options
61
     * @throws RequestErrorException
62
     * @return array
63
     */
64 2
    protected function prepareCurlOptions($options)
65
    {
66 2
        $requestOptions = $this->buildMethodOptions();
67 2
        $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines();
68
69 2
        if ($this->getVersion() === '1.1') {
70 2
            $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
71
        } elseif ($this->getVersion() === '1.0') {
72
            $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
73
        } else {
74
            throw new RequestErrorException('Request version "' . $this->getVersion() . '" is not support by cURL', $this);
75
        }
76
77 2
        return ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions, $options);
0 ignored issues
show
Bug introduced by
Accessing config on the interface hiqdev\hiart\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
    }
79
80
    /**
81
     * @return array
82
     */
83 2
    protected function buildMethodOptions()
84
    {
85 2
        $options = [];
86
87 2
        if ($this->getMethod() === 'GET') {
88 2
            return $options;
89
        }
90
91
        if (!empty($this->getBody())) {
92
            $options[CURLOPT_POSTFIELDS] = $this->getBody();
93
        }
94
95
        if ($this->getMethod() === 'POST') {
96
            $options[CURLOPT_POST] = true;
97
        } else {
98
            $options[CURLOPT_CUSTOMREQUEST] = $this->getMethod();
99
        }
100
101
        return $options;
102
    }
103
104
    /**
105
     * @return array
106
     */
107 2 View Code Duplication
    protected function buildHeaderLines()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 2
        $result = [];
110
111 2
        foreach ($this->getHeaders() as $name => $values) {
112 2
            $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
113 2
            if (is_string($values)) {
114 2
                $values = [$values];
115
            }
116 2
            foreach ($values as $value) {
117 2
                $result[] = "$name: $value";
118
            }
119
        }
120
121 2
        return $result;
122
    }
123
124 2
    public static function isSupported()
125
    {
126 2
        return function_exists('curl_version');
127
    }
128
}
129