Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmurray committed Oct 22, 2018
2 parents 6bad3ae + 30e0266 commit 6a8c877
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 232 deletions.
22 changes: 3 additions & 19 deletions app/Commands/AddRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace App\Commands;

use App\Helpers\DigitalOceanHelper;
use App\Helpers\SettingsHelper;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;

Expand All @@ -24,28 +22,14 @@ class AddRecord extends Command
*/
protected $description = 'Set which records to update.';

protected $settings;
protected $token;

private $digitalocean;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
public function handle(DigitalOceanHelper $digitalocean)
{
$this->settings = new SettingsHelper();

if ($this->settings->error !== null) {
$this->error($this->settings->error);
return;
}

$this->digitalocean = new DigitalOceanHelper($this->settings->getToken());

$domains = $this->digitalocean->getDomains();
$domains = $digitalocean->getDomains();

$selected_domain = $this->menu("Which domain?", $domains)->open();

Expand All @@ -54,7 +38,7 @@ public function handle()
return;
}

$records = $this->digitalocean->getDomainRecords($selected_domain);
$records = $digitalocean->getDomainRecords($selected_domain);

$records_for_menu = $records->mapWithKeys(function ($values, $key) {
return [$key => "{$values->name} ({$values->type}): {$values->data}"];
Expand Down
1 change: 0 additions & 1 deletion app/Commands/ListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Commands;

use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;

Expand Down
1 change: 0 additions & 1 deletion app/Commands/RemoveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Commands;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;

Expand Down
14 changes: 4 additions & 10 deletions app/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace App\Commands;

use App\Helpers\SettingsHelper;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;
use Storage;

class Setup extends Command
{
Expand All @@ -25,27 +23,23 @@ class Setup extends Command
*/
protected $description = 'Well... It sets things up.';

private $settings;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
public function handle(SettingsHelper $settings)
{
if ($this->confirm("This will destroy any existing doddns configuration. Is that ok?")) {
$this->createDatabase();
}

$this->settings = new SettingsHelper();

$token = $this->ask("What is your Digital Ocean peronal access token?");

if ($this->settings->error !== null) {
$this->insertToken($token);
} else {
if ($settings->hasToken()) {
$this->updateToken($token);
} else {
$this->insertToken($token);
}

$this->info("All done! We're good to go!");
Expand Down
21 changes: 3 additions & 18 deletions app/Commands/UpdateRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Commands;

use App\Helpers\DigitalOceanHelper;
use App\Helpers\SettingsHelper;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
Expand All @@ -26,32 +25,18 @@ class UpdateRecords extends Command
*/
protected $description = 'Update saved records to current IP address.';

protected $settings;

private $digitalocean;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
public function handle(DigitalOceanHelper $digitalocean)
{
$this->settings = new SettingsHelper();

if ($this->settings->error !== null) {
$this->error($this->settings->error);
return;
}

$this->digitalocean = new DigitalOceanHelper($this->settings->getToken());

$current_ip = Ip::get();
$records_to_update = DB::table('records')->get();

$records_to_update->each(function ($record) use ($current_ip) {
$this->digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);
;
$records_to_update->each(function ($record) use ($current_ip, $digitalocean) {
$digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);

DB::update('update records set record_updated_at = ? where id = ?', [Carbon::now()->toDatetimeString(), $record->id]);

Expand Down
7 changes: 2 additions & 5 deletions app/Commands/UpdateToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Commands;

use App\Helpers\SettingsHelper;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;

Expand All @@ -30,11 +29,9 @@ class UpdateToken extends Command
*
* @return mixed
*/
public function handle()
public function handle(SettingsHelper $settings)
{
$this->settings = new SettingsHelper();

if ($this->settings->error !== null) {
if ($settings->hasToken()) {
$this->updateToken();
} else {
$this->insertToken();
Expand Down
11 changes: 11 additions & 0 deletions app/Exceptions/InvalidSettingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace App\Exceptions;

use Symfony\Component\Console\Exception\ExceptionInterface;

class InvalidSettingException extends \Exception implements ExceptionInterface
{

}
74 changes: 55 additions & 19 deletions app/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Helpers;

use App\Exceptions\InvalidSettingException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
Expand All @@ -10,37 +12,71 @@
class SettingsHelper
{

/**
* @var Collection
*/
private $settings;
private $token;

public $error = null;

public function __construct()
public function getSettings()
{
return $this->settings;
}

if (!is_file(config('database.connections.sqlite.database'))) {
$this->error = "You have to run the setup command in order to create the local database.";
return $this;
}
public function hasToken()
{
return $this->hasSetting('token');
}

$this->settings = DB::table('settings')->get();
public function getToken()
{
return $this->getSetting('token');
}

if ($this->settings->isNotEmpty()) {
$this->token = $this->settings->first()->token;
} else {
$this->error = "There is no settings to the database.";
}
/**
* @param $key
* @return mixed
*/
private function getSetting($key)
{
$this->setupSettings();

return $this;
return $this->settings->{$key};
}

public function getSettings()
/**
* Check to see if the settings exists in the database
*
* @param $key
* @return boolean
*/
private function hasSetting($key)
{
return $this->settings;
try {
$this->setupSettings();

return is_object($this->settings) &&
property_exists($this->settings, $key);
} catch (InvalidSettingException $exception) {
return false;
}
}

public function getToken()
/**
* @throws InvalidSettingException
*/
private function setupSettings()
{
return $this->token;
// We bail if the settings are already loaded
if($this->settings) {
return;
}

if (!is_file(config('database.connections.sqlite.database'))) {
throw new InvalidSettingException(
"The local database does not exist. Please run the setup command."
);
}

$this->settings = DB::table('settings')->first();
}
}
13 changes: 12 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Helpers\DigitalOceanHelper;
use App\Helpers\SettingsHelper;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -23,6 +25,15 @@ public function boot()
*/
public function register()
{
//
$this->app->singleton(SettingsHelper::class, function() {
return new SettingsHelper();
});

$this->app->singleton(DigitalOceanHelper::class, function() {
/** @var SettingsHelper $settings */
$settings = app(SettingsHelper::class);

return new DigitalOceanHelper($settings->getToken());
});
}
}
Binary file modified builds/doddns
100755 → 100644
Binary file not shown.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

# X (Next release)
- Nothing yet.

# 1.3.0
- Now with more dependency injection thanks to @victorlap !
- Added ASCII logo from latest version of Laravel Zero.

# 1.2.1
Updated readme to add notes on updating.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"require": {
"php": "^7.1.3",
"benjamin-smith/php-ipify": "^1.0",
"toin0u/digitalocean-v2": "~2.0",
"guzzlehttp/guzzle": "^6.3",
"illuminate/database": "5.7.*",
"laravel-zero/framework": "5.7.*"
"laravel-zero/framework": "5.7.*",
"toin0u/digitalocean-v2": "~2.0",
"zendframework/zend-text": "^2.7"
},
"require-dev": {
"mockery/mockery": "^1.0",
Expand Down
Loading

0 comments on commit 6a8c877

Please sign in to comment.