UpdateComponentCommand is now done

This commit is contained in:
James Brooks
2015-09-18 15:12:31 +01:00
parent 80caeea86a
commit d3cd8201a6
5 changed files with 201 additions and 8 deletions

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Commands\Component;
use CachetHQ\Cachet\Models\Component;
class UpdateComponentCommand
{
/**
* The component.
*
* @var \CachetHQ\Cachet\Models\Component
*/
public $component;
/**
* The component name.
*
* @var string
*/
public $name;
/**
* The component description.
*
* @var string
*/
public $description;
/**
* The component status.
*
* @var int
*/
public $status;
/**
* The component link.
*
* @var string
*/
public $link;
/**
* The component order.
*
* @var int
*/
public $order;
/**
* The component group.
*
* @var int
*/
public $group_id;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => '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;
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events\Component;
use CachetHQ\Cachet\Models\Component;
class ComponentWasUpdatedEvent
{
/**
* The component that was updated.
*
* @var \CachetHQ\Cachet\Models\Component
*/
public $component;
/**
* Create a new component was updated event instance.
*
* @return void
*/
public function __construct(Component $component)
{
$this->component = $component;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Component;
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Events\Component\ComponentWasUpdatedEvent;
use CachetHQ\Cachet\Models\Component;
class UpdateComponentCommandHandler
{
/**
* Handle the update component command.
*
* @param \CachetHQ\Cachet\Commands\Component\UpdateComponentCommand $command
*
* @return \CachetHQ\Cachet\Models\Component
*/
public function handle(UpdateComponentCommand $command)
{
$component = $command->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,
]);
}
}

View File

@@ -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();
}

View File

@@ -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())