1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Checker; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\EachPromise; |
6
|
|
|
use GuzzleHttp\Promise\Promise; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use Spatie\UptimeMonitor\Helpers\ConsoleOutput; |
9
|
|
|
use Spatie\UptimeMonitor\MonitorCollection; |
10
|
|
|
|
11
|
|
|
class SMTPChecker extends Checker |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function check(MonitorCollection $monitors) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$monitors->resetItemKeys(); |
19
|
|
|
$this->monitors = $monitors; |
20
|
|
|
(new EachPromise($this->getPromises($monitors), [ |
21
|
|
|
'concurrency' => config('laravel-uptime-monitor.uptime_check.concurrent_checks'), |
22
|
|
|
'fulfilled' => function ($monitor, $index) { |
23
|
|
|
$monitor = $this->monitors->getMonitorAtIndex($index); |
24
|
|
|
ConsoleOutput::info("Could reach {$monitor->url}"); |
25
|
|
|
$monitor->uptimeRequestSucceeded(new Response(200, [], "Could reach {$monitor->url}")); |
26
|
|
|
}, |
27
|
|
|
'rejected' => function ($exception, $index) { |
28
|
|
|
$monitor = $this->monitors->getMonitorAtIndex($index); |
29
|
|
|
ConsoleOutput::error("Could not reach {$monitor->url} error: `{$exception->getMessage()}`"); |
30
|
|
|
$monitor->uptimeRequestFailed($exception->getMessage()); |
31
|
|
|
}, |
32
|
|
|
]))->promise()->wait(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getPromises($monitors): \Generator |
36
|
|
|
{ |
37
|
|
|
foreach ($monitors as $monitor) { |
38
|
|
|
$promise = with(new Promise())->then(null, function () use (&$promise, $monitor) { |
39
|
|
|
$urlSegments = explode('://', $monitor->url); |
40
|
|
|
$protocol = $urlSegments[0]; |
|
|
|
|
41
|
|
|
$hostSegments = explode(':', $urlSegments[1]); |
42
|
|
|
$host = $hostSegments[0]; |
43
|
|
|
$port = (array_key_exists(1, $hostSegments)) ? $hostSegments[1] : 25; |
44
|
|
|
ConsoleOutput::info("Checking {$monitor->url}"); |
45
|
|
|
try { |
46
|
|
|
$smtpTransport = \Swift_SmtpTransport::newInstance($host, $port); |
47
|
|
|
$smtpTransport->setTimeout(config('laravel-uptime-monitor.uptime_check.timeout_per_connection')); |
48
|
|
|
$smtpTransport->start(); |
49
|
|
|
} catch (\Swift_TransportException $e) { |
50
|
|
|
throw new \Exception($e->getMessage()); |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
yield $promise; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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.