From 4923a59b7671616c957276788627176652141cd1 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Feb 2016 20:35:31 +0000 Subject: [PATCH 1/3] Working on datetime mess --- app/Dates/DateFactory.php | 14 +++++++++++ app/Presenters/IncidentPresenter.php | 29 ++++++++--------------- app/Presenters/SubscriberPresenter.php | 6 ++--- app/Presenters/Traits/TimestampsTrait.php | 12 ++++------ 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/app/Dates/DateFactory.php b/app/Dates/DateFactory.php index 588016ef..47b6240d 100644 --- a/app/Dates/DateFactory.php +++ b/app/Dates/DateFactory.php @@ -74,4 +74,18 @@ class DateFactory { return Date::createFromFormat($format, $time)->setTimezone($this->appTimezone); } + + /** + * Make a Carbon instance from a string. + * + * @param string $time + * + * @throws \InvalidArgumentException + * + * @return \Carbon\Carbon + */ + public function make($time) + { + return (new Date($time))->setTimezone($this->cachetTimezone); + } } diff --git a/app/Presenters/IncidentPresenter.php b/app/Presenters/IncidentPresenter.php index 05ae6dee..1db4e35b 100644 --- a/app/Presenters/IncidentPresenter.php +++ b/app/Presenters/IncidentPresenter.php @@ -11,11 +11,11 @@ namespace CachetHQ\Cachet\Presenters; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; use GrahamCampbell\Markdown\Facades\Markdown; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; use McCool\LaravelAutoPresenter\BasePresenter; class IncidentPresenter extends BasePresenter implements Arrayable @@ -39,9 +39,7 @@ class IncidentPresenter extends BasePresenter implements Arrayable */ public function created_at_diff() { - return (new Date($this->wrappedObject->created_at)) - ->setTimezone(Config::get('cachet.timezone')) - ->diffForHumans(); + return app(DateFactory::class)->make($this->wrappedObject->created_at)->diffForHumans(); } /** @@ -51,9 +49,7 @@ class IncidentPresenter extends BasePresenter implements Arrayable */ public function created_at_formatted() { - return ucfirst((new Date($this->wrappedObject->created_at)) - ->setTimezone(Config::get('cachet.timezone')) - ->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); + return ucfirst(app(DateFactory::class)->make($this->wrappedObject->created_at)->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -83,8 +79,7 @@ class IncidentPresenter extends BasePresenter implements Arrayable */ public function scheduled_at() { - return (new Date($this->wrappedObject->scheduled_at)) - ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); + return app(DateFactory::class)->make($this->wrappedObject->scheduled_at)->toDateTimeString(); } /** @@ -94,9 +89,7 @@ class IncidentPresenter extends BasePresenter implements Arrayable */ public function scheduled_at_diff() { - return (new Date($this->wrappedObject->scheduled_at)) - ->setTimezone(Config::get('cachet.timezone')) - ->diffForHumans(); + return app(DateFactory::class)->make($this->wrappedObject->scheduled_at)->diffForHumans(); } /** @@ -106,9 +99,7 @@ class IncidentPresenter extends BasePresenter implements Arrayable */ public function scheduled_at_formatted() { - return ucfirst((new Date($this->wrappedObject->scheduled_at)) - ->setTimezone(Config::get('cachet.timezone')) - ->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); + return ucfirst(app(DateFactory::class)->make($this->wrappedObject->created_at)->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -140,9 +131,9 @@ class IncidentPresenter extends BasePresenter implements Arrayable { if ($this->wrappedObject->is_scheduled) { return $this->scheduled_at_formatted; - } else { - return $this->created_at_formatted; } + + return $this->created_at_formatted; } /** @@ -154,9 +145,9 @@ class IncidentPresenter extends BasePresenter implements Arrayable { if ($this->wrappedObject->is_scheduled) { return $this->scheduled_at_iso; - } else { - return $this->created_at_iso; } + + return $this->created_at_iso; } /** diff --git a/app/Presenters/SubscriberPresenter.php b/app/Presenters/SubscriberPresenter.php index 87b3be4e..adf970b5 100644 --- a/app/Presenters/SubscriberPresenter.php +++ b/app/Presenters/SubscriberPresenter.php @@ -11,10 +11,9 @@ namespace CachetHQ\Cachet\Presenters; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; use McCool\LaravelAutoPresenter\BasePresenter; class SubscriberPresenter extends BasePresenter implements Arrayable @@ -28,8 +27,7 @@ class SubscriberPresenter extends BasePresenter implements Arrayable */ public function verified_at() { - return (new Date($this->wrappedObject->verified_at)) - ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); + return app(DateFactory::class)->make($this->wrappedObject->verified_at)->toDateTimeString(); } /** diff --git a/app/Presenters/Traits/TimestampsTrait.php b/app/Presenters/Traits/TimestampsTrait.php index 655e37ba..eba58b2a 100644 --- a/app/Presenters/Traits/TimestampsTrait.php +++ b/app/Presenters/Traits/TimestampsTrait.php @@ -11,8 +11,7 @@ namespace CachetHQ\Cachet\Presenters\Traits; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; +use CachetHQ\Cachet\Dates\DateFactory; trait TimestampsTrait { @@ -23,8 +22,7 @@ trait TimestampsTrait */ public function created_at() { - return (new Date($this->wrappedObject->created_at)) - ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); + return app(DateFactory::class)->make($this->wrappedObject->created_at)->toDateTimeString(); } /** @@ -34,8 +32,7 @@ trait TimestampsTrait */ public function updated_at() { - return (new Date($this->wrappedObject->updated_at)) - ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); + return app(DateFactory::class)->make($this->wrappedObject->updated_at)->toDateTimeString(); } /** @@ -45,7 +42,6 @@ trait TimestampsTrait */ public function deleted_at() { - return (new Date($this->wrappedObject->deleted_at)) - ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); + return app(DateFactory::class)->make($this->wrappedObject->deleted_at)->toDateTimeString(); } } From 921116a198865b909805a3fba38699c7cd18b102 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Feb 2016 20:46:46 +0000 Subject: [PATCH 2/3] More fixes --- app/Dates/DateFactory.php | 4 +-- app/Http/Controllers/StatusPageController.php | 15 +++------ app/Http/Middleware/Admin.php | 2 ++ app/Http/Middleware/Timezone.php | 22 ++++++++++++- app/Repositories/Metric/MetricRepository.php | 33 ++++++++++++------- 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/app/Dates/DateFactory.php b/app/Dates/DateFactory.php index 47b6240d..d2e0ca45 100644 --- a/app/Dates/DateFactory.php +++ b/app/Dates/DateFactory.php @@ -78,13 +78,13 @@ class DateFactory /** * Make a Carbon instance from a string. * - * @param string $time + * @param string|null $time * * @throws \InvalidArgumentException * * @return \Carbon\Carbon */ - public function make($time) + public function make($time = null) { return (new Date($time))->setTimezone($this->cachetTimezone); } diff --git a/app/Http/Controllers/StatusPageController.php b/app/Http/Controllers/StatusPageController.php index e83e0d78..24b398bd 100644 --- a/app/Http/Controllers/StatusPageController.php +++ b/app/Http/Controllers/StatusPageController.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Http\Controllers; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -54,27 +55,19 @@ class StatusPageController extends Controller } else { $incidentDays = range(0, $daysToShow); } - $dateTimeZone = Config::get('cachet.timezone'); $incidentVisibility = Auth::check() ? 0 : 1; $allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisibility)->whereBetween('created_at', [ $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', $startDate->format('Y-m-d').' 23:59:59', - ])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) use ($dateTimeZone) { - // If it's scheduled, get the scheduled at date. - if ($incident->is_scheduled) { - return (new Date($incident->scheduled_at)) - ->setTimezone($dateTimeZone)->toDateString(); - } - - return (new Date($incident->created_at)) - ->setTimezone($dateTimeZone)->toDateString(); + ])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) { + return app(DateFactory::class)->make($incident->is_scheduled ? $incident->scheduled_at : $incident->created_at)->toDateString(); }); // Add in days that have no incidents foreach ($incidentDays as $i) { - $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); + $date = app(DateFactory::class)->make($startDate)->subDays($i); if (!isset($allIncidents[$date->toDateString()])) { $allIncidents[$date->toDateString()] = []; diff --git a/app/Http/Middleware/Admin.php b/app/Http/Middleware/Admin.php index 312792a7..b0773765 100644 --- a/app/Http/Middleware/Admin.php +++ b/app/Http/Middleware/Admin.php @@ -28,6 +28,8 @@ class Admin * Create a new admin middleware instance. * * @param \Illuminate\Contracts\Auth\Guard $auth + * + * @return void */ public function __construct(Guard $auth) { diff --git a/app/Http/Middleware/Timezone.php b/app/Http/Middleware/Timezone.php index 00684209..dfe4f560 100644 --- a/app/Http/Middleware/Timezone.php +++ b/app/Http/Middleware/Timezone.php @@ -12,9 +12,29 @@ namespace CachetHQ\Cachet\Http\Middleware; use Closure; +use Illuminate\Contracts\Config\Repository; class Timezone { + /** + * Config repository. + * + * @var \Illuminate\Contracts\Config\Repository + */ + protected $config; + + /** + * Creates a new release instance. + * + * @param \Illuminate\Contracts\Config\Repository $config + * + * @return void + */ + public function __construct(Repository $config) + { + $this->config = $config; + } + /** * Handle an incoming request. * @@ -26,7 +46,7 @@ class Timezone public function handle($request, Closure $next) { if ($tz = $request->header('Time-Zone')) { - app('config')->set('cachet.timezone', $tz); + $this->config->set('cachet.timezone', $tz); } return $next($request); diff --git a/app/Repositories/Metric/MetricRepository.php b/app/Repositories/Metric/MetricRepository.php index 7fa24e40..b91e15c9 100644 --- a/app/Repositories/Metric/MetricRepository.php +++ b/app/Repositories/Metric/MetricRepository.php @@ -11,10 +11,9 @@ namespace CachetHQ\Cachet\Repositories\Metric; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Models\Metric; use DateInterval; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; class MetricRepository { @@ -26,21 +25,24 @@ class MetricRepository protected $repository; /** - * The timezone the status page is showing in. + * The date factory instance. * - * @var string + * @var \CachetHQ\Cachet\Dates\DateFactory */ - protected $dateTimeZone; + protected $dates; /** * Create a new metric repository class. * * @param \CachetHQ\Cachet\Repositories\Metric\MetricInterface $repository + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void */ - public function __construct(MetricInterface $repository) + public function __construct(MetricInterface $repository, DateFactory $dates) { $this->repository = $repository; - $this->dateTimeZone = Config::get('cachet.timezone'); + $this->dates = $dates; } /** @@ -52,10 +54,12 @@ class MetricRepository */ public function listPointsLastHour(Metric $metric) { - $dateTime = (new Date())->setTimezone($this->dateTimeZone); + $dateTime = $this->dates->make(); + $points = []; $pointKey = $dateTime->format('H:i'); + for ($i = 0; $i <= 60; $i++) { $points[$pointKey] = $this->repository->getPointsLastHour($metric, 0, $i); $pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('H:i'); @@ -74,10 +78,12 @@ class MetricRepository */ public function listPointsToday(Metric $metric, $hours = 12) { - $dateTime = (new Date())->setTimezone($this->dateTimeZone); + $dateTime = $this->dates->make(); + $points = []; $pointKey = $dateTime->format('H:00'); + for ($i = 0; $i <= $hours; $i++) { $points[$pointKey] = $this->repository->getPointsByHour($metric, $i); $pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00'); @@ -95,10 +101,12 @@ class MetricRepository */ public function listPointsForWeek(Metric $metric) { - $dateTime = (new Date())->setTimezone($this->dateTimeZone); + $dateTime = $this->dates->make(); + $points = []; $pointKey = $dateTime->format('jS M'); + for ($i = 0; $i <= 7; $i++) { $points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i); $pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M'); @@ -116,11 +124,14 @@ class MetricRepository */ public function listPointsForMonth(Metric $metric) { - $dateTime = (new Date())->setTimezone($this->dateTimeZone); + $dateTime = $this->dates->make(); + $daysInMonth = $dateTime->format('t'); + $points = []; $pointKey = $dateTime->format('jS M'); + for ($i = 0; $i <= $daysInMonth; $i++) { $points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i); $pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M'); From 9edba5aaccbc41f036f4e7ae049a812914bb3d07 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Feb 2016 20:49:00 +0000 Subject: [PATCH 3/3] Minor fixes --- app/GitHub/Release.php | 4 ++-- app/Http/Middleware/Localize.php | 2 +- app/Http/Middleware/Timezone.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/GitHub/Release.php b/app/GitHub/Release.php index 74d8971a..6fe0de8d 100644 --- a/app/GitHub/Release.php +++ b/app/GitHub/Release.php @@ -18,14 +18,14 @@ use Illuminate\Contracts\Config\Repository as ConfigRepository; class Release { /** - * Cache instance. + * The cache repository instance. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** - * Config repository. + * The config repository instance. * * @var \Illuminate\Contracts\Config\Repository */ diff --git a/app/Http/Middleware/Localize.php b/app/Http/Middleware/Localize.php index cf04cdeb..f97c8362 100644 --- a/app/Http/Middleware/Localize.php +++ b/app/Http/Middleware/Localize.php @@ -25,7 +25,7 @@ class Localize protected $langs; /** - * Config repository. + * The config repository instance. * * @var \Illuminate\Config\Repository */ diff --git a/app/Http/Middleware/Timezone.php b/app/Http/Middleware/Timezone.php index dfe4f560..bca54111 100644 --- a/app/Http/Middleware/Timezone.php +++ b/app/Http/Middleware/Timezone.php @@ -17,7 +17,7 @@ use Illuminate\Contracts\Config\Repository; class Timezone { /** - * Config repository. + * The config repository instance. * * @var \Illuminate\Contracts\Config\Repository */