Completed
Push — master ( c8064d...729f46 )
by Dmitry
02:44
created

Request::buildMethodOptions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 0
crap 20
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
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
    public function send($options = [])
39
    {
40
        try {
41
            $this->build();
42
43
            $curl = curl_init($this->getFullUri());
44
            $this->setCurlOptions($curl);
45
            $response = curl_exec($curl);
46
            $info = curl_getinfo($curl);
47
            $error = curl_error($curl);
48
            $errorCode = curl_errno($curl);
49
            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
        return new $this->responseClass($this, $response, $info, $error, $errorCode);
57
    }
58
59
    /**
60
     * @param resource $curl
61
     * @throws RequestErrorException
62
     */
63
    protected function setCurlOptions($curl)
64
    {
65
        $requestOptions = $this->buildMethodOptions();
66
        $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines();
67
68
        if ($this->getVersion() === '1.1') {
69
            $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
70
        } elseif ($this->getVersion() === '1.0') {
71
            $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
72
        } else {
73
            throw new RequestErrorException('Request version "' . $this->getVersion() . '" is not support by cURL', $this);
74
        }
75
76
        $options = ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions);
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...
77
        curl_setopt_array($curl, $options);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    protected function buildMethodOptions()
84
    {
85
        $options = [];
86
87
        if ($this->getMethod() === 'GET') {
88
            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 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
        $result = [];
110
111
        foreach ($this->getHeaders() as $name => $values) {
112
            $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
113
            if (is_string($values)) {
114
                $values = [$values];
115
            }
116
            foreach ($values as $value) {
117
                $result[] = "$name: $value";
118
            }
119
        }
120
121
        return $result;
122
    }
123
}
124