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

Request::send()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 6
nop 1
crap 2.032
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\stream;
12
13
use hiqdev\hiart\AbstractRequest;
14
use hiqdev\hiart\RequestErrorException;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Inflector;
17
18
/**
19
 * PHP stream request implementation.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Request extends AbstractRequest
24
{
25
    protected $responseClass = Response::class;
26
27
    public $defaultOptions = [
28
        'http' => [
29
            'ignore_errors' => true,
30
        ],
31
        'ssl' => [
32
            'verify_peer' => false,
33
        ],
34
    ];
35
36
    /**
37
     * @param array $options
38
     * @throws RequestErrorException
39
     * @return mixed
40
     */
41 2
    public function send($options = [])
42
    {
43
        try {
44 2
            $this->build();
45 2
            $context = stream_context_create($this->prepareContextOptions($options));
46 2
            $stream = fopen($this->getFullUri(), 'rb', false, $context);
47 2
            $responseContent = stream_get_contents($stream);
48
            // see http://php.net/manual/en/reserved.variables.httpresponseheader.php
49 2
            $responseHeaders = $http_response_header;
50 2
            fclose($stream);
51
        } catch (\Exception $e) {
52
            throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e);
53
        }
54
55 2
        return new $this->responseClass($this, $responseContent, $responseHeaders);
56
    }
57
58 2
    protected function prepareContextOptions($options)
59
    {
60
        $requestOptions = [
61
            'http' => [
62 2
                'protocol_version' => $this->version,
63 2
                'method' => $this->method,
64 2
                'header' => static::composeHeaderLines($this->headers),
65
            ],
66
        ];
67
68 2
        if (isset($this->body)) {
69
            $requestOptions['http']['content'] = $this->body;
70
        }
71
72 2
        $dbOptions = $this->convertContextOptions($this->getDb()->config);
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...
73 2
        $sendOptions = $this->convertContextOptions($options);
74
75 2
        return ArrayHelper::merge($this->defaultOptions, $dbOptions, $requestOptions, $sendOptions);
76
    }
77
78
    /**
79
     * Composes stream context options from raw options.
80
     * @param array $options raw options
81
     * @return array stream context options
82
     */
83 2
    protected function convertContextOptions(array $options)
84
    {
85 2
        $contextOptions = [];
86 2
        foreach ($options as $key => $value) {
87
            $section = 'http';
88
            if (strpos($key, 'ssl') === 0) {
89
                $section = 'ssl';
90
                $key = substr($key, 3);
91
            }
92
            $key = Inflector::underscore($key);
93
            $contextOptions[$section][$key] = $value;
94
        }
95
96 2
        return $contextOptions;
97
    }
98
99 2 View Code Duplication
    public static function composeHeaderLines($headers)
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...
100
    {
101 2
        $result = [];
102 2
        foreach ($headers as $name => $values) {
103 2
            $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
104 2
            if (is_string($values)) {
105 2
                $values = [$values];
106
            }
107 2
            foreach ($values as $value) {
108 2
                $result[] = "$name: $value";
109
            }
110
        }
111
112 2
        return $result;
113
    }
114
}
115