Added UpdateMetric and UpdateMetricPoint commands

This commit is contained in:
James Brooks 2015-09-23 18:38:42 +01:00
parent b8b81f7e1c
commit 18f98d19f0
9 changed files with 384 additions and 12 deletions

View File

@ -0,0 +1,112 @@
<?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\Metric;
use CachetHQ\Cachet\Models\Metric;
class UpdateMetricCommand
{
/**
* The metric.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* The metric name.
*
* @var string
*/
public $name;
/**
* The metric suffix.
*
* @var string
*/
public $suffix;
/**
* The metric description.
*
* @var string
*/
public $description;
/**
* The metric default value.
*
* @var float
*/
public $default_value;
/**
* The metric calculation type.
*
* @var int
*/
public $calc_type;
/**
* The metric display chart.
*
* @var int
*/
public $display_chart;
/**
* The metric decimal places.
*
* @var int
*/
public $places;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => '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;
}
}

View File

@ -0,0 +1,64 @@
<?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\Metric;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
class UpdateMetricPointCommand
{
/**
* The metric point.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $point;
/**
* The metric.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
public $metric;
/**
* The metric point value.
*
* @var int
*/
public $value;
/**
* The metric point created at.
*
* @var string
*/
public $createdAt;
/**
* Create a new update metric point command instance.
*
* @param \CachetHQ\Cachet\Models\MetricPoint $point
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $value
* @param string $createdAt
*
* @return void
*/
public function __construct(MetricPoint $point, Metric $metric, $value, $createdAt)
{
$this->point = $point;
$this->metric = $metric;
$this->value = $value;
$this->createdAt = $createdAt;
}
}

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\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
class MetricPointWasUpdatedEvent
{
/**
* The metric point that was updated.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $point;
/**
* Create a new metric point was updated event instance.
*
* @return void
*/
public function __construct(MetricPoint $point)
{
$this->point = $point;
}
}

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\Metric;
use CachetHQ\Cachet\Models\Metric;
class MetricWasUpdatedEvent
{
/**
* The metric that was updated.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
public $metric;
/**
* Create a new metric was updated event instance.
*
* @return void
*/
public function __construct(Metric $metric)
{
$this->metric = $metric;
}
}

View File

@ -0,0 +1,57 @@
<?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\Metric;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand;
use CachetHQ\Cachet\Events\Metric\MetricWasUpdatedEvent;
use CachetHQ\Cachet\Models\Metric;
class UpdateMetricCommandHandler
{
/**
* Handle the update metric command.
*
* @param \CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand $command
*
* @return \CachetHQ\Cachet\Models\Metric
*/
public function handle(UpdateMetricCommand $command)
{
$metric = $command->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,
]);
}
}

View File

@ -0,0 +1,49 @@
<?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\Metric;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand;
use CachetHQ\Cachet\Events\Metric\MetricPointWasUpdatedEvent;
use CachetHQ\Cachet\Models\MetricPoint;
use Carbon\Carbon;
class UpdateMetricPointCommandHandler
{
/**
* Handle the update metric point command.
*
* @param \CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand $command
*
* @return \CachetHQ\Cachet\Models\MetricPoint
*/
public function handle(UpdateMetricPointCommand $command)
{
$point = $command->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;
}
}

View File

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

View File

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

View File

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