Skip to content

Commit

Permalink
Changing model events to use observer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
REBELinBLUE committed Mar 5, 2017
1 parent cf4b731 commit 6536878
Show file tree
Hide file tree
Showing 54 changed files with 925 additions and 654 deletions.
6 changes: 1 addition & 5 deletions Makefile
Expand Up @@ -96,10 +96,6 @@ coverage: ##@tests Test Coverage HTML
--html storage/app/tmp/coverage/ --clover storage/app/tmp/coverage.xml
@rm storage/app/tmp/*.cov

phpunit-fast: ##@tests Unit Tests - Excluding slow model tests which touch the database
@echo "${GREEN}Fast unit tests${RESET}"
@php vendor/bin/phpunit --no-coverage --testsuite "Unit Tests" --exclude-group slow

phpunit: ##@tests Unit Tests
@echo "${GREEN}Unit tests${RESET}"
@php vendor/bin/phpunit --no-coverage --testsuite "Unit Tests"
Expand All @@ -109,7 +105,7 @@ integration: ##@tests Integration Tests
@php vendor/bin/phpunit --no-coverage --testsuite "Integration Tests"

quicktest: ##@shortcuts Runs fast tests; these exclude PHPMD, slow unit tests, integration & dusk tests
quicktest: install-dev lint phpcs phpdoc-check phpcpd phpunit-fast
quicktest: install-dev lint phpcs phpdoc-check phpcpd

test: ##@shortcuts Runs most tests; but excludes integration & dusk tests
test: install-dev lint phpcs phpdoc-check phpunit phpcpd phpmd phpstan
Expand Down
15 changes: 0 additions & 15 deletions app/Channel.php
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use REBELinBLUE\Deployer\Notifications\System\NewTestNotification;
use REBELinBLUE\Deployer\Traits\BroadcastChanges;

/**
Expand Down Expand Up @@ -67,20 +66,6 @@ public function project()
return $this->belongsTo(Project::class);
}

/**
* Override the boot method to bind model event listeners.
*/
public static function boot()
{
parent::boot();

// When the notification has been saved queue a test
static::saved(function (Channel $model) {
// FIXME: Change to use an event listener
$model->notify(new NewTestNotification(app('translator')));
});
}

