Completed
Push — master ( f2f464...1707fe )
by Andrii
16:25 queued 11:15
created

src/Request.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
            curl_setopt_array($curl, $this->prepareCurlOptions($options));
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 array $options
61
     * @throws RequestErrorException
62
     * @return array
63
     */
64
    protected function prepareCurlOptions($options)
65
    {
66
        $requestOptions = $this->buildMethodOptions();
67
        $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines();
68
69
        if ($this->getVersion() === '1.1') {
70
            $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
        return ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions, $options);
0 ignored issues
show
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
    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
    protected function buildHeaderLines()
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