diff --git a/app/Config/Repository.php b/app/Config/Repository.php index 29eab653..785f9b7d 100644 --- a/app/Config/Repository.php +++ b/app/Config/Repository.php @@ -44,9 +44,9 @@ class Repository /** * Returns a setting from the database. * - * @param string $name - * @param string $default - * @param bool $checkEnv + * @param string $name + * @param string|null $default + * @param bool $checkEnv * * @return string|null */ diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index a6fe8c6c..e7b46c94 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Models\Tag; use CachetHQ\Cachet\Repositories\Component\ComponentRepository; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Http\Request; class ComponentController extends AbstractApiController @@ -58,18 +59,20 @@ class ComponentController extends AbstractApiController */ public function getComponent($id) { - return $this->component->findOrFail($id); + return $this->item($this->component->findOrFail($id)); } /** * Create a new component. * + * @param \Illuminate\Contracts\Auth\Guard $auth + * * @return \CachetHQ\Cachet\Models\Component */ - public function postComponents() + public function postComponents(Guard $auth) { $component = $this->component->create( - $this->auth->user()->id, + $auth->user()->id, Binput::except('tags') ); @@ -87,7 +90,7 @@ class ComponentController extends AbstractApiController $component->tags()->sync($componentTags); } - return $component; + return $this->item($component); } /** @@ -114,7 +117,7 @@ class ComponentController extends AbstractApiController $component->tags()->sync($componentTags); } - return $component; + return $this->item($component); } /** diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index ac6f5b2c..b06fd085 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -15,6 +15,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Repositories\Incident\IncidentRepository; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Http\Request; class IncidentController extends AbstractApiController @@ -57,17 +58,19 @@ class IncidentController extends AbstractApiController */ public function getIncident($id) { - return $this->incident->findOrFail($id); + return $this->item($this->incident->findOrFail($id)); } /** * Create a new incident. * + * @param \Illuminate\Contracts\Auth\Guard $auth + * * @return \CachetHQ\Cachet\Models\Incident */ - public function postIncidents() + public function postIncidents(Guard $auth) { - return $this->incident->create($this->auth->user()->id, Binput::all()); + return $this->item($this->incident->create($auth->user()->id, Binput::all())); } /** @@ -79,7 +82,7 @@ class IncidentController extends AbstractApiController */ public function putIncident($id) { - return $this->incident->update($id, Binput::all()); + return $this->item($this->incident->update($id, Binput::all())); } /** diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index c7e3119e..b6058594 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -79,7 +79,7 @@ class MetricController extends AbstractApiController */ public function postMetrics() { - return $this->metric->create(Binput::all()); + return $this->item($this->metric->create(Binput::all())); } /** @@ -91,7 +91,7 @@ class MetricController extends AbstractApiController */ public function putMetric($id) { - return $this->metric->update($id, Binput::all()); + return $this->item($this->metric->update($id, Binput::all())); } /** diff --git a/app/Http/Controllers/Api/MetricPointController.php b/app/Http/Controllers/Api/MetricPointController.php index 12f2b0d3..0c579dc4 100644 --- a/app/Http/Controllers/Api/MetricPointController.php +++ b/app/Http/Controllers/Api/MetricPointController.php @@ -44,7 +44,7 @@ class MetricPointController extends AbstractApiController */ public function getMetricPoints($id) { - return $this->metricPoint->findOrFail($id); + return $this->item($this->metricPoint->findOrFail($id)); } /** @@ -56,7 +56,7 @@ class MetricPointController extends AbstractApiController */ public function postMetricPoints($id) { - return $this->metricPoint->create($id, Binput::all()); + return $this->item($this->metricPoint->create($id, Binput::all())); } /** @@ -72,7 +72,7 @@ class MetricPointController extends AbstractApiController $metricPoint = $this->metricPoint->findOrFail($pointId); $metricPoint->update(Binput::all()); - return $metricPoint; + return $this->item($metricPoint); } /** diff --git a/app/Models/Component.php b/app/Models/Component.php index bdbf9dad..e5a8cd8c 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Models; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use McCool\LaravelAutoPresenter\HasPresenter; use Watson\Validating\ValidatingTrait; /** @@ -31,7 +32,7 @@ use Watson\Validating\ValidatingTrait; * @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $deleted_at */ -class Component extends Model +class Component extends Model implements HasPresenter { use SoftDeletes, ValidatingTrait; @@ -161,4 +162,14 @@ class Component extends Model return implode(', ', $tags->toArray()); } + + /** + * Get the presenter class. + * + * @return string + */ + public function getPresenterClass() + { + return 'CachetHQ\Cachet\Presenters\ComponentPresenter'; + } } diff --git a/app/Presenters/AbstractPresenter.php b/app/Presenters/AbstractPresenter.php new file mode 100644 index 00000000..01786014 --- /dev/null +++ b/app/Presenters/AbstractPresenter.php @@ -0,0 +1,40 @@ + + * (c) Joseph Cohen + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CachetHQ\Cachet\Presenters; + +use CachetHQ\Cachet\Facades\Setting; +use Illuminate\Contracts\Support\Arrayable; +use McCool\LaravelAutoPresenter\BasePresenter as BaseLaravelAutoPresenter; + +abstract class AbstractPresenter extends BaseLaravelAutoPresenter implements Arrayable +{ + /** + * The setting repository. + * + * @var \CachetHQ\Cachet\Config\Repository + */ + protected $setting; + + /** + * Create a incident presenter instance. + * + * @param object $resource + */ + public function __construct($resource) + { + parent::__construct($resource); + + $this->setting = app('setting'); + } +} diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php new file mode 100644 index 00000000..96bb31b4 --- /dev/null +++ b/app/Presenters/ComponentPresenter.php @@ -0,0 +1,34 @@ + + * (c) Joseph Cohen + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CachetHQ\Cachet\Presenters; + +use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; + +class ComponentPresenter extends AbstractPresenter +{ + use TimestampsTrait; + + /** + * Convert the presenter instance to an array. + * + * @return string[] + */ + public function toArray() + { + return array_merge($this->wrappedObject->toArray(), [ + 'created_at' => $this->created_at(), + 'updated_at' => $this->updated_at(), + ]); + } +} diff --git a/app/Presenters/IncidentPresenter.php b/app/Presenters/IncidentPresenter.php index b995210d..5238f028 100644 --- a/app/Presenters/IncidentPresenter.php +++ b/app/Presenters/IncidentPresenter.php @@ -14,32 +14,13 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Facades\Setting; -use CachetHQ\Cachet\Models\Incident; +use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; use GrahamCampbell\Markdown\Facades\Markdown; use Jenssegers\Date\Date; -use McCool\LaravelAutoPresenter\BasePresenter; -class IncidentPresenter extends BasePresenter +class IncidentPresenter extends AbstractPresenter { - /** - * Time zone setting. - * - * @var string - */ - protected $tz; - - /** - * Create a incident presenter instance. - * - * @param object $resource - */ - public function __construct($resource) - { - parent::__construct($resource); - - $this->tz = Setting::get('app_timezone'); - $this->format = Setting::get('incident_date_format') ?: 'l jS F Y H:i:s'; - } + use TimestampsTrait; /** * Renders the message from Markdown into HTML. @@ -59,7 +40,7 @@ class IncidentPresenter extends BasePresenter public function created_at_diff() { return (new Date($this->wrappedObject->created_at)) - ->setTimezone($this->tz) + ->setTimezone($this->setting->get('app_timezone')) ->diffForHumans(); } @@ -71,8 +52,8 @@ class IncidentPresenter extends BasePresenter public function created_at_formatted() { return ucfirst((new Date($this->wrappedObject->created_at)) - ->setTimezone($this->tz) - ->format($this->format)); + ->setTimezone($this->setting->get('app_timezone')) + ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -82,7 +63,7 @@ class IncidentPresenter extends BasePresenter */ public function created_at_datetimepicker() { - return $this->wrappedObject->created_at->setTimezone($this->tz)->format('d/m/Y H:i'); + return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i'); } /** @@ -92,7 +73,18 @@ class IncidentPresenter extends BasePresenter */ public function created_at_iso() { - return $this->wrappedObject->created_at->setTimezone($this->tz)->toISO8601String(); + return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String(); + } + + /** + * Present formatted date time. + * + * @return string + */ + public function scheduled_at() + { + return (new Date($this->wrappedObject->scheduled_at)) + ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); } /** @@ -103,7 +95,7 @@ class IncidentPresenter extends BasePresenter public function scheduled_at_diff() { return (new Date($this->wrappedObject->scheduled_at)) - ->setTimezone($this->tz) + ->setTimezone($this->setting->get('app_timezone')) ->diffForHumans(); } @@ -115,8 +107,8 @@ class IncidentPresenter extends BasePresenter public function scheduled_at_formatted() { return ucfirst((new Date($this->wrappedObject->scheduled_at)) - ->setTimezone($this->tz) - ->format($this->format)); + ->setTimezone($this->setting->get('app_timezone')) + ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -126,7 +118,7 @@ class IncidentPresenter extends BasePresenter */ public function scheduled_at_iso() { - return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->toISO8601String(); + return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String(); } /** @@ -136,7 +128,7 @@ class IncidentPresenter extends BasePresenter */ public function scheduled_at_datetimepicker() { - return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->format('d/m/Y H:i'); + return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i'); } /** @@ -161,4 +153,18 @@ class IncidentPresenter extends BasePresenter return ''; } } + + /** + * Convert the presenter instance to an array. + * + * @return string[] + */ + public function toArray() + { + return array_merge($this->wrappedObject->toArray(), [ + 'scheduled_at' => $this->created_at(), + 'created_at' => $this->created_at(), + 'updated_at' => $this->updated_at(), + ]); + } } diff --git a/app/Presenters/MetricPointPresenter.php b/app/Presenters/MetricPointPresenter.php index 0f5b4e19..5bf232b0 100644 --- a/app/Presenters/MetricPointPresenter.php +++ b/app/Presenters/MetricPointPresenter.php @@ -4,6 +4,8 @@ * This file is part of Cachet. * * (c) James Brooks + * (c) Joseph Cohen + * (c) Graham Campbell * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -11,58 +13,16 @@ namespace CachetHQ\Cachet\Presenters; -use CachetHQ\Cachet\Facades\Setting; -use Illuminate\Contracts\Support\Arrayable; -use Jenssegers\Date\Date; -use McCool\LaravelAutoPresenter\BasePresenter; +use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; -class MetricPointPresenter extends BasePresenter implements Arrayable +class MetricPointPresenter extends AbstractPresenter { - /** - * Time zone setting. - * - * @var string - */ - protected $tz; - - /** - * Create a incident presenter instance. - * - * @param object $resource - */ - public function __construct($resource) - { - parent::__construct($resource); - - $this->tz = Setting::get('app_timezone'); - } - - /** - * Present formatted date time. - * - * @return string - */ - public function created_at() - { - return (new Date($this->wrappedObject->created_at)) - ->setTimezone($this->tz)->toDateTimeString(); - } - - /** - * Present formatted date time. - * - * @return string - */ - public function updated_at() - { - return (new Date($this->wrappedObject->updated_at)) - ->setTimezone($this->tz)->toDateTimeString(); - } + use TimestampsTrait; /** * Convert the presenter instance to an array. * - * @return array + * @return string[] */ public function toArray() { diff --git a/app/Presenters/MetricPresenter.php b/app/Presenters/MetricPresenter.php index 122ca0f6..d117cff1 100644 --- a/app/Presenters/MetricPresenter.php +++ b/app/Presenters/MetricPresenter.php @@ -4,6 +4,8 @@ * This file is part of Cachet. * * (c) James Brooks + * (c) Joseph Cohen + * (c) Graham Campbell * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -11,58 +13,16 @@ namespace CachetHQ\Cachet\Presenters; -use CachetHQ\Cachet\Facades\Setting; -use Illuminate\Contracts\Support\Arrayable; -use Jenssegers\Date\Date; -use McCool\LaravelAutoPresenter\BasePresenter; +use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; -class MetricPresenter extends BasePresenter implements Arrayable +class MetricPresenter extends AbstractPresenter { - /** - * Time zone setting. - * - * @var string - */ - protected $tz; - - /** - * Create a incident presenter instance. - * - * @param object $resource - */ - public function __construct($resource) - { - parent::__construct($resource); - - $this->tz = Setting::get('app_timezone'); - } - - /** - * Present formatted date time. - * - * @return string - */ - public function created_at() - { - return (new Date($this->wrappedObject->created_at)) - ->setTimezone($this->tz)->toDateTimeString(); - } - - /** - * Present formatted date time. - * - * @return string - */ - public function updated_at() - { - return (new Date($this->wrappedObject->updated_at)) - ->setTimezone($this->tz)->toDateTimeString(); - } + use TimestampsTrait; /** * Convert the presenter instance to an array. * - * @return array + * @return string[] */ public function toArray() { diff --git a/app/Presenters/Traits/TimestampsTrait.php b/app/Presenters/Traits/TimestampsTrait.php new file mode 100644 index 00000000..bf6833d6 --- /dev/null +++ b/app/Presenters/Traits/TimestampsTrait.php @@ -0,0 +1,52 @@ + + * (c) Joseph Cohen + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CachetHQ\Cachet\Presenters\Traits; + +use Jenssegers\Date\Date; + +trait TimestampsTrait +{ + /** + * Present formatted date time. + * + * @return string + */ + public function created_at() + { + return (new Date($this->wrappedObject->created_at)) + ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + } + + /** + * Present formatted date time. + * + * @return string + */ + public function updated_at() + { + return (new Date($this->wrappedObject->updated_at)) + ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + } + + /** + * Present formatted date time. + * + * @return string + */ + public function deleted_at() + { + return (new Date($this->wrappedObject->deleted_at)) + ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + } +}