From 085552607a3e34af1e70231835f6b6bd99c50a27 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 2 Jul 2018 18:05:12 +0100 Subject: [PATCH] Move calculated_value presenter method into MetricPoint getter --- app/Models/MetricPoint.php | 19 +++++++++++++++++++ app/Presenters/MetricPointPresenter.php | 15 ++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/Models/MetricPoint.php b/app/Models/MetricPoint.php index 34125a2f..dd70b0f7 100644 --- a/app/Models/MetricPoint.php +++ b/app/Models/MetricPoint.php @@ -29,6 +29,15 @@ class MetricPoint extends Model implements HasPresenter { use ValidatingTrait; + /** + * The accessors to append to the model's array form. + * + * @var string[] + */ + protected $appends = [ + 'calculated_value', + ]; + /** * The model's attributes. * @@ -81,6 +90,16 @@ class MetricPoint extends Model implements HasPresenter return $this->belongsTo(Metric::class); } + /** + * Show the actual calculated value; as per (value * counter). + * + * @return int + */ + public function getCalculatedValueAttribute() + { + return $this->value * $this->counter; + } + /** * Round the created at value into intervals of 30 seconds. * diff --git a/app/Presenters/MetricPointPresenter.php b/app/Presenters/MetricPointPresenter.php index 388ef816..5cbfdd60 100644 --- a/app/Presenters/MetricPointPresenter.php +++ b/app/Presenters/MetricPointPresenter.php @@ -19,16 +19,6 @@ class MetricPointPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; - /** - * Show the actual calculated value; as per (value * counter). - * - * @return int - */ - public function calculated_value() - { - return $this->wrappedObject->value * $this->wrappedObject->counter; - } - /** * Convert the presenter instance to an array. * @@ -37,9 +27,8 @@ class MetricPointPresenter extends BasePresenter implements Arrayable public function toArray() { return array_merge($this->wrappedObject->toArray(), [ - 'created_at' => $this->created_at(), - 'updated_at' => $this->updated_at(), - 'calculated_value' => $this->calculated_value(), + 'created_at' => $this->created_at(), + 'updated_at' => $this->updated_at(), ]); } }