Completed
Pull Request — master (#18)
by Auke
02:41
created

ClientStream   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.66%

Importance

Changes 10
Bugs 4 Features 1
Metric Value
wmc 19
c 10
b 4
f 1
lcom 1
cbo 3
dl 0
loc 146
ccs 52
cts 58
cp 0.8966
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A delete() 0 4 1
B mergeHeaders() 0 17 5
B request() 0 29 5
A post() 0 4 1
A put() 0 4 1
A headers() 0 16 4
1
<?php
2
3
/*
4
 * This file is part of the Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace arc\http;
13
14
/**
15
 * Class ClientStream
16
 * Implements a HTTP client using PHP's stream handling.
17
 * @package arc\http
18
 */
19
class ClientStream implements Client
20
{
21
    private $options = ['headers' => []];
22
23
    public $responseHeaders = null;
24
    public $requestHeaders = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
25
26
    /**
27
     * Merges header string and headers array to single string with all headers
28
     * @return string
29
     */
30 8
    private function mergeHeaders() {
31 8
        $args = func_get_args();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32 8
        $result = '';
33 8
        foreach ( $args as $headers ) {
34 8
            if (is_array($headers) || $headers instanceof \ArrayObject ) {
35 1
                $result .= array_reduce( (array) $headers, function($carry, $entry) {
36 1
                    return $carry . "\r\n" . $entry;
37 1
                }, '');
38 1
            } else {
39 8
                $result .= (string) $headers;
40
            }
41 8
        }
42 8
        if (substr($result, -2)!="\r\n") {
43 8
            $result .= "\r\n";
44 8
        }
45 8
        return $result;
46
    }
47
48
    /**
49
     * Send a HTTP request and return the response
50
     * @param string       $method  The method to use, GET, POST, etc.
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     * @param string       $url     The URL to request
52
     * @param array|string $request The query string
53
     * @param array        $options Any of the HTTP stream context options, e.g. extra headers.
54
     * @return string
55
     */
56 8
    public function request( $type, $url, $request = null, $options = [] )
57
    {
58 8
        $url = \arc\url::url( (string) $url);
59 8
        if ($type == 'GET' && $request) {
60 1
            $url->query->import( $request);
0 ignored issues
show
Documentation introduced by
The property $query is declared private in arc\url\Url. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61 1
            $request = null;
62 1
        }
63
64
        $options = [
65 8
            'method' => $type,
66
            'content' => $request
67 8
        ] + $options;
68
69 8
        $options['headers'] = $this->mergeHeaders(
70 8
            \arc\hash::get('header', $this->options),
71 8
            \arc\hash::get('headers', $this->options),
72 8
            \arc\hash::get('header', $options),
73 8
            \arc\hash::get('headers', $options)
74 8
        );
75
76 8
        $options += (array) $this->options;
77
78 8
        $context = stream_context_create( [ 'http' => $options ] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
79 8
        $result  = @file_get_contents( (string) $url, false, $context );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 2 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80 8
        $this->responseHeaders = isset($http_response_header) ? $http_response_header : null; //magic php variable set by file_get_contents.
81 8
        $this->requestHeaders  = isset($options['headers']) ? explode("\r\n",$options['headers']) : [];
82
83 8
        return $result;
84
    }
85
86
    /**
87
     * @param array $options Any of the HTTP stream context options, e.g. extra headers.
88
     */
89 10
    public function __construct( $options = [] )
90
    {
91 10
        $this->options = $options;
92 10
    }
93
94
    /**
95
     * Send a GET request
96
     * @param string         $url     The URL to request
97
     * @param array|string   $request The query string
98
     * @param array          $options Any of the HTTP stream context options, e.g. extra headers.
99
     * @return string
100
     */
101 7
    public function get( $url, $request = null, $options = [] )
102
    {
103 7
        return $this->request( 'GET', $url, $request, $options );
104
    }
105
106
    /**
107
     * Send a POST request
108
     * @param string         $url     The URL to request
109
     * @param array|string   $request The query string
110
     * @param array          $options Any of the HTTP stream context options, e.g. extra headers.
111
     * @return string
112
     */
113
    public function post( $url, $request = null, $options = [] )
114
    {
115
        return $this->request( 'POST', $url, $request, $options );
116
    }
117
118
    /**
119
     * Send a PUT request
120
     * @param string         $url     The URL to request
121
     * @param array|string   $request The query string
122
     * @param array          $options Any of the HTTP stream context options, e.g. extra headers.
123
     * @return string
124
     */
125
    public function put( $url, $request = null, $options = [] )
126
    {
127
        return $this->request( 'PUT', $url, $request, $options );
128
    }
129
130
    /**
131
     * Send a DELETE request
132
     * @param string         $url     The URL to request
133
     * @param array|string   $request The query string
134
     * @param array          $options Any of the HTTP stream context options, e.g. extra headers.
135
     * @return string
136
     */
137
    public function delete( $url, $request = null, $options = [] )
138
    {
139
        return $this->request( 'DELETE', $url, $request, $options );
140
    }
141
142
143
    /**
144
     * Adds headers for subsequent requests
145
     * @param mixed $headers The headers to add, either as a string or an array of headers.
146
     * @return $this
147
     */
148 1
    public function headers($headers)
149
    {
150 1
        if (!isset($this->options['headers'])) {
151 1
            $this->options['headers'] = [];
152 1
        }
153 1
        if ( !is_array($headers) ) {
154 1
            $headers = explode("\r\n",$headers);
155 1
            if (end($headers) == '') {
156 1
                array_pop($headers);
157 1
            }
158 1
        }
159
160 1
        $this->options['headers'] = array_merge($this->options['headers'], $headers);
161
162 1
        return $this;
163
    }
164
}
165