From 11b4ab5c6c1275faf4b65b18425df77f0cd77e01 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Mon, 3 Aug 2015 12:51:00 -0500 Subject: [PATCH 01/24] Start working on the commands --- .../Incident/ReportIncidentCommand.php | 70 +++++++++++++++++ app/Commands/Metric/AddNewMetricCommand.php | 78 +++++++++++++++++++ .../Subscriber/SubscribeCustomerCommand.php | 34 ++++++++ app/Commands/User/AddTeamMemberCommand.php | 61 +++++++++++++++ app/Commands/User/GenerateApiTokenCommand.php | 34 ++++++++ .../User/GenerateApiTokenCommandHandler.php | 35 +++++++++ 6 files changed, 312 insertions(+) create mode 100644 app/Commands/Incident/ReportIncidentCommand.php create mode 100644 app/Commands/Metric/AddNewMetricCommand.php create mode 100644 app/Commands/Subscriber/SubscribeCustomerCommand.php create mode 100644 app/Commands/User/AddTeamMemberCommand.php create mode 100644 app/Commands/User/GenerateApiTokenCommand.php create mode 100644 app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php diff --git a/app/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php new file mode 100644 index 00000000..3bf1e000 --- /dev/null +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -0,0 +1,70 @@ +name = $name; + $this->status = $status; + $this->message = $message; + $this->visible = $visible; + $this->component = $component; + } +} diff --git a/app/Commands/Metric/AddNewMetricCommand.php b/app/Commands/Metric/AddNewMetricCommand.php new file mode 100644 index 00000000..5ebadbaa --- /dev/null +++ b/app/Commands/Metric/AddNewMetricCommand.php @@ -0,0 +1,78 @@ +name = $name; + $this->suffix = $suffix; + $this->description = $description; + $this->default = $default; + $this->chart = $chart; + } +} diff --git a/app/Commands/Subscriber/SubscribeCustomerCommand.php b/app/Commands/Subscriber/SubscribeCustomerCommand.php new file mode 100644 index 00000000..f7051d3d --- /dev/null +++ b/app/Commands/Subscriber/SubscribeCustomerCommand.php @@ -0,0 +1,34 @@ +email = $email; + } +} diff --git a/app/Commands/User/AddTeamMemberCommand.php b/app/Commands/User/AddTeamMemberCommand.php new file mode 100644 index 00000000..07be0219 --- /dev/null +++ b/app/Commands/User/AddTeamMemberCommand.php @@ -0,0 +1,61 @@ +username = $username; + $this->password = $password; + $this->email = $email; + $this->level = $level; + } +} diff --git a/app/Commands/User/GenerateApiTokenCommand.php b/app/Commands/User/GenerateApiTokenCommand.php new file mode 100644 index 00000000..71a75fdc --- /dev/null +++ b/app/Commands/User/GenerateApiTokenCommand.php @@ -0,0 +1,34 @@ +user = $user; + } +} diff --git a/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php b/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php new file mode 100644 index 00000000..fc169218 --- /dev/null +++ b/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php @@ -0,0 +1,35 @@ +user; + + $user->api_key = User::generateApiKey(); + $user->save(); + + //event(new GeneratedApiTokenEvent($user)); + } +} From 8250e4ebcaedec63fc33293b9fa313af16d13075 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Thu, 6 Aug 2015 21:16:49 -0500 Subject: [PATCH 02/24] Subscribe and unsubscribe subscribers via commands --- .../Subscriber/SubscribeSubscriberCommand.php | 52 +++++++++++++++++++ .../UnsubscribeSubscriberCommand.php | 36 +++++++++++++ ...ommand.php => VerifySubscriberCommand.php} | 16 +++--- app/Events/SubscriberHasSubscribedEvent.php | 32 ++++++++++++ app/Events/SubscriberHasUnsubscribedEvent.php | 32 ++++++++++++ ...ent.php => SubscriberHasVerifiedEvent.php} | 6 +-- .../SubscribeSubscriberCommandHandler.php | 43 +++++++++++++++ .../UnsubscribeSubscriberCommandHandler.php | 35 +++++++++++++ .../VerifySubscriberCommandHandler.php | 37 +++++++++++++ .../User/GenerateApiTokenCommandHandler.php | 4 +- ...SendSubscriberVerificationEmailHandler.php | 4 +- .../Controllers/Api/SubscriberController.php | 17 +++--- .../Dashboard/SubscriberController.php | 10 ++-- app/Http/Controllers/SubscribeController.php | 22 ++++---- app/Providers/AppServiceProvider.php | 2 +- app/Providers/EventServiceProvider.php | 2 +- 16 files changed, 311 insertions(+), 39 deletions(-) create mode 100644 app/Commands/Subscriber/SubscribeSubscriberCommand.php create mode 100644 app/Commands/Subscriber/UnsubscribeSubscriberCommand.php rename app/Commands/Subscriber/{SubscribeCustomerCommand.php => VerifySubscriberCommand.php} (54%) create mode 100644 app/Events/SubscriberHasSubscribedEvent.php create mode 100644 app/Events/SubscriberHasUnsubscribedEvent.php rename app/Events/{CustomerHasSubscribedEvent.php => SubscriberHasVerifiedEvent.php} (80%) create mode 100644 app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php create mode 100644 app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php create mode 100644 app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php diff --git a/app/Commands/Subscriber/SubscribeSubscriberCommand.php b/app/Commands/Subscriber/SubscribeSubscriberCommand.php new file mode 100644 index 00000000..d409d18f --- /dev/null +++ b/app/Commands/Subscriber/SubscribeSubscriberCommand.php @@ -0,0 +1,52 @@ + 'required|email', + ]; + + /** + * Create a new subscribe subscriber command instance. + * + * @param string $email + * @param bool $verified + * + * @return void + */ + public function __construct($email, $verified = false) + { + $this->email = $email; + $this->verified = $verified; + } +} diff --git a/app/Commands/Subscriber/UnsubscribeSubscriberCommand.php b/app/Commands/Subscriber/UnsubscribeSubscriberCommand.php new file mode 100644 index 00000000..e49c0ffe --- /dev/null +++ b/app/Commands/Subscriber/UnsubscribeSubscriberCommand.php @@ -0,0 +1,36 @@ +subscriber = $subscriber; + } +} diff --git a/app/Commands/Subscriber/SubscribeCustomerCommand.php b/app/Commands/Subscriber/VerifySubscriberCommand.php similarity index 54% rename from app/Commands/Subscriber/SubscribeCustomerCommand.php rename to app/Commands/Subscriber/VerifySubscriberCommand.php index f7051d3d..91bf6414 100644 --- a/app/Commands/Subscriber/SubscribeCustomerCommand.php +++ b/app/Commands/Subscriber/VerifySubscriberCommand.php @@ -11,24 +11,26 @@ namespace CachetHQ\Cachet\Commands\Subscriber; -class SubscribeCustomerCommand +use CachetHQ\Cachet\Models\Subscriber; + +class VerifySubscriberCommand { /** - * The customer email. + * The subscriber to verify. * * @var string */ - public $email; + public $subscriber; /** - * Create a new subscribe customer command instance. + * Create a verify subscriber command instance. * - * @param string $email + * @param string $subscriber * * @return void */ - public function __construct($email) + public function __construct(Subscriber $subscriber) { - $this->email = $email; + $this->subscriber = $subscriber; } } diff --git a/app/Events/SubscriberHasSubscribedEvent.php b/app/Events/SubscriberHasSubscribedEvent.php new file mode 100644 index 00000000..db6fd5a0 --- /dev/null +++ b/app/Events/SubscriberHasSubscribedEvent.php @@ -0,0 +1,32 @@ +subscriber = $subscriber; + } +} diff --git a/app/Events/SubscriberHasUnsubscribedEvent.php b/app/Events/SubscriberHasUnsubscribedEvent.php new file mode 100644 index 00000000..8c20a98d --- /dev/null +++ b/app/Events/SubscriberHasUnsubscribedEvent.php @@ -0,0 +1,32 @@ +subscriber = $subscriber; + } +} diff --git a/app/Events/CustomerHasSubscribedEvent.php b/app/Events/SubscriberHasVerifiedEvent.php similarity index 80% rename from app/Events/CustomerHasSubscribedEvent.php rename to app/Events/SubscriberHasVerifiedEvent.php index 809f579b..e095ddb9 100644 --- a/app/Events/CustomerHasSubscribedEvent.php +++ b/app/Events/SubscriberHasVerifiedEvent.php @@ -13,17 +13,17 @@ namespace CachetHQ\Cachet\Events; use CachetHQ\Cachet\Models\Subscriber; -class CustomerHasSubscribedEvent +class SubscriberHasVerifiedEvent { /** - * The customer who has subscribed. + * The subscriber who has verified. * * @var \CachetHQ\Cachet\Models\Subscriber */ public $subscriber; /** - * Create a new customer has subscribed event instance. + * Create a new subscriber has subscribed event instance. * * @return void */ diff --git a/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php new file mode 100644 index 00000000..07ae6fae --- /dev/null +++ b/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php @@ -0,0 +1,43 @@ + $command->email]); + + if ($command->verified) { + $this->dispatch(new VerifySubscriberCommand($subscriber)); + } else { + event(new SubscriberHasSubscribedEvent($subscriber)); + } + + return $subscriber; + } +} diff --git a/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php new file mode 100644 index 00000000..7552b820 --- /dev/null +++ b/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php @@ -0,0 +1,35 @@ +subscriber; + + event(new SubscriberHasUnsubscribedEvent($subscriber)); + + $subscriber->delete(); + } +} diff --git a/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php new file mode 100644 index 00000000..25f94e42 --- /dev/null +++ b/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php @@ -0,0 +1,37 @@ +subscriber; + + $subscriber->verified_at = Carbon::now(); + $subscriber->save(); + + event(new SubscriberHasVerifiedEvent($subscriber)); + } +} diff --git a/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php b/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php index fc169218..1751dc19 100644 --- a/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php +++ b/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Handlers\Commands\User; -use CachetHQ\Cachet\Commands\GenerateApiTokenCommand; +use CachetHQ\Cachet\Commands\User\GenerateApiTokenCommand; use CachetHQ\Cachet\Models\User; class GenerateApiTokenCommandHandler @@ -19,7 +19,7 @@ class GenerateApiTokenCommandHandler /** * Handle the generate api key command. * - * @param \CachetHQ\Cachet\Commands\GenerateApiTokenCommand $command + * @param \CachetHQ\Cachet\Commands\User\GenerateApiTokenCommand $command * * @return void */ diff --git a/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php b/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php index 656344a9..1de26a31 100644 --- a/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php +++ b/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Handlers\Events; -use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent; +use CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; @@ -43,7 +43,7 @@ class SendSubscriberVerificationEmailHandler * * @return void */ - public function handle(CustomerHasSubscribedEvent $event) + public function handle(SubscriberHasSubscribedEvent $event) { $mail = [ 'email' => $event->subscriber->email, diff --git a/app/Http/Controllers/Api/SubscriberController.php b/app/Http/Controllers/Api/SubscriberController.php index 6efa8163..eca4e985 100644 --- a/app/Http/Controllers/Api/SubscriberController.php +++ b/app/Http/Controllers/Api/SubscriberController.php @@ -11,15 +11,19 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; -use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent; +use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; +use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; use CachetHQ\Cachet\Models\Subscriber; use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class SubscriberController extends AbstractApiController { + use DispatchesJobs; + /** * Get all subscribers. * @@ -41,19 +45,12 @@ class SubscriberController extends AbstractApiController */ public function postSubscribers() { - $subscriberData = Binput::except('verify'); - try { - $subscriber = Subscriber::create($subscriberData); + $subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verified', false))); } catch (Exception $e) { throw new BadRequestHttpException(); } - // If we're auto-verifying the subscriber, don't bother with this event. - if (!(Binput::get('verify'))) { - event(new CustomerHasSubscribedEvent($subscriber)); - } - return $this->item($subscriber); } @@ -66,7 +63,7 @@ class SubscriberController extends AbstractApiController */ public function deleteSubscriber(Subscriber $subscriber) { - $subscriber->delete(); + $this->dispatch(new UnsubscribeSubscriberCommand($subscriber)); return $this->noContent(); } diff --git a/app/Http/Controllers/Dashboard/SubscriberController.php b/app/Http/Controllers/Dashboard/SubscriberController.php index ad3e5cd0..50a8bb15 100644 --- a/app/Http/Controllers/Dashboard/SubscriberController.php +++ b/app/Http/Controllers/Dashboard/SubscriberController.php @@ -12,15 +12,19 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent; +use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; +use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; use CachetHQ\Cachet\Models\Subscriber; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; class SubscriberController extends Controller { + use DispatchesJobs; + /** * Shows the subscribers view. * @@ -56,7 +60,7 @@ class SubscriberController extends Controller $email = Binput::get('email'); try { - $subscriber = Subscriber::create(['email' => $email]); + $this->dispatch(new SubscribeSubscriberCommand($email)); } catch (ValidationException $e) { return Redirect::route('dashboard.subscribers.add') ->withInput(Binput::all()) @@ -81,7 +85,7 @@ class SubscriberController extends Controller */ public function deleteSubscriberAction(Subscriber $subscriber) { - $subscriber->delete(); + $this->dispatch(new UnsubscribeSubscriberCommand($subscriber)); return Redirect::route('dashboard.subscribers.index'); } diff --git a/app/Http/Controllers/SubscribeController.php b/app/Http/Controllers/SubscribeController.php index 36e9dc29..ab29ef48 100644 --- a/app/Http/Controllers/SubscribeController.php +++ b/app/Http/Controllers/SubscribeController.php @@ -12,19 +12,24 @@ namespace CachetHQ\Cachet\Http\Controllers; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent; +use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; +use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; +use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Subscriber; -use Carbon\Carbon; use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Markdown\Facades\Markdown; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class SubscribeController extends Controller { + use DispatchesJobs; + /** * Show the subscribe by email page. * @@ -45,7 +50,7 @@ class SubscribeController extends Controller public function postSubscribe() { try { - $subscriber = Subscriber::create(['email' => Binput::get('email')]); + $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'))); } catch (ValidationException $e) { return Redirect::route('subscribe.subscribe') ->withInput(Binput::all()) @@ -53,8 +58,6 @@ class SubscribeController extends Controller ->withErrors($e->getMessageBag()); } - event(new CustomerHasSubscribedEvent($subscriber)); - return Redirect::route('status-page') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed'))); } @@ -75,11 +78,10 @@ class SubscribeController extends Controller $subscriber = Subscriber::where('verify_code', '=', $code)->first(); if (!$subscriber || $subscriber->verified()) { - return Redirect::route('status-page'); + throw new BadRequestHttpException(); } - $subscriber->verified_at = Carbon::now(); - $subscriber->save(); + $this->dispatch(new VerifySubscriberCommand($subscriber)); return Redirect::route('status-page') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.verified'))); @@ -101,10 +103,10 @@ class SubscribeController extends Controller $subscriber = Subscriber::where('verify_code', '=', $code)->first(); if (!$subscriber || !$subscriber->verified()) { - return Redirect::route('status-page'); + throw new BadRequestHttpException(); } - $subscriber->delete(); + $this->dispatch(new UnsubscribeSubscriberCommand($subscriber)); return Redirect::route('status-page') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.unsuscribed'))); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 8a5d804e..7a016273 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -25,7 +25,7 @@ class AppServiceProvider extends ServiceProvider public function boot(Dispatcher $dispatcher) { $dispatcher->mapUsing(function ($command) { - return Dispatcher::simpleMapping($command, 'CachetHQ\Cachet\Commands', 'CachetHQ\Cachet\Handlers\Commands'); + return Dispatcher::simpleMapping($command, 'CachetHQ\Cachet', 'CachetHQ\Cachet\Handlers'); }); Str::macro('canonicalize', function ($url) { diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 05914609..1f0e5cc6 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -21,7 +21,7 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - 'CachetHQ\Cachet\Events\CustomerHasSubscribedEvent' => [ + 'CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent' => [ 'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler', ], 'CachetHQ\Cachet\Events\IncidentHasReportedEvent' => [ From 924eee975279d86babf150514bcc82524f9ab319 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Fri, 7 Aug 2015 00:16:54 -0700 Subject: [PATCH 03/24] Fix tests --- tests/Api/SubscriberTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Api/SubscriberTest.php b/tests/Api/SubscriberTest.php index 6b34b8e4..d5ebc04e 100644 --- a/tests/Api/SubscriberTest.php +++ b/tests/Api/SubscriberTest.php @@ -40,7 +40,7 @@ class SubscriberTest extends AbstractTestCase { $this->beUser(); - $this->expectsEvents('CachetHQ\Cachet\Events\CustomerHasSubscribedEvent'); + $this->expectsEvents('CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent'); $this->post('/api/v1/subscribers', [ 'email' => 'james@cachethq.io', From a93472f544b0d158c4cf99b5760b850fecf70c6d Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 15 Aug 2015 15:10:31 -0500 Subject: [PATCH 04/24] Namespace events --- app/Events/{ => Incident}/IncidentHasReportedEvent.php | 0 .../{ => Incident}/MaintenanceHasScheduledEvent.php | 0 .../{ => Subscriber}/SubscriberHasSubscribedEvent.php | 2 +- .../SubscriberHasUnsubscribedEvent.php | 2 +- .../{ => Subscriber}/SubscriberHasVerifiedEvent.php | 2 +- .../Subscriber/SubscribeSubscriberCommandHandler.php | 2 +- .../Subscriber/UnsubscribeSubscriberCommandHandler.php | 2 +- .../Subscriber/VerifySubscriberCommandHandler.php | 2 +- .../SendIncidentEmailNotificationHandler.php | 6 +++--- .../SendMaintenanceEmailNotificationHandler.php | 2 +- .../SendSubscriberVerificationEmailHandler.php | 2 +- app/Http/Controllers/Api/IncidentController.php | 2 +- app/Http/Controllers/Dashboard/IncidentController.php | 2 +- app/Providers/EventServiceProvider.php | 10 +++++----- 14 files changed, 18 insertions(+), 18 deletions(-) rename app/Events/{ => Incident}/IncidentHasReportedEvent.php (100%) rename app/Events/{ => Incident}/MaintenanceHasScheduledEvent.php (100%) rename app/Events/{ => Subscriber}/SubscriberHasSubscribedEvent.php (93%) rename app/Events/{ => Subscriber}/SubscriberHasUnsubscribedEvent.php (93%) rename app/Events/{ => Subscriber}/SubscriberHasVerifiedEvent.php (93%) rename app/Handlers/Events/{ => Incident}/SendIncidentEmailNotificationHandler.php (94%) rename app/Handlers/Events/{ => Incident}/SendMaintenanceEmailNotificationHandler.php (98%) rename app/Handlers/Events/{ => Subscriber}/SendSubscriberVerificationEmailHandler.php (96%) diff --git a/app/Events/IncidentHasReportedEvent.php b/app/Events/Incident/IncidentHasReportedEvent.php similarity index 100% rename from app/Events/IncidentHasReportedEvent.php rename to app/Events/Incident/IncidentHasReportedEvent.php diff --git a/app/Events/MaintenanceHasScheduledEvent.php b/app/Events/Incident/MaintenanceHasScheduledEvent.php similarity index 100% rename from app/Events/MaintenanceHasScheduledEvent.php rename to app/Events/Incident/MaintenanceHasScheduledEvent.php diff --git a/app/Events/SubscriberHasSubscribedEvent.php b/app/Events/Subscriber/SubscriberHasSubscribedEvent.php similarity index 93% rename from app/Events/SubscriberHasSubscribedEvent.php rename to app/Events/Subscriber/SubscriberHasSubscribedEvent.php index db6fd5a0..b1c8297f 100644 --- a/app/Events/SubscriberHasSubscribedEvent.php +++ b/app/Events/Subscriber/SubscriberHasSubscribedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Subscriber; use CachetHQ\Cachet\Models\Subscriber; diff --git a/app/Events/SubscriberHasUnsubscribedEvent.php b/app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php similarity index 93% rename from app/Events/SubscriberHasUnsubscribedEvent.php rename to app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php index 8c20a98d..cbd1ec83 100644 --- a/app/Events/SubscriberHasUnsubscribedEvent.php +++ b/app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Subscriber; use CachetHQ\Cachet\Models\Subscriber; diff --git a/app/Events/SubscriberHasVerifiedEvent.php b/app/Events/Subscriber/SubscriberHasVerifiedEvent.php similarity index 93% rename from app/Events/SubscriberHasVerifiedEvent.php rename to app/Events/Subscriber/SubscriberHasVerifiedEvent.php index e095ddb9..0d072f12 100644 --- a/app/Events/SubscriberHasVerifiedEvent.php +++ b/app/Events/Subscriber/SubscriberHasVerifiedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Subscriber; use CachetHQ\Cachet\Models\Subscriber; diff --git a/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php index 07ae6fae..e96fb8fe 100644 --- a/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php +++ b/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php @@ -13,7 +13,7 @@ namespace CachetHQ\Cachet\Handlers\Commands\Subscriber; use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand; -use CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent; +use CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Foundation\Bus\DispatchesJobs; diff --git a/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php index 7552b820..904e1fe5 100644 --- a/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php +++ b/app/Handlers/Commands/Subscriber/UnsubscribeSubscriberCommandHandler.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Handlers\Commands\Subscriber; use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; -use CachetHQ\Cachet\Events\SubscriberHasUnsubscribedEvent; +use CachetHQ\Cachet\Events\Subscriber\SubscriberHasUnsubscribedEvent; use CachetHQ\Cachet\Models\Subscriber; class UnsubscribeSubscriberCommandHandler diff --git a/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php index 25f94e42..9f4c1ba3 100644 --- a/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php +++ b/app/Handlers/Commands/Subscriber/VerifySubscriberCommandHandler.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Handlers\Commands\Subscriber; use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand; -use CachetHQ\Cachet\Events\SubscriberHasVerifiedEvent; +use CachetHQ\Cachet\Events\Subscriber\SubscriberHasVerifiedEvent; use CachetHQ\Cachet\Models\Subscriber; use Carbon\Carbon; diff --git a/app/Handlers/Events/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php similarity index 94% rename from app/Handlers/Events/SendIncidentEmailNotificationHandler.php rename to app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 4f8abc02..9c44ee74 100644 --- a/app/Handlers/Events/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -9,9 +9,9 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Handlers\Events; +namespace CachetHQ\Cachet\Handlers\Events\Incident; -use CachetHQ\Cachet\Events\IncidentHasReportedEvent; +use CachetHQ\Cachet\Events\Incident\HasReportedEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; @@ -59,7 +59,7 @@ class SendIncidentEmailNotificationHandler /** * Handle the event. * - * @param \CachetHQ\Cachet\Events\IncidentHasReportedEvent $event + * @param \CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent $event * * @return void */ diff --git a/app/Handlers/Events/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php similarity index 98% rename from app/Handlers/Events/SendMaintenanceEmailNotificationHandler.php rename to app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index 983d8358..b1195c34 100644 --- a/app/Handlers/Events/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Handlers\Events; +namespace CachetHQ\Cachet\Handlers\Events\Incident; use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent; use CachetHQ\Cachet\Models\Subscriber; diff --git a/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php b/app/Handlers/Events/Subscriber/SendSubscriberVerificationEmailHandler.php similarity index 96% rename from app/Handlers/Events/SendSubscriberVerificationEmailHandler.php rename to app/Handlers/Events/Subscriber/SendSubscriberVerificationEmailHandler.php index 1de26a31..593734f9 100644 --- a/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php +++ b/app/Handlers/Events/Subscriber/SendSubscriberVerificationEmailHandler.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Handlers\Events; +namespace CachetHQ\Cachet\Handlers\Events\Subscriber; use CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent; use Illuminate\Contracts\Mail\MailQueue; diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 69026e29..16d5d3c9 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; -use CachetHQ\Cachet\Events\IncidentHasReportedEvent; +use CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index cdb68d93..e89f69c7 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\IncidentHasReportedEvent; +use CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 1f0e5cc6..aef6d1e6 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -21,14 +21,14 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - 'CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent' => [ + 'CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent' => [ 'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler', ], - 'CachetHQ\Cachet\Events\IncidentHasReportedEvent' => [ - 'CachetHQ\Cachet\Handlers\Events\SendIncidentEmailNotificationHandler', + 'CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent' => [ + 'CachetHQ\Cachet\Handlers\Events\Incident\SendIncidentEmailNotificationHandler', ], - 'CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent' => [ - 'CachetHQ\Cachet\Handlers\Events\SendMaintenanceEmailNotificationHandler', + 'CachetHQ\Cachet\Events\Incident\MaintenanceHasScheduledEvent' => [ + 'CachetHQ\Cachet\Handlers\Events\Incident\SendMaintenanceEmailNotificationHandler', ], ]; } From 64ff4d73c24e40e7495d2e5e66f9983028d33a2e Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 15 Aug 2015 22:15:06 -0500 Subject: [PATCH 05/24] Rename incident events and fixes --- ...Event.php => IncidentWasReportedEvent.php} | 2 +- ...t.php => MaintenanceWasScheduledEvent.php} | 2 +- app/Events/SubscriberHasVerifiedEvent.php | 34 ++++++++++ .../SendIncidentEmailNotificationHandler.php | 2 +- ...endMaintenanceEmailNotificationHandler.php | 8 ++- ...SendSubscriberVerificationEmailHandler.php | 62 +++++++++++++++++++ .../Controllers/Api/IncidentController.php | 4 +- .../Controllers/Api/SubscriberController.php | 2 +- .../Dashboard/IncidentController.php | 4 +- .../Dashboard/ScheduleController.php | 4 +- .../Dashboard/SubscriberController.php | 6 +- app/Providers/EventServiceProvider.php | 4 +- tests/Api/SubscriberTest.php | 2 +- 13 files changed, 116 insertions(+), 20 deletions(-) rename app/Events/Incident/{IncidentHasReportedEvent.php => IncidentWasReportedEvent.php} (95%) rename app/Events/Incident/{MaintenanceHasScheduledEvent.php => MaintenanceWasScheduledEvent.php} (94%) create mode 100644 app/Events/SubscriberHasVerifiedEvent.php create mode 100644 app/Handlers/Events/SendSubscriberVerificationEmailHandler.php diff --git a/app/Events/Incident/IncidentHasReportedEvent.php b/app/Events/Incident/IncidentWasReportedEvent.php similarity index 95% rename from app/Events/Incident/IncidentHasReportedEvent.php rename to app/Events/Incident/IncidentWasReportedEvent.php index 81f13990..82e4fa6a 100644 --- a/app/Events/Incident/IncidentHasReportedEvent.php +++ b/app/Events/Incident/IncidentWasReportedEvent.php @@ -13,7 +13,7 @@ namespace CachetHQ\Cachet\Events; use CachetHQ\Cachet\Models\Incident; -class IncidentHasReportedEvent +class IncidentWasReportedEvent { /** * The incident that has been reported. diff --git a/app/Events/Incident/MaintenanceHasScheduledEvent.php b/app/Events/Incident/MaintenanceWasScheduledEvent.php similarity index 94% rename from app/Events/Incident/MaintenanceHasScheduledEvent.php rename to app/Events/Incident/MaintenanceWasScheduledEvent.php index 4aea3173..b7b7f079 100644 --- a/app/Events/Incident/MaintenanceHasScheduledEvent.php +++ b/app/Events/Incident/MaintenanceWasScheduledEvent.php @@ -13,7 +13,7 @@ namespace CachetHQ\Cachet\Events; use CachetHQ\Cachet\Models\Incident; -class MaintenanceHasScheduledEvent +class MaintenanceWasScheduledEvent { /** * The incident that has been reported. diff --git a/app/Events/SubscriberHasVerifiedEvent.php b/app/Events/SubscriberHasVerifiedEvent.php new file mode 100644 index 00000000..e095ddb9 --- /dev/null +++ b/app/Events/SubscriberHasVerifiedEvent.php @@ -0,0 +1,34 @@ +subscriber = $subscriber; + } +} diff --git a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 9c44ee74..4914f0cc 100644 --- a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -63,7 +63,7 @@ class SendIncidentEmailNotificationHandler * * @return void */ - public function handle(IncidentHasReportedEvent $event) + public function handle(IncidentWasReportedEvent $event) { $incident = $this->presenter->decorate($event->incident); $component = $this->presenter->decorate($event->incident->component); diff --git a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index b1195c34..5b03d6f7 100644 --- a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Handlers\Events\Incident; -use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent; +use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; @@ -59,11 +59,15 @@ class SendMaintenanceEmailNotificationHandler /** * Handle the event. * +<<<<<<< cc10f8f42347c5fdfe7e464bedd3a388059ebc4b * @param \CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent $event * * @return void +======= + * @param \CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent $event +>>>>>>> Rename incident events and fixes */ - public function handle(MaintenanceHasScheduledEvent $event) + public function handle(MaintenanceWasScheduledEvent $event) { $data = $this->presenter->decorate($event->incident); diff --git a/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php b/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php new file mode 100644 index 00000000..1de26a31 --- /dev/null +++ b/app/Handlers/Events/SendSubscriberVerificationEmailHandler.php @@ -0,0 +1,62 @@ +mailer = $mailer; + } + + /** + * Handle the event. + * + * @param \CachetHQ\Cachet\Events\CustomerHasSubscribedEvent $event + * + * @return void + */ + public function handle(SubscriberHasSubscribedEvent $event) + { + $mail = [ + 'email' => $event->subscriber->email, + 'subject' => 'Confirm your subscription.', + 'link' => route('subscribe.verify', ['code' => $event->subscriber->verify_code]), + 'app_url' => env('APP_URL'), + ]; + + $this->mailer->queue([ + 'html' => 'emails.subscribers.verify-html', + 'text' => 'emails.subscribers.verify-text', + ], $mail, function (Message $message) use ($mail) { + $message->to($mail['email'])->subject($mail['subject']); + }); + } +} diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 16d5d3c9..97c95e69 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; -use CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent; +use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -80,7 +80,7 @@ class IncidentController extends AbstractApiController } if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentHasReportedEvent($incident)); + event(new IncidentWasReportedEvent($incident)); } return $this->item($incident); diff --git a/app/Http/Controllers/Api/SubscriberController.php b/app/Http/Controllers/Api/SubscriberController.php index eca4e985..0f5d75ab 100644 --- a/app/Http/Controllers/Api/SubscriberController.php +++ b/app/Http/Controllers/Api/SubscriberController.php @@ -46,7 +46,7 @@ class SubscriberController extends AbstractApiController public function postSubscribers() { try { - $subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verified', false))); + $subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verify', false))); } catch (Exception $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index e89f69c7..51024510 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent; +use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; @@ -133,7 +133,7 @@ class IncidentController extends Controller } if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentHasReportedEvent($incident)); + event(new IncidentWasReportedEvent($incident)); } return Redirect::route('dashboard.incidents.add') diff --git a/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index 9cdc8e9c..caafec10 100644 --- a/app/Http/Controllers/Dashboard/ScheduleController.php +++ b/app/Http/Controllers/Dashboard/ScheduleController.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent; +use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; @@ -118,7 +118,7 @@ class ScheduleController extends Controller } if (array_get($scheduleData, 'notify') && subscribers_enabled()) { - event(new MaintenanceHasScheduledEvent($incident)); + event(new MaintenanceWasScheduledEvent($incident)); } return Redirect::route('dashboard.schedule.add') diff --git a/app/Http/Controllers/Dashboard/SubscriberController.php b/app/Http/Controllers/Dashboard/SubscriberController.php index 50a8bb15..22798a41 100644 --- a/app/Http/Controllers/Dashboard/SubscriberController.php +++ b/app/Http/Controllers/Dashboard/SubscriberController.php @@ -57,10 +57,8 @@ class SubscriberController extends Controller */ public function createSubscriberAction() { - $email = Binput::get('email'); - try { - $this->dispatch(new SubscribeSubscriberCommand($email)); + $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'))); } catch (ValidationException $e) { return Redirect::route('dashboard.subscribers.add') ->withInput(Binput::all()) @@ -68,8 +66,6 @@ class SubscriberController extends Controller ->withErrors($e->getMessageBag()); } - event(new CustomerHasSubscribedEvent($subscriber)); - return Redirect::route('dashboard.subscribers.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.subscribers.add.success'))); } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index aef6d1e6..7646ad66 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -24,10 +24,10 @@ class EventServiceProvider extends ServiceProvider 'CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent' => [ 'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler', ], - 'CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent' => [ + 'CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent' => [ 'CachetHQ\Cachet\Handlers\Events\Incident\SendIncidentEmailNotificationHandler', ], - 'CachetHQ\Cachet\Events\Incident\MaintenanceHasScheduledEvent' => [ + 'CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent' => [ 'CachetHQ\Cachet\Handlers\Events\Incident\SendMaintenanceEmailNotificationHandler', ], ]; diff --git a/tests/Api/SubscriberTest.php b/tests/Api/SubscriberTest.php index d5ebc04e..403430f1 100644 --- a/tests/Api/SubscriberTest.php +++ b/tests/Api/SubscriberTest.php @@ -40,7 +40,7 @@ class SubscriberTest extends AbstractTestCase { $this->beUser(); - $this->expectsEvents('CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent'); + $this->expectsEvents('CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent'); $this->post('/api/v1/subscribers', [ 'email' => 'james@cachethq.io', From 9581c5a394b18b6cf1407e9b2918c417e88bf623 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 15 Aug 2015 22:15:41 -0500 Subject: [PATCH 06/24] Add and remove metrics and metric points commands --- ...MetricCommand.php => AddMetricCommand.php} | 47 +++++++++++----- app/Commands/Metric/AddMetricPointCommand.php | 54 +++++++++++++++++++ app/Commands/Metric/RemoveMetricCommand.php | 36 +++++++++++++ .../Metric/RemoveMetricPointCommand.php | 36 +++++++++++++ .../Metric/MetricPointWasAddedEvent.php | 34 ++++++++++++ .../Metric/MetricPointWasRemovedEvent.php | 34 ++++++++++++ app/Events/Metric/MetricWasAddedEvent.php | 34 ++++++++++++ app/Events/Metric/MetricWasRemovedEvent.php | 34 ++++++++++++ .../Metric/AddMetricCommandHandler.php | 43 +++++++++++++++ .../Metric/AddMetricPointCommandHandler.php | 48 +++++++++++++++++ .../Metric/RemoveMetricCommandHandler.php | 35 ++++++++++++ .../RemoveMetricPointCommandHandler.php | 35 ++++++++++++ app/Http/Controllers/Api/MetricController.php | 17 +++++- .../Controllers/Api/MetricPointController.php | 18 +++---- .../Dashboard/MetricController.php | 9 +++- tests/Api/MetricTest.php | 1 + 16 files changed, 489 insertions(+), 26 deletions(-) rename app/Commands/Metric/{AddNewMetricCommand.php => AddMetricCommand.php} (52%) create mode 100644 app/Commands/Metric/AddMetricPointCommand.php create mode 100644 app/Commands/Metric/RemoveMetricCommand.php create mode 100644 app/Commands/Metric/RemoveMetricPointCommand.php create mode 100644 app/Events/Metric/MetricPointWasAddedEvent.php create mode 100644 app/Events/Metric/MetricPointWasRemovedEvent.php create mode 100644 app/Events/Metric/MetricWasAddedEvent.php create mode 100644 app/Events/Metric/MetricWasRemovedEvent.php create mode 100644 app/Handlers/Commands/Metric/AddMetricCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/RemoveMetricCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php diff --git a/app/Commands/Metric/AddNewMetricCommand.php b/app/Commands/Metric/AddMetricCommand.php similarity index 52% rename from app/Commands/Metric/AddNewMetricCommand.php rename to app/Commands/Metric/AddMetricCommand.php index 5ebadbaa..b65072ce 100644 --- a/app/Commands/Metric/AddNewMetricCommand.php +++ b/app/Commands/Metric/AddMetricCommand.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Commands\Metric; -class AddNewMetricCommand +class AddMetricCommand { /** * The metric name. @@ -39,40 +39,63 @@ class AddNewMetricCommand * * @var float */ - public $default; + public $default_value; /** - * The metric calc type. + * The metric calculation type. * * @var int */ - public $type; + public $calc_type; /** * The metric display chart. * * @var int */ - public $chart; + public $display_chart; /** - * Create a new add team member command instance. + * The metric decimal places. + * + * @var int + */ + public $places; + + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'name' => 'required', + 'suffix' => 'required', + 'display_chart' => 'boolean', + 'default_value' => 'numeric', + 'places' => 'numeric|min:0|max:4', + ]; + + /** + * Create a new add metric command instance. * * @param string $name * @param string $suffix * @param string $description - * @param float $default - * @param int $type - * @param int $chart + * @param float $default_value + * @param int $calc_type + * @param int $display_chart + * @param int $places * * @return void */ - public function __construct($name, $suffix, $description, $default, $type, $chart) + public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places) { $this->name = $name; $this->suffix = $suffix; $this->description = $description; - $this->default = $default; - $this->chart = $chart; + $this->default_value = $default_value; + $this->calc_type = $calc_type; + $this->display_chart = $display_chart; + $this->places = $places; } } diff --git a/app/Commands/Metric/AddMetricPointCommand.php b/app/Commands/Metric/AddMetricPointCommand.php new file mode 100644 index 00000000..e0b676b8 --- /dev/null +++ b/app/Commands/Metric/AddMetricPointCommand.php @@ -0,0 +1,54 @@ +metric = $metric; + $this->value = $value; + $this->createdAt = $createdAt; + } +} diff --git a/app/Commands/Metric/RemoveMetricCommand.php b/app/Commands/Metric/RemoveMetricCommand.php new file mode 100644 index 00000000..a893551f --- /dev/null +++ b/app/Commands/Metric/RemoveMetricCommand.php @@ -0,0 +1,36 @@ +metric = $metric; + } +} diff --git a/app/Commands/Metric/RemoveMetricPointCommand.php b/app/Commands/Metric/RemoveMetricPointCommand.php new file mode 100644 index 00000000..c305b238 --- /dev/null +++ b/app/Commands/Metric/RemoveMetricPointCommand.php @@ -0,0 +1,36 @@ +metricPoint = $metricPoint; + } +} diff --git a/app/Events/Metric/MetricPointWasAddedEvent.php b/app/Events/Metric/MetricPointWasAddedEvent.php new file mode 100644 index 00000000..e5b47b77 --- /dev/null +++ b/app/Events/Metric/MetricPointWasAddedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Events/Metric/MetricPointWasRemovedEvent.php b/app/Events/Metric/MetricPointWasRemovedEvent.php new file mode 100644 index 00000000..6315749b --- /dev/null +++ b/app/Events/Metric/MetricPointWasRemovedEvent.php @@ -0,0 +1,34 @@ +metricPoint = $metricPoint; + } +} diff --git a/app/Events/Metric/MetricWasAddedEvent.php b/app/Events/Metric/MetricWasAddedEvent.php new file mode 100644 index 00000000..b579e7ad --- /dev/null +++ b/app/Events/Metric/MetricWasAddedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Events/Metric/MetricWasRemovedEvent.php b/app/Events/Metric/MetricWasRemovedEvent.php new file mode 100644 index 00000000..41c5c63e --- /dev/null +++ b/app/Events/Metric/MetricWasRemovedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Handlers/Commands/Metric/AddMetricCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php new file mode 100644 index 00000000..1a329984 --- /dev/null +++ b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php @@ -0,0 +1,43 @@ + $command->name, + 'suffix' => $command->suffix, + 'description' => $command->description, + 'default_value' => $command->default_value, + 'calc_type' => $command->calc_type, + 'display_chart' => $command->display_chart, + 'places' => $command->places, + ]); + + event(new MetricWasAddedEvent($metric)); + + return $metric; + } +} diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php new file mode 100644 index 00000000..c690f1cb --- /dev/null +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -0,0 +1,48 @@ +metric; + $createdAt = $command->createdAt; + + $data = [ + 'metric_id' => $metric->id, + 'value' => $command->value, + ]; + + if ($createdAt) { + $data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s'); + } + + $metricPoint = MetricPoint::create($data); + + event(new MetricPointWasAddedEvent($metricPoint)); + + return $metricPoint; + } +} diff --git a/app/Handlers/Commands/Metric/RemoveMetricCommandHandler.php b/app/Handlers/Commands/Metric/RemoveMetricCommandHandler.php new file mode 100644 index 00000000..dd27fbaf --- /dev/null +++ b/app/Handlers/Commands/Metric/RemoveMetricCommandHandler.php @@ -0,0 +1,35 @@ +metric; + + event(new MetricWasRemovedEvent($metric)); + + $metric->delete(); + } +} diff --git a/app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php new file mode 100644 index 00000000..a6967abf --- /dev/null +++ b/app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php @@ -0,0 +1,35 @@ +metricPoint; + + event(new MetricPointWasRemovedEvent($metricPoint)); + + $metricPoint->delete(); + } +} diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index 140b2735..cbf5c467 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -11,14 +11,19 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\Metric\AddMetricCommand; +use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand; use CachetHQ\Cachet\Models\Metric; use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class MetricController extends AbstractApiController { + use DispatchesJobs; + /** * Get all metrics. * @@ -65,7 +70,15 @@ class MetricController extends AbstractApiController public function postMetrics() { try { - $metric = Metric::create(Binput::all()); + $metric = $this->dispatch(new AddMetricCommand( + Binput::get('name'), + Binput::get('suffix'), + Binput::get('description'), + Binput::get('default_value'), + Binput::get('calc_type', 0), + Binput::get('display_chart'), + Binput::get('places') + )); } catch (Exception $e) { throw new BadRequestHttpException(); } @@ -100,7 +113,7 @@ class MetricController extends AbstractApiController */ public function deleteMetric(Metric $metric) { - $metric->delete(); + $this->dispatch(new RemoveMetricCommand($metric)); return $this->noContent(); } diff --git a/app/Http/Controllers/Api/MetricPointController.php b/app/Http/Controllers/Api/MetricPointController.php index d10a210d..dfe7f225 100644 --- a/app/Http/Controllers/Api/MetricPointController.php +++ b/app/Http/Controllers/Api/MetricPointController.php @@ -11,14 +11,20 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand; +use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; use Carbon\Carbon; use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class MetricPointController extends AbstractApiController { + use DispatchesJobs; + /** * Get a single metric point. * @@ -41,16 +47,8 @@ class MetricPointController extends AbstractApiController */ public function postMetricPoints(Metric $metric) { - $metricPointData = Binput::all(); - $metricPointData['metric_id'] = $metric->id; - - if ($timestamp = array_pull($metricPointData, 'timestamp')) { - $pointTimestamp = Carbon::createFromFormat('U', $timestamp); - $metricPointData['created_at'] = $pointTimestamp->format('Y-m-d H:i:s'); - } - try { - $metricPoint = MetricPoint::create($metricPointData); + $metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp'))); } catch (Exception $e) { throw new BadRequestHttpException(); } @@ -91,7 +89,7 @@ class MetricPointController extends AbstractApiController */ public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint) { - $metricPoint->delete(); + $this->dispatch(new RemoveMetricPointCommand($metricPoint)); return $this->noContent(); } diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index 21b6f785..4fe2790e 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -12,15 +12,20 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; +use CachetHQ\Cachet\Commands\Metric\AddMetricCommand; +use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; class MetricController extends Controller { + use DispatchesJobs; + /** * Shows the metrics view. * @@ -66,7 +71,7 @@ class MetricController extends Controller public function createMetricAction() { try { - Metric::create(Binput::get('metric')); + $this->dispatchFromArray(AddMetricCommand::class, Binput::get('metric')); } catch (ValidationException $e) { return Redirect::route('dashboard.metrics.add') ->withInput(Binput::all()) @@ -98,7 +103,7 @@ class MetricController extends Controller */ public function deleteMetricAction(Metric $metric) { - $metric->delete(); + $this->dispatch(new RemoveMetricCommand($metric)); return Redirect::route('dashboard.metrics.index'); } diff --git a/tests/Api/MetricTest.php b/tests/Api/MetricTest.php index bf6960ae..eafab7a2 100644 --- a/tests/Api/MetricTest.php +++ b/tests/Api/MetricTest.php @@ -59,6 +59,7 @@ class MetricTest extends AbstractTestCase 'description' => 'Lorem ipsum dolor', 'default_value' => 1, 'display_chart' => 1, + 'places' => 0, ]); $this->seeJson(['name' => 'Foo']); $this->assertResponseOk(); From 7c1f27c4a1920999793accc2fde42bc5222f64f6 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 15 Aug 2015 22:17:24 -0500 Subject: [PATCH 07/24] CS Fixes --- app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php | 2 +- .../Events/Incident/SendIncidentEmailNotificationHandler.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php index c690f1cb..918468b1 100644 --- a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -11,10 +11,10 @@ namespace CachetHQ\Cachet\Handlers\Commands\Metric; -use Carbon\Carbon; use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand; use CachetHQ\Cachet\Events\Metric\MetricPointWasAddedEvent; use CachetHQ\Cachet\Models\MetricPoint; +use Carbon\Carbon; class AddMetricPointCommandHandler { diff --git a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 4914f0cc..0421ec14 100644 --- a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Handlers\Events\Incident; -use CachetHQ\Cachet\Events\Incident\HasReportedEvent; +use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; From 5bc7a3e6f7aba2bb5f42327a67ffb9ab4ea8d7ec Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 16 Aug 2015 13:42:56 +0100 Subject: [PATCH 08/24] Fix CS --- app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php index 918468b1..14d37e41 100644 --- a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -31,8 +31,8 @@ class AddMetricPointCommandHandler $createdAt = $command->createdAt; $data = [ - 'metric_id' => $metric->id, - 'value' => $command->value, + 'metric_id' => $metric->id, + 'value' => $command->value, ]; if ($createdAt) { From 30eec9da247de8d6e872e75128833c7339c9e237 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 16 Aug 2015 13:50:11 +0100 Subject: [PATCH 09/24] Added Component events and handlers --- .../Component/ComponentWasAddedEvent.php | 34 +++++++++++++++ .../Component/ComponentWasRemovedEvent.php | 34 +++++++++++++++ .../Component/AddComponentHandler.php | 42 +++++++++++++++++++ .../Component/RemoveComponentHandler.php | 35 ++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 app/Events/Component/ComponentWasAddedEvent.php create mode 100644 app/Events/Component/ComponentWasRemovedEvent.php create mode 100644 app/Handlers/Commands/Component/AddComponentHandler.php create mode 100644 app/Handlers/Commands/Component/RemoveComponentHandler.php diff --git a/app/Events/Component/ComponentWasAddedEvent.php b/app/Events/Component/ComponentWasAddedEvent.php new file mode 100644 index 00000000..d882e0a8 --- /dev/null +++ b/app/Events/Component/ComponentWasAddedEvent.php @@ -0,0 +1,34 @@ +component = $component; + } +} diff --git a/app/Events/Component/ComponentWasRemovedEvent.php b/app/Events/Component/ComponentWasRemovedEvent.php new file mode 100644 index 00000000..56a666f2 --- /dev/null +++ b/app/Events/Component/ComponentWasRemovedEvent.php @@ -0,0 +1,34 @@ +component = $component; + } +} diff --git a/app/Handlers/Commands/Component/AddComponentHandler.php b/app/Handlers/Commands/Component/AddComponentHandler.php new file mode 100644 index 00000000..7c2988cd --- /dev/null +++ b/app/Handlers/Commands/Component/AddComponentHandler.php @@ -0,0 +1,42 @@ + $command->name, + 'description' => $command->description, + 'link' => $command->link, + 'status' => $command->status, + 'order' => $command->order, + 'group_id' => $command->group_id, + ]); + + event(new ComponentWasAddedEvent($component)); + + return $component; + } +} diff --git a/app/Handlers/Commands/Component/RemoveComponentHandler.php b/app/Handlers/Commands/Component/RemoveComponentHandler.php new file mode 100644 index 00000000..2e82f61f --- /dev/null +++ b/app/Handlers/Commands/Component/RemoveComponentHandler.php @@ -0,0 +1,35 @@ +component; + + event(new ComponentWasRemovedEvent($component)); + + $component->delete(); + } +} From 4a5110ae535ed07789a66ad467ff7d76abfceb02 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Mon, 24 Aug 2015 14:38:10 -0500 Subject: [PATCH 10/24] Finish add and remove components via commands --- .../Component/AddComponentCommand.php | 90 +++++++++++++++++++ .../Component/RemoveComponentCommand.php | 36 ++++++++ .../Component/ComponentWasAddedEvent.php | 2 +- .../Component/ComponentWasRemovedEvent.php | 2 +- ...ler.php => AddComponentCommandHandler.php} | 2 +- ....php => RemoveComponentCommandHandler.php} | 4 +- .../Controllers/Api/ComponentController.php | 18 +++- .../Dashboard/ComponentController.php | 9 +- 8 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 app/Commands/Component/AddComponentCommand.php create mode 100644 app/Commands/Component/RemoveComponentCommand.php rename app/Handlers/Commands/Component/{AddComponentHandler.php => AddComponentCommandHandler.php} (97%) rename app/Handlers/Commands/Component/{RemoveComponentHandler.php => RemoveComponentCommandHandler.php} (88%) diff --git a/app/Commands/Component/AddComponentCommand.php b/app/Commands/Component/AddComponentCommand.php new file mode 100644 index 00000000..0dc850ac --- /dev/null +++ b/app/Commands/Component/AddComponentCommand.php @@ -0,0 +1,90 @@ + 'required|string', + 'status' => 'required|integer', + 'link' => 'url', + ]; + + /** + * Create a new add metric command instance. + * + * @param string $name + * @param string $description + * @param int $status + * @param string $link + * @param int $order + * @param int $group_id + * + * @return void + */ + public function __construct($name, $description, $status, $link, $order, $group_id) + { + $this->name = $name; + $this->description = $description; + $this->status = (int) $status; + $this->link = $link; + $this->order = $order; + $this->group_id = $group_id; + } +} diff --git a/app/Commands/Component/RemoveComponentCommand.php b/app/Commands/Component/RemoveComponentCommand.php new file mode 100644 index 00000000..80dbe1ae --- /dev/null +++ b/app/Commands/Component/RemoveComponentCommand.php @@ -0,0 +1,36 @@ +component = $component; + } +} diff --git a/app/Events/Component/ComponentWasAddedEvent.php b/app/Events/Component/ComponentWasAddedEvent.php index d882e0a8..5629642d 100644 --- a/app/Events/Component/ComponentWasAddedEvent.php +++ b/app/Events/Component/ComponentWasAddedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events\Metric; +namespace CachetHQ\Cachet\Events\Component; use CachetHQ\Cachet\Models\Component; diff --git a/app/Events/Component/ComponentWasRemovedEvent.php b/app/Events/Component/ComponentWasRemovedEvent.php index 56a666f2..e2280f21 100644 --- a/app/Events/Component/ComponentWasRemovedEvent.php +++ b/app/Events/Component/ComponentWasRemovedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events\Metric; +namespace CachetHQ\Cachet\Events\Component; use CachetHQ\Cachet\Models\Component; diff --git a/app/Handlers/Commands/Component/AddComponentHandler.php b/app/Handlers/Commands/Component/AddComponentCommandHandler.php similarity index 97% rename from app/Handlers/Commands/Component/AddComponentHandler.php rename to app/Handlers/Commands/Component/AddComponentCommandHandler.php index 7c2988cd..d160aabf 100644 --- a/app/Handlers/Commands/Component/AddComponentHandler.php +++ b/app/Handlers/Commands/Component/AddComponentCommandHandler.php @@ -15,7 +15,7 @@ use CachetHQ\Cachet\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Events\Component\ComponentWasAddedEvent; use CachetHQ\Cachet\Models\Component; -class AddComponentHandler +class AddComponentCommandHandler { /** * Handle the add component command. diff --git a/app/Handlers/Commands/Component/RemoveComponentHandler.php b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php similarity index 88% rename from app/Handlers/Commands/Component/RemoveComponentHandler.php rename to app/Handlers/Commands/Component/RemoveComponentCommandHandler.php index 2e82f61f..da74fd36 100644 --- a/app/Handlers/Commands/Component/RemoveComponentHandler.php +++ b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php @@ -12,10 +12,10 @@ namespace CachetHQ\Cachet\Handlers\Commands\Component; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; -use CachetHQ\Cachet\Events\Component\ComponentWasAddedEvent; +use CachetHQ\Cachet\Events\Component\ComponentWasRemovedEvent; use CachetHQ\Cachet\Models\Component; -class RemoveComponentHandler +class RemoveComponentCommandHandler { /** * Handle the remove component command. diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 959d2e4f..9aad79a5 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -11,16 +11,21 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\Component\AddComponentCommand; +use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Tag; use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class ComponentController extends AbstractApiController { + use DispatchesJobs; + /** * Get all components. * @@ -56,10 +61,15 @@ class ComponentController extends AbstractApiController */ public function postComponents(Guard $auth) { - $componentData = Binput::except('tags'); - try { - $component = Component::create($componentData); + $component = $this->dispatch(new AddComponentCommand( + Binput::get('name'), + Binput::get('description'), + Binput::get('status'), + Binput::get('link'), + Binput::get('order'), + Binput::get('group_id') + )); } catch (Exception $e) { throw new BadRequestHttpException(); } @@ -119,7 +129,7 @@ class ComponentController extends AbstractApiController */ public function deleteComponent(Component $component) { - $component->delete(); + $this->dispatch(new RemoveComponentCommand($component)); return $this->noContent(); } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 8af44d95..529c3c20 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -11,17 +11,22 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; +use CachetHQ\Cachet\Commands\Component\AddComponentCommand; +use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Tag; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; class ComponentController extends Controller { + use DispatchesJobs; + protected $subMenu = []; public function __construct() @@ -157,7 +162,7 @@ class ComponentController extends Controller $tags = array_pull($_component, 'tags'); try { - $component = Component::create($_component); + $component = $this->dispatchFromArray(AddComponentCommand::class, Binput::get('component')); } catch (ValidationException $e) { return Redirect::route('dashboard.components.add') ->withInput(Binput::all()) @@ -188,7 +193,7 @@ class ComponentController extends Controller */ public function deleteComponentAction(Component $component) { - $component->delete(); + $this->dispatch(new RemoveComponentCommand($component)); return Redirect::route('dashboard.components.index'); } From a239c348920079399492d1ca56c50d25b8421707 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Mon, 24 Aug 2015 14:46:09 -0500 Subject: [PATCH 11/24] CS Fixes --- app/Http/Controllers/Dashboard/ComponentController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 529c3c20..a63ce00a 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; +use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; -use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Tag; From b80b53191b31c51b3103baca347af19a265b97e9 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Aug 2015 13:14:38 +0100 Subject: [PATCH 12/24] Commands and events for removing incidents --- .../Incident/RemoveIncidentCommand.php | 36 +++++++++++++++++++ .../Incident/IncidentWasRemovedEvent.php | 32 +++++++++++++++++ .../Incident/RemoveIncidentCommandHandler.php | 35 ++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 app/Commands/Incident/RemoveIncidentCommand.php create mode 100644 app/Events/Incident/IncidentWasRemovedEvent.php create mode 100644 app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php diff --git a/app/Commands/Incident/RemoveIncidentCommand.php b/app/Commands/Incident/RemoveIncidentCommand.php new file mode 100644 index 00000000..ca095b51 --- /dev/null +++ b/app/Commands/Incident/RemoveIncidentCommand.php @@ -0,0 +1,36 @@ +incident = $incident; + } +} diff --git a/app/Events/Incident/IncidentWasRemovedEvent.php b/app/Events/Incident/IncidentWasRemovedEvent.php new file mode 100644 index 00000000..cab1a30f --- /dev/null +++ b/app/Events/Incident/IncidentWasRemovedEvent.php @@ -0,0 +1,32 @@ +incident = $incident; + } +} diff --git a/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php new file mode 100644 index 00000000..f06f0b0c --- /dev/null +++ b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php @@ -0,0 +1,35 @@ +incident; + + event(new IncidentWasRemovedEvent($incident)); + + $incident->delete(); + } +} From 7be3aa255272bbe6f48bfb3fd134981c7a1aa3ed Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Aug 2015 13:21:23 +0100 Subject: [PATCH 13/24] Incidents will now be removed via the command --- app/Http/Controllers/Api/IncidentController.php | 6 +++++- app/Http/Controllers/Dashboard/IncidentController.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 97c95e69..731aa847 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -11,16 +11,20 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class IncidentController extends AbstractApiController { + use DispatchesJobs; + /** * Get all incidents. * @@ -122,7 +126,7 @@ class IncidentController extends AbstractApiController */ public function deleteIncident(Incident $incident) { - $incident->delete(); + $this->dispatch(new RemoveIncidentCommand($incident)); return $this->noContent(); } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 51024510..1b16aaf4 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; +use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; @@ -19,6 +20,7 @@ use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; @@ -27,6 +29,8 @@ use Jenssegers\Date\Date; class IncidentController extends Controller { + use DispatchesJobs; + /** * Stores the sub-sidebar tree list. * @@ -208,7 +212,7 @@ class IncidentController extends Controller */ public function deleteIncidentAction(Incident $incident) { - $incident->delete(); + $this->dispatch(new RemoveIncidentCommand($incident)); return Redirect::route('dashboard.incidents.index'); } From c466620435afbf57d3dbb571190299675043ed2a Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Aug 2015 13:28:10 +0100 Subject: [PATCH 14/24] Added RemoveUserCommand --- app/Commands/User/RemoveUserCommand.php | 36 +++++++++++++++++++ app/Events/User/UserWasRemovedEvent.php | 32 +++++++++++++++++ .../User/RemoveUserCommandHandler.php | 35 ++++++++++++++++++ .../Controllers/Dashboard/TeamController.php | 6 +++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 app/Commands/User/RemoveUserCommand.php create mode 100644 app/Events/User/UserWasRemovedEvent.php create mode 100644 app/Handlers/Commands/User/RemoveUserCommandHandler.php diff --git a/app/Commands/User/RemoveUserCommand.php b/app/Commands/User/RemoveUserCommand.php new file mode 100644 index 00000000..c5998249 --- /dev/null +++ b/app/Commands/User/RemoveUserCommand.php @@ -0,0 +1,36 @@ +user = $user; + } +} diff --git a/app/Events/User/UserWasRemovedEvent.php b/app/Events/User/UserWasRemovedEvent.php new file mode 100644 index 00000000..f7af80fc --- /dev/null +++ b/app/Events/User/UserWasRemovedEvent.php @@ -0,0 +1,32 @@ +user = $user; + } +} diff --git a/app/Handlers/Commands/User/RemoveUserCommandHandler.php b/app/Handlers/Commands/User/RemoveUserCommandHandler.php new file mode 100644 index 00000000..7291c313 --- /dev/null +++ b/app/Handlers/Commands/User/RemoveUserCommandHandler.php @@ -0,0 +1,35 @@ +user; + + event(new UserWasRemovedEvent($user)); + + $user->delete(); + } +} diff --git a/app/Http/Controllers/Dashboard/TeamController.php b/app/Http/Controllers/Dashboard/TeamController.php index 15783969..ba518fa8 100644 --- a/app/Http/Controllers/Dashboard/TeamController.php +++ b/app/Http/Controllers/Dashboard/TeamController.php @@ -12,14 +12,18 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; +use CachetHQ\Cachet\Commands\User\RemoveUserCommand; use CachetHQ\Cachet\Models\User; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; class TeamController extends Controller { + use DispatchesJobs; + /** * Shows the team members view. * @@ -116,7 +120,7 @@ class TeamController extends Controller */ public function deleteUser(User $user) { - $user->delete(); + $this->dispatch(new RemoveUserCommand($user)); return Redirect::route('dashboard.team.index') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.team.delete.success'))); From be080a10efb933d8ff04d69b69fe9701dd81b63f Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Aug 2015 13:36:10 +0100 Subject: [PATCH 15/24] Added commands to delete component group (also fixes bug in API) --- .../RemoveComponentGroupCommand.php | 36 +++++++++++++++++ .../ComponentGroupWasAddedEvent.php | 34 ++++++++++++++++ .../ComponentGroupWasRemovedEvent.php | 34 ++++++++++++++++ .../RemoveComponentGroupCommandHandler.php | 40 +++++++++++++++++++ .../Api/ComponentGroupController.php | 6 ++- .../Dashboard/ComponentController.php | 7 +--- 6 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 app/Commands/ComponentGroup/RemoveComponentGroupCommand.php create mode 100644 app/Events/ComponentGroup/ComponentGroupWasAddedEvent.php create mode 100644 app/Events/ComponentGroup/ComponentGroupWasRemovedEvent.php create mode 100644 app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php diff --git a/app/Commands/ComponentGroup/RemoveComponentGroupCommand.php b/app/Commands/ComponentGroup/RemoveComponentGroupCommand.php new file mode 100644 index 00000000..54a212de --- /dev/null +++ b/app/Commands/ComponentGroup/RemoveComponentGroupCommand.php @@ -0,0 +1,36 @@ +group = $group; + } +} diff --git a/app/Events/ComponentGroup/ComponentGroupWasAddedEvent.php b/app/Events/ComponentGroup/ComponentGroupWasAddedEvent.php new file mode 100644 index 00000000..d76174d0 --- /dev/null +++ b/app/Events/ComponentGroup/ComponentGroupWasAddedEvent.php @@ -0,0 +1,34 @@ +group = $group; + } +} diff --git a/app/Events/ComponentGroup/ComponentGroupWasRemovedEvent.php b/app/Events/ComponentGroup/ComponentGroupWasRemovedEvent.php new file mode 100644 index 00000000..07913871 --- /dev/null +++ b/app/Events/ComponentGroup/ComponentGroupWasRemovedEvent.php @@ -0,0 +1,34 @@ +group = $group; + } +} diff --git a/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php new file mode 100644 index 00000000..669e7828 --- /dev/null +++ b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php @@ -0,0 +1,40 @@ +group; + + event(new ComponentGroupWasRemovedEvent($group)); + + // Remove the group id from all component. + $group->components->map(function ($component) { + $component->update(['group_id' => 0]); + }); + + $group->delete(); + } +} diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index c8d8a7be..2a871047 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -11,14 +11,18 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Models\ComponentGroup; use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class ComponentGroupController extends AbstractApiController { + use DispatchesJobs; + /** * Get all groups. * @@ -93,7 +97,7 @@ class ComponentGroupController extends AbstractApiController */ public function deleteGroup(ComponentGroup $group) { - $group->delete(); + $this->dispatch(new RemoveComponentGroupCommand($group)); return $this->noContent(); } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index a63ce00a..49d52fbf 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; +use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Tag; @@ -207,11 +208,7 @@ class ComponentController extends Controller */ public function deleteComponentGroupAction(ComponentGroup $group) { - $group->components->map(function ($component) { - $component->update(['group_id' => 0]); - }); - - $group->delete(); + $this->dispatch(new RemoveComponentGroupCommand($group)); return Redirect::route('dashboard.components.index'); } From 32b2a56573a0741a8aa55929443b4bda84224935 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Aug 2015 13:37:17 +0100 Subject: [PATCH 16/24] Remove unused imports --- .../Commands/Component/RemoveComponentCommandHandler.php | 1 - .../ComponentGroup/RemoveComponentGroupCommandHandler.php | 1 - app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php | 1 - 3 files changed, 3 deletions(-) diff --git a/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php index da74fd36..7f8e7b20 100644 --- a/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php +++ b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php @@ -13,7 +13,6 @@ namespace CachetHQ\Cachet\Handlers\Commands\Component; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; use CachetHQ\Cachet\Events\Component\ComponentWasRemovedEvent; -use CachetHQ\Cachet\Models\Component; class RemoveComponentCommandHandler { diff --git a/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php index 669e7828..bae19715 100644 --- a/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php +++ b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php @@ -13,7 +13,6 @@ namespace CachetHQ\Cachet\Handlers\Commands\ComponentGroup; use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Events\ComponentGroup\ComponentGroupWasRemovedEvent; -use CachetHQ\Cachet\Models\ComponentGroup; class RemoveComponentGroupCommandHandler { diff --git a/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php index f06f0b0c..9b12bbd8 100644 --- a/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php @@ -13,7 +13,6 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasRemovedEvent; -use CachetHQ\Cachet\Models\Incident; class RemoveIncidentCommandHandler { From 698b05980d5f77ff8deecbe42e31ad0f3e8566f3 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 30 Aug 2015 21:30:23 +0100 Subject: [PATCH 17/24] Fix description --- app/Commands/Component/AddComponentCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/Component/AddComponentCommand.php b/app/Commands/Component/AddComponentCommand.php index 0dc850ac..4952bf57 100644 --- a/app/Commands/Component/AddComponentCommand.php +++ b/app/Commands/Component/AddComponentCommand.php @@ -67,7 +67,7 @@ class AddComponentCommand ]; /** - * Create a new add metric command instance. + * Create a new add component command instance. * * @param string $name * @param string $description From 6ee697cf7f203556aa60955a1a5c1b1043f528e2 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 30 Aug 2015 21:37:29 +0100 Subject: [PATCH 18/24] Added AddComponentGroupCommand --- .../AddComponentGroupCommand.php | 53 +++++++++++++++++++ .../AddComponentGroupCommandHandler.php | 38 +++++++++++++ .../Api/ComponentGroupController.php | 9 ++-- .../Dashboard/ComponentController.php | 6 ++- app/Models/ComponentGroup.php | 2 +- .../dashboard/components/groups/add.blade.php | 2 +- 6 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 app/Commands/ComponentGroup/AddComponentGroupCommand.php create mode 100644 app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php diff --git a/app/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Commands/ComponentGroup/AddComponentGroupCommand.php new file mode 100644 index 00000000..1ca433d2 --- /dev/null +++ b/app/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -0,0 +1,53 @@ + 'required|string', + 'order' => 'integer', + ]; + + /** + * Create a add component group command instance. + * + * @param string $name + * @param int $order + * + * @return void + */ + public function __construct($name, $order) + { + $this->name = $name; + $this->order = (int) $order; + } +} diff --git a/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php new file mode 100644 index 00000000..4cc7025d --- /dev/null +++ b/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -0,0 +1,38 @@ + $command->name, + 'order' => $command->order, + ]); + + event(new ComponentGroupWasAddedEvent($group)); + + return $group; + } +} diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 2a871047..dcf691a3 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Models\ComponentGroup; use Exception; @@ -56,10 +57,11 @@ class ComponentGroupController extends AbstractApiController */ public function postGroups() { - $groupData = array_filter(Binput::only(['name', 'order'])); - try { - $group = ComponentGroup::create($groupData); + $group = $this->dispatch(new AddComponentGroupCommand( + Binput::get('name'), + Binput::get('order', 0) + )); } catch (Exception $e) { throw new BadRequestHttpException(); } @@ -81,7 +83,6 @@ class ComponentGroupController extends AbstractApiController try { $group->update($groupData); } catch (Exception $e) { - dd($e->getMessage()); throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 49d52fbf..84df5ec1 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; +use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; @@ -246,7 +247,10 @@ class ComponentController extends Controller public function postAddComponentGroup() { try { - $group = ComponentGroup::create(Binput::get('group')); + $group = $this->dispatch(new AddComponentGroupCommand( + Binput::get('name'), + Binput::get('order', 0) + )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') ->withInput(Binput::all()) diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 4f1cdf5d..f78aa276 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -42,7 +42,7 @@ class ComponentGroup extends Model * @var string[] */ public $rules = [ - 'name' => 'required', + 'name' => 'required|string', 'order' => 'integer', ]; diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index d0f9073b..ece35042 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -19,7 +19,7 @@
- +
From 25a3626de54fe57fe9420ba32638900a50dd3bbb Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 08:54:01 +0100 Subject: [PATCH 19/24] Fix rebase issue --- .../Incident/SendMaintenanceEmailNotificationHandler.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index 5b03d6f7..3783160c 100644 --- a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php @@ -59,13 +59,9 @@ class SendMaintenanceEmailNotificationHandler /** * Handle the event. * -<<<<<<< cc10f8f42347c5fdfe7e464bedd3a388059ebc4b * @param \CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent $event * * @return void -======= - * @param \CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent $event ->>>>>>> Rename incident events and fixes */ public function handle(MaintenanceWasScheduledEvent $event) { From 37d7908606c7a2254e6f487c3139d18a8148d586 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 09:04:35 +0100 Subject: [PATCH 20/24] Added AddTeamMemberCommand --- app/Events/User/UserWasAddedEvent.php | 32 +++++++++++++++ .../User/AddTeamMemberCommandHandler.php | 40 +++++++++++++++++++ .../Controllers/Dashboard/TeamController.php | 8 +++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/Events/User/UserWasAddedEvent.php create mode 100644 app/Handlers/Commands/User/AddTeamMemberCommandHandler.php diff --git a/app/Events/User/UserWasAddedEvent.php b/app/Events/User/UserWasAddedEvent.php new file mode 100644 index 00000000..e230ba95 --- /dev/null +++ b/app/Events/User/UserWasAddedEvent.php @@ -0,0 +1,32 @@ +user = $user; + } +} diff --git a/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php b/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php new file mode 100644 index 00000000..c377c8df --- /dev/null +++ b/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php @@ -0,0 +1,40 @@ + $command->username, + 'password' => $command->password, + 'email' => $command->email, + 'level' => $command->level, + ]); + + event(new UserWasAddedEvent($user)); + + return $user; + } +} diff --git a/app/Http/Controllers/Dashboard/TeamController.php b/app/Http/Controllers/Dashboard/TeamController.php index ba518fa8..4730e3d1 100644 --- a/app/Http/Controllers/Dashboard/TeamController.php +++ b/app/Http/Controllers/Dashboard/TeamController.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; +use CachetHQ\Cachet\Commands\User\AddTeamMemberCommand; use CachetHQ\Cachet\Commands\User\RemoveUserCommand; use CachetHQ\Cachet\Models\User; use GrahamCampbell\Binput\Facades\Binput; @@ -69,7 +70,12 @@ class TeamController extends Controller public function postAddUser() { try { - User::create(Binput::all()); + $this->dispatch(new AddTeamMemberCommand( + Binput::get('username'), + Binput::get('password'), + Binput::get('email'), + Binput::get('level') + )); } catch (ValidationException $e) { return Redirect::route('dashboard.team.add') ->withInput(Binput::except('password')) From 082062fa1b7bb8b9e5da575f7e2f9edc02e7ac2c Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 11:32:39 +0100 Subject: [PATCH 21/24] Added ReportIncidentCommand --- .../Incident/ReportIncidentCommand.php | 26 ++++++++-- .../Incident/IncidentWasReportedEvent.php | 2 +- .../Incident/MaintenanceWasScheduledEvent.php | 2 +- .../Incident/ReportIncidentCommandHandler.php | 52 +++++++++++++++++++ .../SendIncidentEmailNotificationHandler.php | 2 +- .../Controllers/Api/IncidentController.php | 30 ++++------- .../Dashboard/IncidentController.php | 37 ++++++------- .../views/dashboard/incidents/add.blade.php | 22 ++++---- 8 files changed, 115 insertions(+), 58 deletions(-) create mode 100644 app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php diff --git a/app/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php index 3bf1e000..073e183a 100644 --- a/app/Commands/Incident/ReportIncidentCommand.php +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -46,7 +46,21 @@ class ReportIncidentCommand * * @var int */ - public $component; + public $component_id; + + /** + * The component status. + * + * @var int + */ + public $component_status; + + /** + * Whether to notify about the incident or not. + * + * @var bool + */ + public $notify; /** * Create a new report incident command instance. @@ -55,16 +69,20 @@ class ReportIncidentCommand * @param int $status * @param string $message * @param int $visible - * @param int $component + * @param int $component_id + * @param int $component_status + * @param bool $notify * * @return void */ - public function __construct($name, $status, $message, $visible, $component) + public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify) { $this->name = $name; $this->status = $status; $this->message = $message; $this->visible = $visible; - $this->component = $component; + $this->component_id = $component_id; + $this->component_status = $component_status; + $this->notify = $notify; } } diff --git a/app/Events/Incident/IncidentWasReportedEvent.php b/app/Events/Incident/IncidentWasReportedEvent.php index 82e4fa6a..3c15e0bf 100644 --- a/app/Events/Incident/IncidentWasReportedEvent.php +++ b/app/Events/Incident/IncidentWasReportedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; diff --git a/app/Events/Incident/MaintenanceWasScheduledEvent.php b/app/Events/Incident/MaintenanceWasScheduledEvent.php index b7b7f079..fcd0aaaf 100644 --- a/app/Events/Incident/MaintenanceWasScheduledEvent.php +++ b/app/Events/Incident/MaintenanceWasScheduledEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php new file mode 100644 index 00000000..bd0a01a8 --- /dev/null +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -0,0 +1,52 @@ + $command->name, + 'status' => $command->status, + 'message' => $command->message, + 'visible' => $command->visible, + 'component' => $command->component_id, + ]); + + // Update the component. + if ($command->component_id) { + Component::find($command->component_id)->update([ + 'status' => $command->component_status, + ]); + } + + // Notify subscribers. + if ($command->notify) { + event(new IncidentWasReportedEvent($incident)); + } + + return $incident; + } +} diff --git a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 0421ec14..f991b08f 100644 --- a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -75,7 +75,7 @@ class SendIncidentEmailNotificationHandler 'email' => $subscriber->email, 'subject' => 'New incident reported.', 'has_component' => ($event->incident->component) ? true : false, - 'component_name' => $component->name, + 'component_name' => $component ? $component->name : null, 'status' => $incident->humanStatus, 'html_content' => $incident->formattedMessage, 'text_content' => $incident->message, diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 731aa847..34576b07 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; -use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -63,30 +63,20 @@ class IncidentController extends AbstractApiController */ public function postIncidents(Guard $auth) { - $incidentData = array_filter(Binput::only([ - 'name', - 'message', - 'status', - 'component_id', - 'notify', - 'visible', - ])); - - // Default visibility is 1. - if (!array_has($incidentData, 'visible')) { - $incidentData['visible'] = 1; - } - try { - $incident = Incident::create($incidentData); + $incident = $this->dispatch(new ReportIncidentCommand( + Binput::get('name'), + Binput::get('status'), + Binput::get('message'), + Binput::get('visible', true), + Binput::get('component_id'), + Binput::get('component_status'), + Binput::get('notify', true) + )); } catch (Exception $e) { throw new BadRequestHttpException(); } - if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentWasReportedEvent($incident)); - } - return $this->item($incident); } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 1b16aaf4..b8ea80ca 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -13,7 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; -use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; @@ -111,19 +111,25 @@ class IncidentController extends Controller */ public function createIncidentAction() { - $incidentData = Binput::get('incident'); - $componentStatus = array_pull($incidentData, 'component_status'); - - if (array_has($incidentData, 'created_at') && $incidentData['created_at']) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $incidentData['created_at'], Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); - $incidentData['created_at'] = $incidentDate; - $incidentData['updated_at'] = $incidentDate; - } else { - unset($incidentData['created_at']); + if ($createdAt = Binput::get('created_at')) { + $incidentDate = Date::createFromFormat('d/m/Y H:i', $createdAt, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); } try { - $incident = Incident::create($incidentData); + $incident = $this->dispatch(new ReportIncidentCommand( + Binput::get('name'), + Binput::get('status'), + Binput::get('message'), + Binput::get('visible', true), + Binput::get('component_id'), + Binput::get('component_status'), + Binput::get('notify', true) + )); + + $incident->update([ + 'created_at' => $incidentDate, + 'updated_at' => $incidentDate, + ]); } catch (ValidationException $e) { return Redirect::route('dashboard.incidents.add') ->withInput(Binput::all()) @@ -131,15 +137,6 @@ class IncidentController extends Controller ->withErrors($e->getMessageBag()); } - // Update the component. - if (isset($incidentData['component_id']) && (int) $incidentData['component_id'] > 0) { - Component::find($incidentData['component_id'])->update(['status' => $componentStatus]); - } - - if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentWasReportedEvent($incident)); - } - return Redirect::route('dashboard.incidents.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.add.success'))); } diff --git a/resources/views/dashboard/incidents/add.blade.php b/resources/views/dashboard/incidents/add.blade.php index b425105d..00f4ef45 100644 --- a/resources/views/dashboard/incidents/add.blade.php +++ b/resources/views/dashboard/incidents/add.blade.php @@ -30,34 +30,34 @@ @endif
- +

