Completed
Push — master ( 36fb73...f4889a )
by Dmitry
02:53
created

Request::composeHeaderLines()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 1
crap 4
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\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 2
        } 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 2
            ],
66 2
        ];
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 2
        }
95
96 2
        return $contextOptions;
97
    }
98
99 2
    public static function composeHeaderLines($headers)
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 2
            }
107 2
            foreach ($values as $value) {
108 2
                $result[] = "$name: $value";
109 2
            }
110 2
        }
111
112 2
        return $result;
113
    }
114
}
115