diff --git a/app/Commands/Component/AddComponentCommand.php b/app/Commands/Component/AddComponentCommand.php new file mode 100644 index 00000000..4952bf57 --- /dev/null +++ b/app/Commands/Component/AddComponentCommand.php @@ -0,0 +1,90 @@ + 'required|string', + 'status' => 'required|integer', + 'link' => 'url', + ]; + + /** + * Create a new add component 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/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/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/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/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php new file mode 100644 index 00000000..073e183a --- /dev/null +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -0,0 +1,88 @@ +name = $name; + $this->status = $status; + $this->message = $message; + $this->visible = $visible; + $this->component_id = $component_id; + $this->component_status = $component_status; + $this->notify = $notify; + } +} 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/Commands/Metric/AddMetricCommand.php b/app/Commands/Metric/AddMetricCommand.php new file mode 100644 index 00000000..b65072ce --- /dev/null +++ b/app/Commands/Metric/AddMetricCommand.php @@ -0,0 +1,101 @@ + '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_value + * @param int $calc_type + * @param int $display_chart + * @param int $places + * + * @return void + */ + public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places) + { + $this->name = $name; + $this->suffix = $suffix; + $this->description = $description; + $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/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/VerifySubscriberCommand.php b/app/Commands/Subscriber/VerifySubscriberCommand.php new file mode 100644 index 00000000..91bf6414 --- /dev/null +++ b/app/Commands/Subscriber/VerifySubscriberCommand.php @@ -0,0 +1,36 @@ +subscriber = $subscriber; + } +} 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/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/Component/ComponentWasAddedEvent.php b/app/Events/Component/ComponentWasAddedEvent.php new file mode 100644 index 00000000..5629642d --- /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..e2280f21 --- /dev/null +++ b/app/Events/Component/ComponentWasRemovedEvent.php @@ -0,0 +1,34 @@ +component = $component; + } +} 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/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/Events/IncidentHasReportedEvent.php b/app/Events/Incident/IncidentWasReportedEvent.php similarity index 88% rename from app/Events/IncidentHasReportedEvent.php rename to app/Events/Incident/IncidentWasReportedEvent.php index 81f13990..3c15e0bf 100644 --- a/app/Events/IncidentHasReportedEvent.php +++ b/app/Events/Incident/IncidentWasReportedEvent.php @@ -9,11 +9,11 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; -class IncidentHasReportedEvent +class IncidentWasReportedEvent { /** * The incident that has been reported. diff --git a/app/Events/MaintenanceHasScheduledEvent.php b/app/Events/Incident/MaintenanceWasScheduledEvent.php similarity index 88% rename from app/Events/MaintenanceHasScheduledEvent.php rename to app/Events/Incident/MaintenanceWasScheduledEvent.php index 4aea3173..fcd0aaaf 100644 --- a/app/Events/MaintenanceHasScheduledEvent.php +++ b/app/Events/Incident/MaintenanceWasScheduledEvent.php @@ -9,11 +9,11 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; -class MaintenanceHasScheduledEvent +class MaintenanceWasScheduledEvent { /** * The incident that has been reported. 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/Events/Subscriber/SubscriberHasSubscribedEvent.php b/app/Events/Subscriber/SubscriberHasSubscribedEvent.php new file mode 100644 index 00000000..b1c8297f --- /dev/null +++ b/app/Events/Subscriber/SubscriberHasSubscribedEvent.php @@ -0,0 +1,32 @@ +subscriber = $subscriber; + } +} diff --git a/app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php b/app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php new file mode 100644 index 00000000..cbd1ec83 --- /dev/null +++ b/app/Events/Subscriber/SubscriberHasUnsubscribedEvent.php @@ -0,0 +1,32 @@ +subscriber = $subscriber; + } +} diff --git a/app/Events/CustomerHasSubscribedEvent.php b/app/Events/Subscriber/SubscriberHasVerifiedEvent.php similarity index 73% rename from app/Events/CustomerHasSubscribedEvent.php rename to app/Events/Subscriber/SubscriberHasVerifiedEvent.php index 809f579b..0d072f12 100644 --- a/app/Events/CustomerHasSubscribedEvent.php +++ b/app/Events/Subscriber/SubscriberHasVerifiedEvent.php @@ -9,21 +9,21 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Subscriber; 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/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/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/Component/AddComponentCommandHandler.php b/app/Handlers/Commands/Component/AddComponentCommandHandler.php new file mode 100644 index 00000000..412eb0ae --- /dev/null +++ b/app/Handlers/Commands/Component/AddComponentCommandHandler.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/RemoveComponentCommandHandler.php b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php new file mode 100644 index 00000000..7f8e7b20 --- /dev/null +++ b/app/Handlers/Commands/Component/RemoveComponentCommandHandler.php @@ -0,0 +1,34 @@ +component; + + event(new ComponentWasRemovedEvent($component)); + + $component->delete(); + } +} diff --git a/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php new file mode 100644 index 00000000..afcba48f --- /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/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php new file mode 100644 index 00000000..bae19715 --- /dev/null +++ b/app/Handlers/Commands/ComponentGroup/RemoveComponentGroupCommandHandler.php @@ -0,0 +1,39 @@ +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/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php new file mode 100644 index 00000000..9b12bbd8 --- /dev/null +++ b/app/Handlers/Commands/Incident/RemoveIncidentCommandHandler.php @@ -0,0 +1,34 @@ +incident; + + event(new IncidentWasRemovedEvent($incident)); + + $incident->delete(); + } +} diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php new file mode 100644 index 00000000..99f4e826 --- /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/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/Commands/Metric/AddMetricCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php new file mode 100644 index 00000000..a497897b --- /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..0f6ea677 --- /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/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php b/app/Handlers/Commands/Subscriber/SubscribeSubscriberCommandHandler.php new file mode 100644 index 00000000..e96fb8fe --- /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..904e1fe5 --- /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..9f4c1ba3 --- /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/AddTeamMemberCommandHandler.php b/app/Handlers/Commands/User/AddTeamMemberCommandHandler.php new file mode 100644 index 00000000..97e8208f --- /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/Handlers/Commands/User/GenerateApiTokenCommandHandler.php b/app/Handlers/Commands/User/GenerateApiTokenCommandHandler.php new file mode 100644 index 00000000..1751dc19 --- /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)); + } +} 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/Handlers/Events/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php similarity index 89% rename from app/Handlers/Events/SendIncidentEmailNotificationHandler.php rename to app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 4f8abc02..f991b08f 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\IncidentWasReportedEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; @@ -59,11 +59,11 @@ class SendIncidentEmailNotificationHandler /** * Handle the event. * - * @param \CachetHQ\Cachet\Events\IncidentHasReportedEvent $event + * @param \CachetHQ\Cachet\Events\Incident\IncidentHasReportedEvent $event * * @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); @@ -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/Handlers/Events/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php similarity index 93% rename from app/Handlers/Events/SendMaintenanceEmailNotificationHandler.php rename to app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index 983d8358..baaa44bb 100644 --- a/app/Handlers/Events/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.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\MaintenanceHasScheduledEvent; +use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Mail\Message; @@ -63,7 +63,7 @@ class SendMaintenanceEmailNotificationHandler * * @return void */ - 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 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/Handlers/Events/Subscriber/SendSubscriberVerificationEmailHandler.php b/app/Handlers/Events/Subscriber/SendSubscriberVerificationEmailHandler.php new file mode 100644 index 00000000..593734f9 --- /dev/null +++ b/app/Handlers/Events/Subscriber/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/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/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index c8d8a7be..dcf691a3 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -11,14 +11,19 @@ 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; 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. * @@ -52,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(); } @@ -77,7 +83,6 @@ class ComponentGroupController extends AbstractApiController try { $group->update($groupData); } catch (Exception $e) { - dd($e->getMessage()); throw new BadRequestHttpException(); } @@ -93,7 +98,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/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 69026e29..34576b07 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\Events\IncidentHasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; 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. * @@ -59,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 IncidentHasReportedEvent($incident)); - } - return $this->item($incident); } @@ -122,7 +116,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/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/Api/SubscriberController.php b/app/Http/Controllers/Api/SubscriberController.php index 6efa8163..0f5d75ab 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('verify', 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/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 8af44d95..84df5ec1 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -12,16 +12,23 @@ 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; 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 +164,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 +195,7 @@ class ComponentController extends Controller */ public function deleteComponentAction(Component $component) { - $component->delete(); + $this->dispatch(new RemoveComponentCommand($component)); return Redirect::route('dashboard.components.index'); } @@ -202,11 +209,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'); } @@ -244,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/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index cdb68d93..b8ea80ca 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -12,13 +12,15 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; -use CachetHQ\Cachet\Events\IncidentHasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; 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. * @@ -107,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()) @@ -127,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 IncidentHasReportedEvent($incident)); - } - return Redirect::route('dashboard.incidents.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.add.success'))); } @@ -208,7 +209,7 @@ class IncidentController extends Controller */ public function deleteIncidentAction(Incident $incident) { - $incident->delete(); + $this->dispatch(new RemoveIncidentCommand($incident)); return Redirect::route('dashboard.incidents.index'); } 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/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index 9cdc8e9c..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\MaintenanceHasScheduledEvent; +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 MaintenanceHasScheduledEvent($incident)); - } - return Redirect::route('dashboard.schedule.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.schedule.add.success'))); } diff --git a/app/Http/Controllers/Dashboard/SubscriberController.php b/app/Http/Controllers/Dashboard/SubscriberController.php index ad3e5cd0..22798a41 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. * @@ -53,10 +57,8 @@ class SubscriberController extends Controller */ public function createSubscriberAction() { - $email = Binput::get('email'); - try { - $subscriber = Subscriber::create(['email' => $email]); + $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'))); } catch (ValidationException $e) { return Redirect::route('dashboard.subscribers.add') ->withInput(Binput::all()) @@ -64,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'))); } @@ -81,7 +81,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/Dashboard/TeamController.php b/app/Http/Controllers/Dashboard/TeamController.php index 15783969..ff3fb4c5 100644 --- a/app/Http/Controllers/Dashboard/TeamController.php +++ b/app/Http/Controllers/Dashboard/TeamController.php @@ -12,14 +12,19 @@ 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; +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. * @@ -65,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')) @@ -86,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()); } @@ -116,7 +125,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'))); 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()); } 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/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/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..7646ad66 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\CustomerHasSubscribedEvent' => [ + 'CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent' => [ 'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler', ], - 'CachetHQ\Cachet\Events\IncidentHasReportedEvent' => [ - 'CachetHQ\Cachet\Handlers\Events\SendIncidentEmailNotificationHandler', + 'CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent' => [ + 'CachetHQ\Cachet\Handlers\Events\Incident\SendIncidentEmailNotificationHandler', ], - 'CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent' => [ - 'CachetHQ\Cachet\Handlers\Events\SendMaintenanceEmailNotificationHandler', + 'CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent' => [ + 'CachetHQ\Cachet\Handlers\Events\Incident\SendMaintenanceEmailNotificationHandler', ], ]; } 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 @@
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