Completed
Branch master (1f9106)
by Nils
02:44
created

Retriever::next()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 47
rs 6.7272
cc 7
eloc 27
nc 6
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($urls)
21
    {
22
        if (is_array($urls)) {
23
            foreach ($urls as $key => $urlList) {
24
                foreach ($urlList as $url) {
25
                    $this->urls[$url] = ['url' => $url, 'system' => $key];
26
                }
27
            }
28
            $this->urlStack = $this->urls;
29
        }
30
    }
31
32
    public function next()
33
    {
34
        if (empty($this->urlStack)) {
35
            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...
36
        }
37
38
        $url = array_pop($this->urlStack);
39
40
        $request = new Request(new Uri($url['url']), 'GET', 'php://memory', ['Accept-Encoding' => 'gzip']);
41
42
        try {
43
            $responses = $this->httpClient->sendRequests(array($request));
44
        } catch (MultiHttpAdapterException $e) {
45
            $exceptions = $e->getExceptions();
46
            $errorMessages = '';
47
            foreach ($exceptions as $exception) {
48
                // @fixme this must be part of the http client
49
                $message = $exception->getMessage();
50
                if (strpos($message, 'An error occurred when fetching the URI') === 0) {
51
                    $corruptUrl = substr($message, '41', strpos($message, '"', 41) - 41);
52
                    if (strpos($corruptUrl, '/') === 0) {
53
                        /* @var \Ivory\HttpAdapter\HttpAdapterException $exception */
54
55
                        $mainUri = $request->getUri();
56
                        $this->redirects[(string) $mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl] = (string) $mainUri;
57
58
                        $this->urls[] = ['url' => $mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl, 'system' => $url['system']];
59
                        $this->urlStack[] = ['url' => $mainUri->getScheme() . '://' . $mainUri->getHost() . $corruptUrl, 'system' => $url['system']];
60
61
                        return $this->next();
62
                    }
63
64
                    // the error handling should be done withing the calling class
65
                    echo "\n   " . $exception->getMessage() . "\n";
66
67
                    return $this->next();
68
                } else {
69
                    $errorMessages .= $exception->getMessage() . "\n";
70
                }
71
            }
72
            if ($errorMessages !== '') {
73
                throw new \RuntimeException($errorMessages);
74
            }
75
        }
76
77
        return $responses[0];
78
    }
79
80 View Code Duplication
    public function getOriginUri(UriInterface $uri)
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...
81
    {
82
        if (array_key_exists((string) $uri, $this->redirects)) {
83
            return $this->urls[$this->redirects[(string) $uri]]['url'];
84
        }
85
86
        return $uri;
87
    }
88
89
    public function getComingFrom(UriInterface $uri)
90
    {
91
        return $uri;
92
    }
93
94 View Code Duplication
    public function getSystem(UriInterface $uri)
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...
95
    {
96
        if (array_key_exists((string) $uri, $this->redirects)) {
97
            return $this->urls[$this->redirects[(string) $uri]]['system'];
98
        }
99
100
        return $this->urls[(string) $uri]['system'];
101
    }
102
103
    public function getSystems()
104
    {
105
        $systems = [];
106
        foreach ($this->urls as $key => $url) {
107
            $systems[] = $url['system'];
108
        }
109
110
        return $systems;
111
    }
112
113
    public function setHttpClient(HttpAdapterInterface $httpClient)
114
    {
115
        $this->httpClient = $httpClient;
116
    }
117
}
118