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

Request   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 16
loc 104
ccs 0
cts 62
cp 0
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 20 3
A setCurlOptions() 0 16 3
A buildMethodOptions() 0 20 4
A buildHeaderLines() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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