Permalink
Browse files

Changing model events to use observer classes

  • Loading branch information...
REBELinBLUE committed Mar 5, 2017
1 parent cf4b731 commit 6536878562a398546198f8f94b0d2babccd2b9f2
Showing with 925 additions and 654 deletions.
  1. +1 −5 Makefile
  2. +0 −15 app/Channel.php
  3. +3 −34 app/CheckUrl.php
  4. +1 −0 app/Deployment.php
  5. +1 −1 app/{ → Events}/Listeners/ClearJwt.php
  6. +1 −1 app/{ → Events}/Listeners/CreateJwt.php
  7. +2 −2 app/{Listeners/SendCheckUrlNotification.php → Events/Listeners/SendCheckUrlNotifications.php}
  8. +2 −1 app/{ → Events}/Listeners/SendDeploymentNotifications.php
  9. +1 −1 app/{ → Events}/Listeners/SendEmailChangeConfirmation.php
  10. +3 −2 app/{Listeners/SendHeartbeatNotification.php → Events/Listeners/SendHeartbeatNotifications.php}
  11. +1 −1 app/{ → Events}/Listeners/SendSignupEmail.php
  12. +1 −1 app/{ → Events}/Listeners/TestProjectUrls.php
  13. +36 −0 app/Events/Observers/ChannelObserver.php
  14. +60 −0 app/Events/Observers/CheckUrlObserver.php
  15. +23 −0 app/Events/Observers/HeartbeatObserver.php
  16. +46 −0 app/Events/Observers/ProjectObserver.php
  17. +43 −0 app/Events/Observers/ServerLogObserver.php
  18. +0 −15 app/Heartbeat.php
  19. +3 −1 app/Jobs/RequestProjectCheckUrl.php
  20. +1 −1 app/Notifications/Configurable/DeploymentFinished.php
  21. +1 −1 app/Notifications/Configurable/HeartbeatChanged.php
  22. +1 −1 app/Notifications/Configurable/UrlChanged.php
  23. +1 −27 app/Project.php
  24. +36 −12 app/Providers/EventServiceProvider.php
  25. +0 −20 app/ServerLog.php
  26. +1 −0 config/app.php
  27. +1 −1 config/view.php
  28. +4 −2 database/seeds/DatabaseSeeder.php
  29. 0 storage/framework/testing/.gitignore
  30. +115 −0 tests/Unit/ChannelTest.php
  31. 0 tests/Unit/DataProvider/Listeners/{SendCheckUrlNotification.yml → SendCheckUrlNotifications.yml}
  32. +1 −1 tests/Unit/DataProvider/Listeners/SendDeploymentNotifications.yml
  33. 0 tests/Unit/DataProvider/Listeners/{SendSignupEmail.yml → SendHeartbeatNotifications.yml}
  34. +0 −126 tests/Unit/Database/ChannelTest.php
  35. +11 −94 tests/Unit/Database/CheckUrlTest.php
  36. +0 −41 tests/Unit/Database/HeartbeatTest.php
  37. +0 −159 tests/Unit/Database/ProjectTest.php
  38. +0 −49 tests/Unit/Database/ServerLogTest.php
  39. +3 −3 tests/Unit/{ → Events}/Listeners/ClearJwtTest.php
  40. +3 −3 tests/Unit/{ → Events}/Listeners/CreateJwtTest.php
  41. +7 −6 ...{Listeners/SendCheckUrlNotificationTest.php → Events/Listeners/SendCheckUrlNotificationsTest.php}
  42. +4 −4 tests/Unit/{ → Events}/Listeners/SendDeploymentNotificationsTest.php
  43. +3 −3 tests/Unit/{ → Events}/Listeners/SendEmailChangeConfirmationTest.php
  44. +7 −6 ...isteners/SendHeartbeatNotificationTest.php → Events/Listeners/SendHeartbeatNotificationsTest.php}
  45. +3 −3 tests/Unit/{ → Events}/Listeners/SendSignupEmailTest.php
  46. +3 −3 tests/Unit/{ → Events}/Listeners/TestProjectUrlsTest.php
  47. +31 −0 tests/Unit/Events/Observers/ChannelObserverTest.php
  48. +141 −0 tests/Unit/Events/Observers/CheckUrlObserverTest.php
  49. +63 −0 tests/Unit/Events/Observers/HeartbeatObserverTest.php
  50. +178 −0 tests/Unit/Events/Observers/ProjectObserverTest.php
  51. +70 −0 tests/Unit/Events/Observers/ServerLogObserverTest.php
  52. +1 −2 tests/Unit/HeartbeatTest.php
  53. +5 −3 tests/Unit/Jobs/RequestProjectCheckUrlTest.php
  54. +2 −3 tests/Unit/Repositories/EloquentUserRepositoryTest.php
View
@@ -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"
@@ -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
View
@@ -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;
/**
@@ -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.
*
View
@@ -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;
@@ -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.
*
@@ -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();
}
/**
@@ -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;
}
/**
View
@@ -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'));
});
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Session\Store;
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\UrlChanged;
@@ -10,7 +10,7 @@
/**
* Event handler class for URL notifications.
**/
class SendCheckUrlNotification
class SendCheckUrlNotifications
{
/**
* @var Translator
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\DeploymentFinished;
@@ -41,6 +41,7 @@ public function handle(DeploymentFinished $event)
$notification = DeploymentFailed::class;
$event = 'deployment_failure';
if ($deployment->isSuccessful()) {
$notification = DeploymentSucceeded::class;
$event = 'deployment_success';
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\EmailChangeRequested;
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\HeartbeatChanged;
@@ -10,7 +10,7 @@
/**
* Event handler class for heartbeat notifications.
**/
class SendHeartbeatNotification
class SendHeartbeatNotifications
{
/**
* @var Translator
@@ -36,6 +36,7 @@ public function handle(HeartbeatChanged $event)
$notification = HeartbeatRecovered::class;
$event = 'heartbeat_recovered';
if (!$heartbeat->isHealthy()) {
$notification = HeartbeatMissing::class;
$event = 'heartbeat_missing';
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Translation\Translator;
use REBELinBLUE\Deployer\Events\UserWasCreated;
@@ -1,6 +1,6 @@
<?php
namespace REBELinBLUE\Deployer\Listeners;
namespace REBELinBLUE\Deployer\Events\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\DispatchesJobs;
@@ -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));
}
}
@@ -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));
}
}
}
@@ -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();
}
}
}
@@ -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));
}
}
}
Oops, something went wrong.

0 comments on commit 6536878

Please sign in to comment.