From 18f98d19f0876fc683c11ec8df14ee11983d1a00 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 23 Sep 2015 18:38:42 +0100 Subject: [PATCH] Added UpdateMetric and UpdateMetricPoint commands --- app/Commands/Metric/UpdateMetricCommand.php | 112 ++++++++++++++++++ .../Metric/UpdateMetricPointCommand.php | 64 ++++++++++ .../Metric/MetricPointWasUpdatedEvent.php | 34 ++++++ app/Events/Metric/MetricWasUpdatedEvent.php | 34 ++++++ .../Metric/UpdateMetricCommandHandler.php | 57 +++++++++ .../UpdateMetricPointCommandHandler.php | 49 ++++++++ app/Http/Controllers/Api/MetricController.php | 12 +- .../Controllers/Api/MetricPointController.php | 22 ++-- .../Dashboard/MetricController.php | 12 +- 9 files changed, 384 insertions(+), 12 deletions(-) create mode 100644 app/Commands/Metric/UpdateMetricCommand.php create mode 100644 app/Commands/Metric/UpdateMetricPointCommand.php create mode 100644 app/Events/Metric/MetricPointWasUpdatedEvent.php create mode 100644 app/Events/Metric/MetricWasUpdatedEvent.php create mode 100644 app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php diff --git a/app/Commands/Metric/UpdateMetricCommand.php b/app/Commands/Metric/UpdateMetricCommand.php new file mode 100644 index 00000000..b6284f89 --- /dev/null +++ b/app/Commands/Metric/UpdateMetricCommand.php @@ -0,0 +1,112 @@ + 'string', + 'suffix' => 'string', + 'display_chart' => 'boolean', + 'default_value' => 'numeric', + 'places' => 'numeric|min:0|max:4', + ]; + + /** + * Create a new update metric command instance. + * + * @param \CachetHQ\Cachet\Models\Metric $metric + * @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(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places) + { + $this->metric = $metric; + $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/UpdateMetricPointCommand.php b/app/Commands/Metric/UpdateMetricPointCommand.php new file mode 100644 index 00000000..7d3cdb9f --- /dev/null +++ b/app/Commands/Metric/UpdateMetricPointCommand.php @@ -0,0 +1,64 @@ +point = $point; + $this->metric = $metric; + $this->value = $value; + $this->createdAt = $createdAt; + } +} diff --git a/app/Events/Metric/MetricPointWasUpdatedEvent.php b/app/Events/Metric/MetricPointWasUpdatedEvent.php new file mode 100644 index 00000000..a1275d66 --- /dev/null +++ b/app/Events/Metric/MetricPointWasUpdatedEvent.php @@ -0,0 +1,34 @@ +point = $point; + } +} diff --git a/app/Events/Metric/MetricWasUpdatedEvent.php b/app/Events/Metric/MetricWasUpdatedEvent.php new file mode 100644 index 00000000..0cdcf0de --- /dev/null +++ b/app/Events/Metric/MetricWasUpdatedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php b/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php new file mode 100644 index 00000000..11d0ce56 --- /dev/null +++ b/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php @@ -0,0 +1,57 @@ +metric; + + $metric->update($this->filterMetricData($command)); + + event(new MetricWasUpdatedEvent($metric)); + + return $metric; + } + + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand $command + * + * @return array + */ + protected function filterMetricData($command) + { + return array_filter([ + 'name' => $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, + ]); + } +} diff --git a/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php new file mode 100644 index 00000000..b340b107 --- /dev/null +++ b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php @@ -0,0 +1,49 @@ +point; + $metric = $command->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'); + } + + $point->update($data); + + event(new MetricPointWasUpdatedEvent($point)); + + return $point; + } +} diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index cbf5c467..8d0f4f17 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Metric\AddMetricCommand; use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand; +use CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand; use CachetHQ\Cachet\Models\Metric; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -96,7 +97,16 @@ class MetricController extends AbstractApiController public function putMetric(Metric $metric) { try { - $metric->update(Binput::all()); + $metric = $this->dispatch(new UpdateMetricCommand( + $metric, + 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(); } diff --git a/app/Http/Controllers/Api/MetricPointController.php b/app/Http/Controllers/Api/MetricPointController.php index dfe7f225..bbf95801 100644 --- a/app/Http/Controllers/Api/MetricPointController.php +++ b/app/Http/Controllers/Api/MetricPointController.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand; use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand; +use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; use Carbon\Carbon; @@ -48,7 +49,11 @@ class MetricPointController extends AbstractApiController public function postMetricPoints(Metric $metric) { try { - $metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp'))); + $metricPoint = $this->dispatch(new AddMetricPointCommand( + $metric, + Binput::get('value'), + Binput::get('timestamp')) + ); } catch (Exception $e) { throw new BadRequestHttpException(); } @@ -66,15 +71,12 @@ class MetricPointController extends AbstractApiController */ public function putMetricPoint(Metric $metric, MetricPoint $metricPoint) { - $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'); - } - - $metricPoint->update($metricPointData); + $metricPoint = $this->dispatch(new UpdateMetricPointCommand( + $metricPoint, + $metric, + Binput::get('value'), + Binput::get('timestamp') + )); return $this->item($metricPoint); } diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index 4fe2790e..a00b2bf2 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -14,6 +14,7 @@ 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\Commands\Metric\UpdateMetricCommand; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; use GrahamCampbell\Binput\Facades\Binput; @@ -132,7 +133,16 @@ class MetricController extends Controller public function editMetricAction(Metric $metric) { try { - $metric->update(Binput::get('metric', null, false)); + $this->dispatch(new UpdateMetricCommand( + $metric, + Binput::get('metric.name', null, false), + Binput::get('metric.suffix', null, false), + Binput::get('metric.description', null, false), + Binput::get('metric.default_value', null, false), + Binput::get('metric.calc_type', null, false), + Binput::get('metric.display_chart', null, false), + Binput::get('metric.places', null, false) + )); } catch (ValidationException $e) { return Redirect::route('dashboard.metrics.edit', ['id' => $metric->id]) ->withInput(Binput::all())