Skip to content

Commit a93d186

Browse files
committed
wip
1 parent cb24480 commit a93d186

File tree

10 files changed

+97
-12
lines changed

10 files changed

+97
-12
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"autoload": {
3333
"psr-4": {
3434
"Spatie\\UptimeMonitor\\": "src"
35-
}
35+
},
36+
"files": [
37+
"src/Helpers/functions.php"
38+
]
3639
},
3740
"autoload-dev": {
3841
"psr-4": {

src/Commands/BaseCommand.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Spatie\UptimeMonitor\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Spatie\UptimeMonitor\Helpers\ConsoleOutput;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
abstract class BaseCommand extends Command
11+
{
12+
/**
13+
* @param \Symfony\Component\Console\Input\InputInterface $input
14+
* @param \Symfony\Component\Console\Output\OutputInterface $output
15+
*
16+
* @return int
17+
*/
18+
public function run(InputInterface $input, OutputInterface $output)
19+
{
20+
app(ConsoleOutput::class)->setOutput($this);
21+
22+
return parent::run($input, $output);
23+
}
24+
}

src/Commands/CheckUptimeMonitors.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Spatie\UptimeMonitor\Commands;
44

5-
use Illuminate\Console\Command;
65
use Spatie\UptimeMonitor\Models\UptimeMonitor;
76
use Spatie\UptimeMonitor\Services\PingMonitors\UptimeMonitorCollection;
87

9-
class CheckUptimeMonitors extends Command
8+
class CheckUptimeMonitors extends BaseCommand
109
{
1110
/**
1211
* The name and signature of the console command.

src/Events/SiteUp.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\UptimeMonitor\Events;
44

55
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Spatie\UptimeMonitor\Models\UptimeMonitor;
67

78
class SiteUp implements ShouldQueue
89
{

src/Helpers/ConsoleOutput.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Spatie\UptimeMonitor\Helpers;
4+
5+
class ConsoleOutput
6+
{
7+
/** @var \Illuminate\Console\OutputStyle */
8+
protected $output;
9+
10+
/**
11+
* @param $output
12+
*/
13+
public function setOutput($output)
14+
{
15+
$this->output = $output;
16+
}
17+
18+
public function __call(string $method, array $arguments)
19+
{
20+
$consoleOutput = app(static::class);
21+
22+
if (! $consoleOutput->output) {
23+
return;
24+
}
25+
26+
$consoleOutput->output->$method($arguments[0]);
27+
}
28+
}

src/Helpers/Format.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\UptimeMonitor\Helpers;
4+
5+
class Format
6+
{
7+
public static function emoji(bool $bool): string
8+
{
9+
if ($bool) {
10+
return "\u{2705}";
11+
}
12+
13+
return "\u{274C}";
14+
}
15+
}

src/Helpers/functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Spatie\UptimeMonitor\Helpers\ConsoleOutput;
4+
5+
6+
7+
function consoleOutput(): ConsoleOutput
8+
{
9+
return app(ConsoleOutput::class);
10+
}

src/Notifications/EventHandler.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ public function __construct(Repository $config)
2222
public function subscribe(Dispatcher $events)
2323
{
2424
$events->listen($this->allUptimeMonitorEventClasses(), function ($event) {
25-
$notifiable = $this->determineNotifiable();
2625

2726
$notification = $this->determineNotification($event);
2827

28+
if (! $notification) {
29+
return;
30+
}
31+
2932
if ($notification->isStillRelevant()) {
33+
$notifiable = $this->determineNotifiable();
34+
3035
$notifiable->notify($notification);
3136
}
3237

@@ -40,7 +45,7 @@ protected function determineNotifiable()
4045
return app($notifiableClass);
4146
}
4247

43-
protected function determineNotification($event): Notification
48+
protected function determineNotification($event)
4449
{
4550
$eventName = class_basename($event);
4651

@@ -55,11 +60,9 @@ protected function determineNotification($event): Notification
5560
return $notificationName === $eventName;
5661
});
5762

58-
if (!$notificationClass) {
59-
throw NotificationCouldNotBeSent::noNotifcationClassForEvent($event);
63+
if ($notificationClass) {
64+
return app($notificationClass)->setEvent($event);
6065
}
61-
62-
return app($notificationClass)->setEvent($event);
6366
}
6467

6568
protected function allUptimeMonitorEventClasses(): array

src/UptimeMonitorCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Spatie\UptimeMonitor\Services\PingMonitors;
44

5-
use Spatie\UptimeMonitor\Models\PingMonitor;
65
use Cache;
76
use Generator;
87
use GuzzleHttp\Client;
@@ -11,14 +10,15 @@
1110
use Illuminate\Support\Collection;
1211
use Log;
1312
use Psr\Http\Message\ResponseInterface;
13+
use Spatie\UptimeMonitor\Models\UptimeMonitor;
1414

1515
class UptimeMonitorCollection extends Collection
1616
{
1717
public function check()
1818
{
1919
$this->resetItemKeys();
2020

21-
Log::info("Start checking for {$this->count()} monitors...");
21+
consoleOutput()->info("Start checking for {$this->count()} monitors...");
2222

2323
(new EachPromise($this->getPromises(), [
2424
'concurrency' => 100,
@@ -71,7 +71,7 @@ protected function resetItemKeys()
7171
$this->items = $this->values()->all();
7272
}
7373

74-
public function log($message, PingMonitor $pingMonitor)
74+
public function log($message, UptimeMonitor $pingMonitor)
7575
{
7676
Log::info("$message (url: {$pingMonitor->url} id: {$pingMonitor->id})");
7777
}

src/UptimeMonitorServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ public function register()
4545
'command.uptime-monitor:delete',
4646
'command.uptime-monitor:list',
4747
]);
48+
49+
$this->app->singleton(ConsoleOutput::class);
4850
}
4951
}

0 commit comments

Comments
 (0)