Completed
Push — master ( 8fef9b...d67ae0 )
by Nils
04:01
created

Retriever::next()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 42
rs 6.7272
cc 7
eloc 25
nc 5
nop 0
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\ListRetriever;
4
5
use Ivory\HttpAdapter\HttpAdapterInterface;
6
use Ivory\HttpAdapter\Message\Request;
7
use Ivory\HttpAdapter\MultiHttpAdapterException;
8
use Psr\Http\Message\UriInterface;
9
use whm\Html\Uri;
10
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever as SmokeRetriever;
11
12
class Retriever implements SmokeRetriever
13
{
14
    private $urls;
15
    private $httpClient;
16
    private $urlStack;
17
18
    private $redirects = array();
19
20
    public function init(array $urls)
21
    {
22
        foreach ($urls as $key => $urlList) {
23
            foreach ($urlList as $url) {
24
                $this->urls[$url] = ['url' => $url, 'system' => $key];
25
            }
26
        }
27
28
        $this->urlStack = $this->urls;
29
    }
30
31
    public function next()
32
    {
33
        if (empty($this->urlStack)) {
34
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface whm\Smoke\Extensions\Smo...triever\Retriever::next of type whm\Smoke\Http\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...
35
        }
36
37
        $url = array_pop($this->urlStack);
38
39
        $request = new Request(new Uri($url['url']), 'GET', 'php://memory', ['Accept-Encoding' => 'gzip']);
40
41
        try {
42
            $responses = $this->httpClient->sendRequests(array($request));
43
        } catch (MultiHttpAdapterException $e) {
44
            $exceptions = $e->getExceptions();
45
            $errorMessages = "";
46
            foreach ($exceptions as $exception) {
47
                // @fixme this must be part of the http client
48
                $message = $exception->getMessage();
49
                if (strpos($message, "An error occurred when fetching the URI") === 0) {
50
                    $corruptUrl = substr($message, "41", strpos($message, '"', 41) - 41);
51
                    if (strpos($corruptUrl, '/') === 0) {
52
                        /** @var \Ivory\HttpAdapter\HttpAdapterException $exception */
53
54
                        $mainUri = $request->getUri();
55
                        $this->redirects[(string)$mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl] = (string)$mainUri;
56
57
                        $this->urls[] = ['url' => $mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl, 'system' => $url['system']];
58
                        $this->urlStack[] = ['url' => $mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl, 'system' => $url['system']];
59
60
                        return $this->next();
61
                    }
62
                } else {
63
                    $errorMessages .= $exception->getMessage() . "\n";
64
                }
65
            }
66
            if ($errorMessages != "") {
67
                throw new \RuntimeException($errorMessages);
68
            }
69
        }
70
71
        return $responses[0];
72
    }
73
74
    public function getComingFrom(UriInterface $uri)
75
    {
76
        return $uri;
77
    }
78
79
    public function getSystem(UriInterface $uri)
80
    {
81
        if (array_key_exists((string)$uri, $this->redirects)) {
82
            return $this->urls[$this->redirects[(string)$uri]]['system'];
83
        }
84
        return $this->urls[(string)$uri]['system'];
85
    }
86
87
    public function getSystems()
88
    {
89
        $systems = [];
90
        foreach ($this->urls as $key => $url) {
91
            $systems[] = $url['system'];
92
        }
93
94
        return $systems;
95
    }
96
97
    public function setHttpClient(HttpAdapterInterface $httpClient)
98
    {
99
        $this->httpClient = $httpClient;
100
    }
101
}
102