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\Exception\ConnectException; |
20
|
|
|
use GuzzleHttp\Pool; |
21
|
|
|
use GuzzleHttp\Promise; |
22
|
|
|
use GuzzleHttp\Psr7\Request; |
23
|
|
|
use GuzzleHttp\TransferStats; |
24
|
|
|
use Hogosha\Monitor\Client\GuzzleClient; |
25
|
|
|
use Hogosha\Monitor\Model\Result; |
26
|
|
|
use Hogosha\Monitor\Model\ResultCollection; |
27
|
|
|
use Hogosha\Monitor\Model\UrlProvider; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Guillaume Cavana <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Runner |
33
|
|
|
{ |
34
|
|
|
protected $urlProvider; |
35
|
|
|
|
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param UrlProvider $urlProvider |
42
|
|
|
* @param GuzzleClient $client |
43
|
|
|
*/ |
44
|
|
|
public function __construct(UrlProvider $urlProvider, Client $client) |
45
|
|
|
{ |
46
|
|
|
$this->urlProvider = $urlProvider; |
47
|
|
|
$this->client = $client; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* run. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function run() |
56
|
|
|
{ |
57
|
|
|
$stats = []; |
|
|
|
|
58
|
|
|
|
59
|
|
|
$urls = $this->urlProvider->getUrls(); |
60
|
|
|
$client = $this->client; |
61
|
|
|
|
62
|
|
|
$resultCollection = new ResultCollection(); |
63
|
|
|
|
64
|
|
|
$requests = function () use ($urls, $client, $resultCollection) { |
65
|
|
|
foreach ($urls as $url) { |
66
|
|
|
yield function () use ($client, $url, $resultCollection) { |
67
|
|
|
return $client->sendAsync( |
68
|
|
|
new Request( |
69
|
|
|
$url->getMethod(), |
70
|
|
|
$url->getUrl(), |
71
|
|
|
$url->getHeaders() |
72
|
|
|
), |
73
|
|
|
[ |
74
|
|
|
'timeout' => $url->getTimeout(), |
75
|
|
|
'on_stats' => function (TransferStats $tranferStats) use ($url, $resultCollection) { |
76
|
|
|
|
77
|
|
|
if ($tranferStats->hasResponse()) { |
78
|
|
|
$statusCode = $tranferStats->getResponse()->getStatusCode(); |
79
|
|
|
$transferTime = $tranferStats->getTransferTime(); |
80
|
|
|
} else { |
81
|
|
|
// If we have a connection error |
82
|
|
|
$statusCode = 400; |
83
|
|
|
$transferTime = 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$resultCollection->append( |
87
|
|
|
(new Result( |
88
|
|
|
$url, |
89
|
|
|
$statusCode, |
90
|
|
|
$transferTime |
91
|
|
|
)) |
92
|
|
|
); |
93
|
|
|
}, |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
$pool = new Pool($this->client, $requests(), [ |
101
|
|
|
'concurrency' => 5, |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$promise = $pool->promise(); |
105
|
|
|
|
106
|
|
|
$promise->wait(); |
107
|
|
|
|
108
|
|
|
return $resultCollection; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.