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

View File

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

View File

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