/**
* Returns the email address to send the notification to.
*
Expand Down
37 changes: 3 additions & 34 deletions app/CheckUrl.php
Expand Up @@ -4,18 +4,14 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Bus\DispatchesJobs;
use REBELinBLUE\Deployer\Events\UrlDown;
use REBELinBLUE\Deployer\Events\UrlUp;
use REBELinBLUE\Deployer\Jobs\RequestProjectCheckUrl;
use REBELinBLUE\Deployer\Traits\BroadcastChanges;

/**
* The application's url store for health check.
*/
class CheckUrl extends Model
{
use SoftDeletes, BroadcastChanges, DispatchesJobs;
use SoftDeletes, BroadcastChanges;

const ONLINE = 0;
const UNTESTED = 1;
Expand Down Expand Up @@ -55,21 +51,6 @@ class CheckUrl extends Model
*/
protected $dates = ['last_seen'];

/**
* Override the boot method to bind model event listeners.
*/
public static function boot()
{
parent::boot();

// When saving the model, if the URL has changed we need to test it
static::saved(function (CheckUrl $model) {
if ($model->status === self::UNTESTED) {
$model->dispatch(new RequestProjectCheckUrl(collect([$model])));
}
});
}

/**
* Define a mutator to set the status to untested if the URL changes.
*
Expand All @@ -92,17 +73,9 @@ public function setUrlAttribute($value)
*/
public function online()
{
$isCurrentlyHealthy = ($this->status === self::UNTESTED || $this->isHealthy());

$this->status = self::ONLINE;
$this->missed = 0;
$this->last_seen = $this->freshTimestamp();

if (!$isCurrentlyHealthy) {
event(new UrlUp($this));
}

return $this->save();
}

/**
Expand All @@ -112,12 +85,8 @@ public function online()
*/
public function offline()
{
$this->status = self::OFFLINE;
$this->missed = $this->missed + 1;

event(new UrlDown($this));

return $this->save();
$this->status = self::OFFLINE;
$this->missed = $this->missed + 1;
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Deployment.php
Expand Up @@ -78,6 +78,7 @@ public static function boot()
{
parent::boot();

// FIXME: Change to use the trait
static::saved(function (Deployment $model) {
event(new ModelChanged($model, 'deployment'));
});
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Session\Store;

Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\UrlChanged;
Expand All @@ -10,7 +10,7 @@
/**
* Event handler class for URL notifications.
**/
class SendCheckUrlNotification
class SendCheckUrlNotifications
{
/**
* @var Translator
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\DeploymentFinished;
Expand Down Expand Up @@ -41,6 +41,7 @@ public function handle(DeploymentFinished $event)

$notification = DeploymentFailed::class;
$event = 'deployment_failure';

if ($deployment->isSuccessful()) {
$notification = DeploymentSucceeded::class;
$event = 'deployment_success';
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\EmailChangeRequested;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\HeartbeatChanged;
Expand All @@ -10,7 +10,7 @@
/**
* Event handler class for heartbeat notifications.
**/
class SendHeartbeatNotification
class SendHeartbeatNotifications
{
/**
* @var Translator
Expand All @@ -36,6 +36,7 @@ public function handle(HeartbeatChanged $event)

$notification = HeartbeatRecovered::class;
$event = 'heartbeat_recovered';

if (!$heartbeat->isHealthy()) {
$notification = HeartbeatMissing::class;
$event = 'heartbeat_missing';
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\UserWasCreated;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\DispatchesJobs;
Expand Down
36 changes: 36 additions & 0 deletions app/Events/Observers/ChannelObserver.php
@@ -0,0 +1,36 @@
<?php

namespace REBELinBLUE\Deployer\Events\Observers;

use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Channel;
use REBELinBLUE\Deployer\Notifications\System\NewTestNotification;

/**
* Event observer for Channel model.
*/
class ChannelObserver
{
/**
* @var Translator
*/
private $translator;

/**
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}

/**
* Called when the model is saved.
*
* @param Channel $channel
*/
public function saved(Channel $channel)
{
$channel->notify(new NewTestNotification($this->translator));
}
}
60 changes: 60 additions & 0 deletions app/Events/Observers/CheckUrlObserver.php
@@ -0,0 +1,60 @@
<?php

namespace REBELinBLUE\Deployer\Events\Observers;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Collection;
use REBELinBLUE\Deployer\CheckUrl;
use REBELinBLUE\Deployer\Events\UrlDown as UrlDownEvent;
use REBELinBLUE\Deployer\Events\UrlUp as UrlUpEvent;
use REBELinBLUE\Deployer\Jobs\RequestProjectCheckUrl;

/**
* Event observer for CheckUrl model.
*/
class CheckUrlObserver
{
use DispatchesJobs;

/**
* @var Dispatcher
*/
private $dispatcher;

/**
* @param Dispatcher $dispatcher
*/
public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Called when the model is saved.
*
* @param CheckUrl $url
*/
public function saved(CheckUrl $url)
{
if ($url->status === CheckUrl::UNTESTED) {
$collection = new Collection([$url]);

$this->dispatch(new RequestProjectCheckUrl($collection));
}
}

/**
* Called when the model is updated.
*
* @param CheckUrl $url
*/
public function updated(CheckUrl $url)
{
if ($url->status === CheckUrl::OFFLINE) {
$this->dispatcher->dispatch(new UrlDownEvent($url));
} elseif ($url->status === CheckUrl::ONLINE && $url->getOriginal('status') === CheckUrl::OFFLINE) {
$this->dispatcher->dispatch(new UrlUpEvent($url));
}
}
}
23 changes: 23 additions & 0 deletions app/Events/Observers/HeartbeatObserver.php
@@ -0,0 +1,23 @@
<?php

namespace REBELinBLUE\Deployer\Events\Observers;

use REBELinBLUE\Deployer\Heartbeat;

/**
* Event observer for Heartbeat model.
*/
class HeartbeatObserver
{
/**
* Called when the model is being created.
*
* @param Heartbeat $heartbeat
*/
public function creating(Heartbeat $heartbeat)
{
if (empty($heartbeat->hash)) {
$heartbeat->generateHash();
}
}
}
46 changes: 46 additions & 0 deletions app/Events/Observers/ProjectObserver.php
@@ -0,0 +1,46 @@
<?php

namespace REBELinBLUE\Deployer\Events\Observers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use REBELinBLUE\Deployer\Jobs\GenerateKey;
use REBELinBLUE\Deployer\Jobs\RegeneratePublicKey;
use REBELinBLUE\Deployer\Project;

/**
* Event observer for Project model.
*/
class ProjectObserver
{
use DispatchesJobs;

/**
* Called when the model is being created.
*
* @param Project $project
*/
public function creating(Project $project)
{
if (empty($project->private_key)) {
$this->dispatch(new GenerateKey($project));
} elseif (empty($project->public_key)) {
$this->dispatch(new RegeneratePublicKey($project));
}

if (empty($project->hash)) {
$project->generateHash();
}
}

/**
* Called when the model is being updated.
*
* @param Project $project
*/
public function updating(Project $project)
{
if (empty($project->public_key)) {
$this->dispatch(new RegeneratePublicKey($project));
}
}
}

0 comments on commit 6536878

Please sign in to comment.