- @@ -65,7 +65,7 @@ @if(!$components_in_groups->isEmpty() || !$components_out_groups->isEmpty())
- @foreach($components_in_groups as $group) @@ -88,7 +88,7 @@ @foreach(trans('cachet.components.status') as $statusID => $status)
@@ -100,17 +100,17 @@
- +
- + {{ trans('forms.optional') }}
- + {{ trans('forms.optional') }}
From 75588db98eb298861dfdbe9045fd8d81c23982e3 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 13:40:23 +0100 Subject: [PATCH 22/24] Fix imports --- app/Handlers/Commands/Component/AddComponentCommandHandler.php | 2 +- .../Commands/ComponentGroup/AddComponentGroupCommandHandler.php | 2 +- app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php | 2 +- app/Handlers/Commands/Metric/AddMetricCommandHandler.php | 2 +- app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php | 2 +- app/Handlers/Commands/User/AddTeamMemberCommandHandler.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Handlers/Commands/Component/AddComponentCommandHandler.php b/app/Handlers/Commands/Component/AddComponentCommandHandler.php index d160aabf..412eb0ae 100644 --- a/app/Handlers/Commands/Component/AddComponentCommandHandler.php +++ b/app/Handlers/Commands/Component/AddComponentCommandHandler.php @@ -22,7 +22,7 @@ class AddComponentCommandHandler * * @param \CachetHQ\Cachet\Commands\Component\AddComponentCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\Component */ public function handle(AddComponentCommand $command) { diff --git a/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index 4cc7025d..afcba48f 100644 --- a/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -22,7 +22,7 @@ class AddComponentGroupCommandHandler * * @param \CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\ComponentGroup */ public function handle(AddComponentGroupCommand $command) { diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index bd0a01a8..99f4e826 100644 --- a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -23,7 +23,7 @@ class ReportIncidentCommandHandler * * @param \CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\Incident */ public function handle(ReportIncidentCommand $command) { diff --git a/app/Handlers/Commands/Metric/AddMetricCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php index 1a329984..a497897b 100644 --- a/app/Handlers/Commands/Metric/AddMetricCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php @@ -22,7 +22,7 @@ class AddMetricCommandHandler * * @param \CachetHQ\Cachet\Commands\Metric\AddMetricCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\Metric */ public function handle(AddMetricCommand $command) { diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php index 14d37e41..0f6ea677 100644 --- a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -23,7 +23,7 @@ class AddMetricPointCommandHandler * * @param \CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\MetricPoint */ public function handle(AddMetricPointCommand $command) { diff --git a/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php b/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php index c377c8df..97e8208f 100644 --- a/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php +++ b/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php @@ -22,7 +22,7 @@ class AddTeamMemberCommandHandler * * @param \CachetHQ\Cachet\Commands\User\AddTeamMemberCommand $command * - * @return void + * @return \CachetHQ\Cachet\Models\User */ public function handle(AddTeamMemberCommand $command) { From 60e9a64a3e43a80421167198e3ac36e4b4b5bfdf Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 19:32:06 +0100 Subject: [PATCH 23/24] Less use of Binput::all() --- .../Controllers/Dashboard/TeamController.php | 17 +++++++------- .../Controllers/Dashboard/UserController.php | 22 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Dashboard/TeamController.php b/app/Http/Controllers/Dashboard/TeamController.php index 4730e3d1..ff3fb4c5 100644 --- a/app/Http/Controllers/Dashboard/TeamController.php +++ b/app/Http/Controllers/Dashboard/TeamController.php @@ -96,19 +96,18 @@ class TeamController extends Controller */ public function postUpdateUser(User $user) { - $items = Binput::all(); - - $passwordChange = array_get($items, 'password'); - - if (trim($passwordChange) === '') { - unset($items['password']); - } + $userData = array_filter(Binput::only([ + 'username', + 'email', + 'password', + 'level', + ])); try { - $user->update($items); + $user->update($userData); } catch (ValidationException $e) { return Redirect::route('dashboard.team.edit', ['id' => $user->id]) - ->withInput(Binput::except('password')) + ->withInput($userData) ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure'))) ->withErrors($e->getMessageBag()); } diff --git a/app/Http/Controllers/Dashboard/UserController.php b/app/Http/Controllers/Dashboard/UserController.php index 611be7a7..3e48d18b 100644 --- a/app/Http/Controllers/Dashboard/UserController.php +++ b/app/Http/Controllers/Dashboard/UserController.php @@ -40,27 +40,27 @@ class UserController extends Controller */ public function postUser() { - $items = Binput::all(); + $userData = array_filter(Binput::only([ + 'username', + 'email', + 'password', + 'google2fa', + ])); - $passwordChange = array_get($items, 'password'); - $enable2FA = (bool) array_pull($items, 'google2fa'); + $enable2FA = (bool) array_pull($userData, 'google2fa'); // Let's enable/disable auth if ($enable2FA && !Auth::user()->hasTwoFactor) { - $items['google_2fa_secret'] = Google2FA::generateSecretKey(); + $userData['google_2fa_secret'] = Google2FA::generateSecretKey(); } elseif (!$enable2FA) { - $items['google_2fa_secret'] = ''; - } - - if (trim($passwordChange) === '') { - unset($items['password']); + $userData['google_2fa_secret'] = ''; } try { - Auth::user()->update($items); + Auth::user()->update($userData); } catch (ValidationException $e) { return Redirect::route('dashboard.user') - ->withInput(Binput::except('password')) + ->withInput($userData) ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure'))) ->withErrors($e->getMessageBag()); } From 1478a2500891155933dd921eafd2d1778b84b9ef Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 20:16:15 +0100 Subject: [PATCH 24/24] ReportMaintenanceCommand is added --- .../Incident/ReportMaintenanceCommand.php | 61 +++++++++++++++++++ app/Events/SubscriberHasVerifiedEvent.php | 34 ----------- .../ReportMaintenanceCommandHandler.php | 51 ++++++++++++++++ ...endMaintenanceEmailNotificationHandler.php | 2 +- .../Dashboard/ScheduleController.php | 32 +++------- 5 files changed, 123 insertions(+), 57 deletions(-) create mode 100644 app/Commands/Incident/ReportMaintenanceCommand.php delete mode 100644 app/Events/SubscriberHasVerifiedEvent.php create mode 100644 app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php diff --git a/app/Commands/Incident/ReportMaintenanceCommand.php b/app/Commands/Incident/ReportMaintenanceCommand.php new file mode 100644 index 00000000..82105055 --- /dev/null +++ b/app/Commands/Incident/ReportMaintenanceCommand.php @@ -0,0 +1,61 @@ +name = $name; + $this->message = $message; + $this->notify = $notify; + $this->timestamp = $timestamp; + } +} diff --git a/app/Events/SubscriberHasVerifiedEvent.php b/app/Events/SubscriberHasVerifiedEvent.php deleted file mode 100644 index e095ddb9..00000000 --- a/app/Events/SubscriberHasVerifiedEvent.php +++ /dev/null @@ -1,34 +0,0 @@ -subscriber = $subscriber; - } -} diff --git a/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php b/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php new file mode 100644 index 00000000..b9542191 --- /dev/null +++ b/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php @@ -0,0 +1,51 @@ +timestamp, Setting::get('app_timezone')) + ->setTimezone(Config::get('app.timezone')); + + $maintenanceEvent = Incident::create([ + 'name' => $command->name, + 'message' => $command->message, + 'scheduled_at' => $scheduledAt, + 'status' => 0, + 'visible' => 1, + ]); + + // Notify subscribers. + if ($command->notify) { + event(new MaintenanceWasScheduledEvent($maintenanceEvent)); + } + + return $maintenanceEvent; + } +} diff --git a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index 3783160c..baaa44bb 100644 --- a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Handlers\Events\Incident; -use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent; +use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; diff --git a/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index caafec10..3e7f94aa 100644 --- a/app/Http/Controllers/Dashboard/ScheduleController.php +++ b/app/Http/Controllers/Dashboard/ScheduleController.php @@ -12,11 +12,12 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent; +use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; @@ -26,6 +27,8 @@ use Jenssegers\Date\Date; class ScheduleController extends Controller { + use DispatchesJobs; + /** * Stores the sub-sidebar tree list. * @@ -92,24 +95,13 @@ class ScheduleController extends Controller */ public function addScheduleAction() { - $scheduleData = Binput::get('incident'); - // Parse the schedule date. - $scheduledAt = Date::createFromFormat('d/m/Y H:i', $scheduleData['scheduled_at'], Setting::get('app_timezone')) - ->setTimezone(Config::get('app.timezone')); - - if ($scheduledAt->isPast()) { - $messageBag = new MessageBag(); - $messageBag->add('scheduled_at', trans('validation.date', ['attribute' => 'scheduled time you supplied'])); - - return Redirect::route('dashboard.schedule.add')->withErrors($messageBag); - } - - $scheduleData['scheduled_at'] = $scheduledAt; - // Bypass the incident.status field. - $scheduleData['status'] = 0; - try { - $incident = Incident::create($scheduleData); + $incident = $this->dispatch(new ReportMaintenanceCommand( + Binput::get('incident.name'), + Binput::get('incident.message'), + Binput::get('incident.notify'), + Binput::get('incident.scheduled_at') + )); } catch (ValidationException $e) { return Redirect::route('dashboard.schedule.add') ->withInput(Binput::all()) @@ -117,10 +109,6 @@ class ScheduleController extends Controller ->withErrors($e->getMessageBag()); } - if (array_get($scheduleData, 'notify') && subscribers_enabled()) { - event(new MaintenanceWasScheduledEvent($incident)); - } - return Redirect::route('dashboard.schedule.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.schedule.add.success'))); }