|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the hogosha-monitor package |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Feel free to edit as you please, and have fun. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Runner; |
|
17
|
|
|
|
|
18
|
|
|
use GuzzleHttp\Client; |
|
19
|
|
|
use GuzzleHttp\Pool; |
|
20
|
|
|
use GuzzleHttp\Promise; |
|
21
|
|
|
use GuzzleHttp\Psr7\Request; |
|
22
|
|
|
use GuzzleHttp\TransferStats; |
|
23
|
|
|
use Hogosha\Monitor\Client\GuzzleClient; |
|
24
|
|
|
use Hogosha\Monitor\Model\Result; |
|
25
|
|
|
use Hogosha\Monitor\Model\ResultCollection; |
|
26
|
|
|
use Hogosha\Monitor\Model\UrlProvider; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Runner |
|
32
|
|
|
{ |
|
33
|
|
|
protected $urlProvider; |
|
34
|
|
|
|
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param UrlProvider $urlProvider |
|
41
|
|
|
* @param GuzzleClient $client |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(UrlProvider $urlProvider, Client $client) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->urlProvider = $urlProvider; |
|
46
|
|
|
$this->client = $client; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* run. |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function run() |
|
55
|
|
|
{ |
|
56
|
|
|
$urls = $this->urlProvider->getUrls(); |
|
57
|
|
|
$client = $this->client; |
|
58
|
|
|
|
|
59
|
|
|
//This is a bit messie, need a refacto |
|
60
|
|
|
$resultCollection = new ResultCollection(); |
|
61
|
|
|
|
|
62
|
|
|
$requests = function () use ($urls, $client, $resultCollection) { |
|
63
|
|
|
foreach ($urls as $url) { |
|
64
|
|
|
yield function () use ($client, $url, $resultCollection) { |
|
65
|
|
|
return $client->sendAsync( |
|
66
|
|
|
new Request( |
|
67
|
|
|
$url->getMethod(), |
|
68
|
|
|
$url->getUrl(), |
|
69
|
|
|
$url->getHeaders() |
|
70
|
|
|
), |
|
71
|
|
|
[ |
|
72
|
|
|
'timeout' => $url->getTimeout(), |
|
73
|
|
|
'connect_timeout' => $url->getTimeout(), |
|
74
|
|
|
'on_stats' => function (TransferStats $tranferStats) use ($url, $resultCollection) { |
|
75
|
|
|
|
|
76
|
|
|
$handlerError = null; |
|
77
|
|
|
$validatorError = null; |
|
78
|
|
|
|
|
79
|
|
|
if ($tranferStats->hasResponse()) { |
|
80
|
|
|
$validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody()); |
|
81
|
|
|
|
|
82
|
|
|
if (false === $validatorResult) { |
|
83
|
|
|
$validatorError = $url->getValidator()->getError(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$statusCode = $tranferStats->getResponse()->getStatusCode(); |
|
87
|
|
|
$transferTime = $tranferStats->getTransferTime(); |
|
88
|
|
|
} else { |
|
89
|
|
|
// If we have a connection error |
|
90
|
|
|
$statusCode = 400; |
|
91
|
|
|
$transferTime = 0; |
|
92
|
|
|
$handlerError = curl_strerror($tranferStats->getHandlerErrorData()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$resultCollection->append( |
|
96
|
|
|
(new Result( |
|
97
|
|
|
$url, |
|
98
|
|
|
$statusCode, |
|
99
|
|
|
$transferTime, |
|
100
|
|
|
$handlerError, |
|
101
|
|
|
$validatorResult, |
|
|
|
|
|
|
102
|
|
|
$validatorError |
|
103
|
|
|
)) |
|
104
|
|
|
); |
|
105
|
|
|
}, |
|
106
|
|
|
] |
|
107
|
|
|
); |
|
108
|
|
|
}; |
|
109
|
|
|
} |
|
110
|
|
|
}; |
|
111
|
|
|
|
|
112
|
|
|
$pool = new Pool($this->client, $requests(), [ |
|
113
|
|
|
'concurrency' => 5, |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$promise = $pool->promise(); |
|
117
|
|
|
|
|
118
|
|
|
$promise->wait(); |
|
119
|
|
|
|
|
120
|
|
|
return $resultCollection; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: