Failed Conditions
Pull Request — dev (#132)
by Steve
02:37
created

Request::__call()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 6
eloc 9
nc 12
nop 2
1
<?php
2
/**
3
 * Mage Scan
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageScan
8
 * @package   MageScan
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magescan
13
 */
14
15
namespace MageScan;
16
17
use GuzzleHttp\Client;
18
use GuzzleHttp\Promise;
19
20
/**
21
 * Make a cURL request to a url
22
 *
23
 * @method \GuzzleHttp\Psr7\Response get(array $path = null, array $params = [])
24
 * @method \GuzzleHttp\Psr7\Response getMany(array $paths, array $params = [])
25
 * @method \GuzzleHttp\Psr7\Response head(array $path = null, array $params = [])
26
 * @method \GuzzleHttp\Psr7\Response headMany(array $paths, array $params = [])
27
 * @method \GuzzleHttp\Psr7\Response post(array $path = null, array $params = [])
28
 * @method \GuzzleHttp\Psr7\Response postMany(array $paths, array $params = [])
29
 *
30
 * @category  MageScan
31
 * @package   MageScan
32
 * @author    Steve Robbins <[email protected]>
33
 * @copyright 2015 Steve Robbins
34
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
35
 * @link      https://github.com/steverobbins/magescan
36
 */
37
class Request
38
{
39
    const REQUEST_TIMEOUT = 15.0;
40
41
    /**
42
     * If true, SSL does not have to be verified
43
     *
44
     * @var boolean
45
     */
46
    protected $insecure = false;
47
48
    /**
49
     * The base URL of the Magento application
50
     *
51
     * @var string|boolean
52
     */
53
    protected $url;
54
55
    /**
56
     * Client cache
57
     *
58
     * @var Client
59
     */
60
    protected $client;
61
62
    /**
63
     * Initialize request object
64
     *
65
     * @param string  $baseUri
66
     * @param boolean $verify
67
     */
68
    public function __construct($baseUri = false, $verify = true)
69
    {
70
        $this->url = $baseUri;
71
        $params = [
72
            'verify'   => $verify,
73
            'http_errors' => false
74
        ];
75
        if ($this->url !== false) {
76
            $params['base_uri'] = $this->url;
77
        }
78
        $this->client = new Client($params);
79
    }
80
81
    /**
82
     * Pass undefined requests to client
83
     *
84
     * @param string $method
85
     * @param arary  $args
86
     *
87
     * @return \GuzzleHttp\Psr7\Response
88
     */
89
    public function __call($method, $args)
90
    {
91
        $paths  = isset($args[0]) ? $args[0] : false;
92
        $params = isset($args[1]) ? $args[1] : [];
93
        if (substr($method, -4) === 'Many') {
94
            $promises = [];
95
            foreach ($paths as $path) {
96
                $promises[$path] = $this->client->requestAsync(substr($method, 0, -4), '/' . $path, $params);
97
            }
98
            return Promise\unwrap($promises);
1 ignored issue
show
Bug Best Practice introduced by
The return type of return \GuzzleHttp\Promise\unwrap($promises); (array) is incompatible with the return type documented by MageScan\Request::__call of type GuzzleHttp\Psr7\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
        }
100
        return $this->client->request($method, $paths ? '/' . $paths : null, $params);
101
    }
102
103
    /**
104
     * Get the base url of this request
105
     *
106
     * @return string
107
     */
108
    public function getUrl()
109
    {
110
        return $this->url;
111
    }
112
113
    /**
114
     * Manipulate header data into a parsable format
115
     *
116
     * @param string $rawData
117
     *
118
     * @return array
119
     */
120
    public function parseHeader($rawData)
121
    {
122
        $data = [];
123
        foreach (explode("\n", trim($rawData)) as $line) {
124
            $bits = explode(': ', $line);
125
            if (count($bits) > 1) {
126
                $key = $bits[0];
127
                unset($bits[0]);
128
                $data[$key] = trim(implode(': ', $bits));
129
            }
130
        }
131
        return $data;
132
    }
133
134
    /**
135
     * Parse out the count from the response
136
     *
137
     * @param string  $response
138
     * @param string  $pattern
139
     * @param boolean $returnAll
140
     *
141
     * @return string|array|boolean
142
     */
143
    public function findMatchInResponse($response, $pattern, $returnAll = false)
144
    {
145
        if (empty($response)) {
146
            return false;
147
        }
148
        if (preg_match($pattern, $response, $match)
149
            && (isset($match[1]) || $returnAll)
150
        ) {
151
            return $returnAll ? $match : $match[1];
152
        }
153
        return false;
154
    }
155
}
156