Commands now use provider DI

This commit is contained in:
Henry Whitaker
2021-03-07 13:45:29 +00:00
parent 85d2d87e53
commit 2861196cff
3 changed files with 22 additions and 21 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Helpers\SpeedtestHelper; use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use Illuminate\Console\Command; use Illuminate\Console\Command;
class SpeedtestCommand extends Command class SpeedtestCommand extends Command
@@ -21,13 +22,17 @@ class SpeedtestCommand extends Command
*/ */
protected $description = 'Performs a new speedtest'; protected $description = 'Performs a new speedtest';
private SpeedtestProvider $speedtestProvider;
/** /**
* Create a new command instance. * Create a new command instance.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct(SpeedtestProvider $speedtestProvider)
{ {
$this->speedtestProvider = $speedtestProvider;
parent::__construct(); parent::__construct();
} }
@@ -42,12 +47,12 @@ class SpeedtestCommand extends Command
$results = SpeedtestHelper::runSpeedtest(false, false); $results = SpeedtestHelper::runSpeedtest(false, false);
if(!is_object($results)) { if (!is_object($results)) {
$this->error('Something went wrong running the speedtest.'); $this->error('Something went wrong running the speedtest.');
exit(); exit();
} }
if(property_exists($results, 'ping') && property_exists($results, 'download') && property_exists($results, 'upload')) { if (property_exists($results, 'ping') && property_exists($results, 'download') && property_exists($results, 'upload')) {
$this->error('Something went wrong running the speedtest.'); $this->error('Something went wrong running the speedtest.');
exit(); exit();
} }

View File

@@ -3,6 +3,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Helpers\SpeedtestHelper; use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
@@ -22,13 +23,17 @@ class SpeedtestLatestCommand extends Command
*/ */
protected $description = 'Returns the latest speedtest result'; protected $description = 'Returns the latest speedtest result';
private SpeedtestProvider $speedtestProvider;
/** /**
* Create a new command instance. * Create a new command instance.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct(SpeedtestProvider $speedtestProvider)
{ {
$this->speedtestProvider = $speedtestProvider;
parent::__construct(); parent::__construct();
} }
@@ -41,8 +46,8 @@ class SpeedtestLatestCommand extends Command
{ {
$latest = SpeedtestHelper::latest(); $latest = SpeedtestHelper::latest();
if($latest) { if ($latest) {
if($latest->scheduled) { if ($latest->scheduled) {
$extra = '(scheduled)'; $extra = '(scheduled)';
} else { } else {
$extra = '(manual)'; $extra = '(manual)';
@@ -50,7 +55,7 @@ class SpeedtestLatestCommand extends Command
$this->info('Last speedtest run at: ' . $latest->created_at . ' ' . $extra); $this->info('Last speedtest run at: ' . $latest->created_at . ' ' . $extra);
if($latest->failed) { if ($latest->failed) {
$this->error('Speedtest failed'); $this->error('Speedtest failed');
} else { } else {
$this->info('Ping: ' . $latest->ping . ' ms'); $this->info('Ping: ' . $latest->ping . ' ms');

View File

@@ -18,20 +18,11 @@ class SpeedtestServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
if (File::exists(env('DB_DATABASE'))) { $this->app->singleton(
if (Schema::hasTable('settings')) { SpeedtestProvider::class,
switch (SettingsHelper::get('speedtest_provider')) { function () {
case 'ookla': return new OoklaTester();
default:
$this->app->singleton(
SpeedtestProvider::class,
function () {
return new OoklaTester();
}
);
break;
}
} }
} );
} }
} }