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; |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.