From d3cd8201a6e006a549e4fce0f2cc6fb31198e882 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Fri, 18 Sep 2015 15:12:31 +0100 Subject: [PATCH] UpdateComponentCommand is now done --- .../Component/UpdateComponentCommand.php | 101 ++++++++++++++++++ .../Component/ComponentWasUpdatedEvent.php | 34 ++++++ .../UpdateComponentCommandHandler.php | 48 +++++++++ .../Controllers/Api/ComponentController.php | 11 +- .../Dashboard/ComponentController.php | 15 +-- 5 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 app/Commands/Component/UpdateComponentCommand.php create mode 100644 app/Events/Component/ComponentWasUpdatedEvent.php create mode 100644 app/Handlers/Commands/Component/UpdateComponentCommandHandler.php diff --git a/app/Commands/Component/UpdateComponentCommand.php b/app/Commands/Component/UpdateComponentCommand.php new file mode 100644 index 00000000..b50d0a5f --- /dev/null +++ b/app/Commands/Component/UpdateComponentCommand.php @@ -0,0 +1,101 @@ + 'string', + 'status' => 'integer', + 'link' => 'url', + ]; + + /** + * Create a new update component command instance. + * + * @param \CachetHQ\Cachet\Models\Component $component + * @param string $name + * @param string $description + * @param int $status + * @param string $link + * @param int $order + * @param int $group_id + * + * @return void + */ + public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id) + { + $this->component = $component; + $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/Events/Component/ComponentWasUpdatedEvent.php b/app/Events/Component/ComponentWasUpdatedEvent.php new file mode 100644 index 00000000..7e776f09 --- /dev/null +++ b/app/Events/Component/ComponentWasUpdatedEvent.php @@ -0,0 +1,34 @@ +component = $component; + } +} diff --git a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php new file mode 100644 index 00000000..9487309c --- /dev/null +++ b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -0,0 +1,48 @@ +component; + $component->update($this->filterComponentData($command)); + + event(new ComponentWasUpdatedEvent($component)); + + return $component; + } + + protected function filterComponentData($command) + { + return array_filter([ + 'name' => $command->name, + 'description' => $command->description, + 'link' => $command->link, + 'status' => $command->status, + 'order' => $command->order, + 'group_id' => $command->group_id, + ]); + } +} diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 9aad79a5..c15ae168 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; +use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Tag; use Exception; @@ -101,7 +102,15 @@ class ComponentController extends AbstractApiController public function putComponent(Component $component) { try { - $component->update(Binput::except('tags')); + $this->dispatch(new UpdateComponentCommand( + $component, + 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(); } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 84df5ec1..aa2c739d 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\Component\UpdateComponentCommand; use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Models\Component; @@ -114,11 +115,12 @@ class ComponentController extends Controller */ public function updateComponentAction(Component $component) { - $_component = Binput::get('component'); - $tags = array_pull($_component, 'tags'); + $componentData = Binput::get('component'); + $tags = array_pull($componentData, 'tags'); try { - $component->update($_component); + $componentData['component'] = $component; + $component = $this->dispatchFromArray(AddComponentCommand::class, $componentData); } catch (ValidationException $e) { return Redirect::route('dashboard.components.edit', ['id' => $component->id]) ->withInput(Binput::all()) @@ -159,12 +161,11 @@ class ComponentController extends Controller */ public function createComponentAction() { - $_component = Binput::get('component'); - // We deal with tags separately. - $tags = array_pull($_component, 'tags'); + $componentData = Binput::get('component'); + $tags = array_pull($componentData, 'tags'); try { - $component = $this->dispatchFromArray(AddComponentCommand::class, Binput::get('component')); + $component = $this->dispatchFromArray(AddComponentCommand::class, $componentData); } catch (ValidationException $e) { return Redirect::route('dashboard.components.add') ->withInput(Binput::all())