From 9581c5a394b18b6cf1407e9b2918c417e88bf623 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 15 Aug 2015 22:15:41 -0500 Subject: [PATCH] Add and remove metrics and metric points commands --- ...MetricCommand.php => AddMetricCommand.php} | 47 +++++++++++----- app/Commands/Metric/AddMetricPointCommand.php | 54 +++++++++++++++++++ app/Commands/Metric/RemoveMetricCommand.php | 36 +++++++++++++ .../Metric/RemoveMetricPointCommand.php | 36 +++++++++++++ .../Metric/MetricPointWasAddedEvent.php | 34 ++++++++++++ .../Metric/MetricPointWasRemovedEvent.php | 34 ++++++++++++ app/Events/Metric/MetricWasAddedEvent.php | 34 ++++++++++++ app/Events/Metric/MetricWasRemovedEvent.php | 34 ++++++++++++ .../Metric/AddMetricCommandHandler.php | 43 +++++++++++++++ .../Metric/AddMetricPointCommandHandler.php | 48 +++++++++++++++++ .../Metric/RemoveMetricCommandHandler.php | 35 ++++++++++++ .../RemoveMetricPointCommandHandler.php | 35 ++++++++++++ app/Http/Controllers/Api/MetricController.php | 17 +++++- .../Controllers/Api/MetricPointController.php | 18 +++---- .../Dashboard/MetricController.php | 9 +++- tests/Api/MetricTest.php | 1 + 16 files changed, 489 insertions(+), 26 deletions(-) rename app/Commands/Metric/{AddNewMetricCommand.php => AddMetricCommand.php} (52%) create mode 100644 app/Commands/Metric/AddMetricPointCommand.php create mode 100644 app/Commands/Metric/RemoveMetricCommand.php create mode 100644 app/Commands/Metric/RemoveMetricPointCommand.php create mode 100644 app/Events/Metric/MetricPointWasAddedEvent.php create mode 100644 app/Events/Metric/MetricPointWasRemovedEvent.php create mode 100644 app/Events/Metric/MetricWasAddedEvent.php create mode 100644 app/Events/Metric/MetricWasRemovedEvent.php create mode 100644 app/Handlers/Commands/Metric/AddMetricCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/RemoveMetricCommandHandler.php create mode 100644 app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php diff --git a/app/Commands/Metric/AddNewMetricCommand.php b/app/Commands/Metric/AddMetricCommand.php similarity index 52% rename from app/Commands/Metric/AddNewMetricCommand.php rename to app/Commands/Metric/AddMetricCommand.php index 5ebadbaa3..b65072cec 100644 --- a/app/Commands/Metric/AddNewMetricCommand.php +++ b/app/Commands/Metric/AddMetricCommand.php @@ -11,7 +11,7 @@ namespace CachetHQ\Cachet\Commands\Metric; -class AddNewMetricCommand +class AddMetricCommand { /** * The metric name. @@ -39,40 +39,63 @@ class AddNewMetricCommand * * @var float */ - public $default; + public $default_value; /** - * The metric calc type. + * The metric calculation type. * * @var int */ - public $type; + public $calc_type; /** * The metric display chart. * * @var int */ - public $chart; + public $display_chart; /** - * Create a new add team member command instance. + * The metric decimal places. + * + * @var int + */ + public $places; + + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'name' => '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 - * @param int $type - * @param int $chart + * @param float $default_value + * @param int $calc_type + * @param int $display_chart + * @param int $places * * @return void */ - public function __construct($name, $suffix, $description, $default, $type, $chart) + public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places) { $this->name = $name; $this->suffix = $suffix; $this->description = $description; - $this->default = $default; - $this->chart = $chart; + $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 000000000..e0b676b81 --- /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 000000000..a893551f4 --- /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 000000000..c305b2383 --- /dev/null +++ b/app/Commands/Metric/RemoveMetricPointCommand.php @@ -0,0 +1,36 @@ +metricPoint = $metricPoint; + } +} diff --git a/app/Events/Metric/MetricPointWasAddedEvent.php b/app/Events/Metric/MetricPointWasAddedEvent.php new file mode 100644 index 000000000..e5b47b77b --- /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 000000000..6315749b0 --- /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 000000000..b579e7ad8 --- /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 000000000..41c5c63e0 --- /dev/null +++ b/app/Events/Metric/MetricWasRemovedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Handlers/Commands/Metric/AddMetricCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricCommandHandler.php new file mode 100644 index 000000000..1a3299847 --- /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 000000000..c690f1cbf --- /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 000000000..dd27fbaf5 --- /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 000000000..a6967abf6 --- /dev/null +++ b/app/Handlers/Commands/Metric/RemoveMetricPointCommandHandler.php @@ -0,0 +1,35 @@ +metricPoint; + + event(new MetricPointWasRemovedEvent($metricPoint)); + + $metricPoint->delete(); + } +} diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index 140b2735d..cbf5c4671 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 d10a210d8..dfe7f225f 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/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index 21b6f7853..4fe2790ec 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/tests/Api/MetricTest.php b/tests/Api/MetricTest.php index bf6960aed..eafab7a26 100644 --- a/tests/Api/MetricTest.php +++ b/tests/Api/MetricTest.php @@ -59,6 +59,7 @@ class MetricTest extends AbstractTestCase 'description' => 'Lorem ipsum dolor', 'default_value' => 1, 'display_chart' => 1, + 'places' => 0, ]); $this->seeJson(['name' => 'Foo']); $this->assertResponseOk();