77
app/Dates/DateFactory.php
Normal file
77
app/Dates/DateFactory.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?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\Dates;
|
||||
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class DateFactory
|
||||
{
|
||||
/**
|
||||
* The application timezone.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $appTimezone;
|
||||
|
||||
/**
|
||||
* The cachet timezone.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $cachetTimezone;
|
||||
|
||||
/**
|
||||
* Create a new date factory instance.
|
||||
*
|
||||
* @param string $appTimezone
|
||||
* @param string $cachetTimezone
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($appTimezone, $cachetTimezone)
|
||||
{
|
||||
$this->appTimezone = $appTimezone;
|
||||
$this->cachetTimezone = $cachetTimezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Carbon instance from a specific format.
|
||||
*
|
||||
* @param string $format
|
||||
* @param string $time
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return \Carbon\Carbon
|
||||
*/
|
||||
public function create($format, $time)
|
||||
{
|
||||
return Date::createFromFormat($format, $time, $this->cachetTimezone)->setTimezone($this->appTimezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Carbon instance from a specific format.
|
||||
*
|
||||
* We're also going to make sure the timezone information is correct.
|
||||
*
|
||||
* @param string $format
|
||||
* @param string $time
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return \Carbon\Carbon
|
||||
*/
|
||||
public function createNormalized($format, $time)
|
||||
{
|
||||
return $this->createFromFormat($format, $time)->setTimezone($this->appTimezone);
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,32 @@
|
||||
namespace CachetHQ\Cachet\Handlers\Commands\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class ReportIncidentCommandHandler
|
||||
{
|
||||
/**
|
||||
* The date factory instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Dates\DateFactory
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* Create a new report incident command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the report incident command.
|
||||
*
|
||||
@@ -43,7 +61,7 @@ class ReportIncidentCommandHandler
|
||||
|
||||
// The incident occurred at a different time.
|
||||
if ($command->incident_date) {
|
||||
$incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, config('cachet.timezone'))->setTimezone(Config::get('app.timezone'));
|
||||
$incidentDate = $this->dates->createNormalized('d/m/Y H:i', $command->incident_date);
|
||||
|
||||
$data['created_at'] = $incidentDate;
|
||||
$data['updated_at'] = $incidentDate;
|
||||
|
||||
@@ -12,13 +12,31 @@
|
||||
namespace CachetHQ\Cachet\Handlers\Commands\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class ReportMaintenanceCommandHandler
|
||||
{
|
||||
/**
|
||||
* The date factory instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Dates\DateFactory
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* Create a new report maintanance command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the report maintenance command.
|
||||
*
|
||||
@@ -28,8 +46,7 @@ class ReportMaintenanceCommandHandler
|
||||
*/
|
||||
public function handle(ReportMaintenanceCommand $command)
|
||||
{
|
||||
$scheduledAt = Date::createFromFormat('d/m/Y H:i', $command->timestamp, config('cachet.timezone'))
|
||||
->setTimezone(Config::get('app.timezone'));
|
||||
$scheduledAt = $this->dates->createNormalized('d/m/Y H:i', $command->timestamp);
|
||||
|
||||
$maintenanceEvent = Incident::create([
|
||||
'name' => $command->name,
|
||||
|
||||
@@ -12,14 +12,32 @@
|
||||
namespace CachetHQ\Cachet\Handlers\Commands\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Events\Incident\IncidentWasUpdatedEvent;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class UpdateIncidentCommandHandler
|
||||
{
|
||||
/**
|
||||
* The date factory instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Dates\DateFactory
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* Create a new update incident command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the update incident command.
|
||||
*
|
||||
@@ -34,7 +52,7 @@ class UpdateIncidentCommandHandler
|
||||
|
||||
// The incident occurred at a different time.
|
||||
if ($command->incident_date) {
|
||||
$incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, config('cachet.timezone'))->setTimezone(Config::get('app.timezone'));
|
||||
$incidentDate = $this->dates->createNormalized('d/m/Y H:i', $command->incident_date);
|
||||
|
||||
$incident->update([
|
||||
'created_at' => $incidentDate,
|
||||
|
||||
@@ -12,12 +12,31 @@
|
||||
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Events\Metric\MetricPointWasAddedEvent;
|
||||
use CachetHQ\Cachet\Models\MetricPoint;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class AddMetricPointCommandHandler
|
||||
{
|
||||
/**
|
||||
* The date factory instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Dates\DateFactory
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* Create a new add metric point command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the add metric point command.
|
||||
*
|
||||
@@ -36,7 +55,7 @@ class AddMetricPointCommandHandler
|
||||
];
|
||||
|
||||
if ($createdAt) {
|
||||
$data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s');
|
||||
$data['created_at'] = $this->dates->create('U', $createdAt)->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$metricPoint = MetricPoint::create($data);
|
||||
|
||||
@@ -12,11 +12,30 @@
|
||||
namespace CachetHQ\Cachet\Handlers\Commands\Metric;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Events\Metric\MetricPointWasUpdatedEvent;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class UpdateMetricPointCommandHandler
|
||||
{
|
||||
/**
|
||||
* The date factory instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Dates\DateFactory
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* Create a new update metric point command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the update metric point command.
|
||||
*
|
||||
@@ -36,7 +55,7 @@ class UpdateMetricPointCommandHandler
|
||||
];
|
||||
|
||||
if ($createdAt) {
|
||||
$data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s');
|
||||
$data['created_at'] = $this->dates->create('U', $createdAt)->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$point->update($data);
|
||||
|
||||
@@ -13,12 +13,12 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
|
||||
|
||||
use AltThree\Validator\ValidationException;
|
||||
use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\IncidentTemplate;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\MessageBag;
|
||||
@@ -130,16 +130,17 @@ class ScheduleController extends Controller
|
||||
/**
|
||||
* Updates the given incident.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Incident $schedule
|
||||
* @param \CachetHQ\Cachet\Models\Incident $schedule
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function editScheduleAction(Incident $schedule)
|
||||
public function editScheduleAction(Incident $schedule, DateFactory $dates)
|
||||
{
|
||||
$scheduleData = Binput::get('incident');
|
||||
|
||||
// Parse the schedule date.
|
||||
$scheduledAt = Date::createFromFormat('d/m/Y H:i', $scheduleData['scheduled_at'], config('cachet.timezone'))
|
||||
->setTimezone(Config::get('app.timezone'));
|
||||
$scheduledAt = $dates->createNormalized('d/m/Y H:i', $scheduleData['scheduled_at']);
|
||||
|
||||
if ($scheduledAt->isPast()) {
|
||||
$messageBag = new MessageBag();
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Providers;
|
||||
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use Illuminate\Bus\Dispatcher;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -40,6 +41,21 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
$this->registerDateFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the date factory.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerDateFactory()
|
||||
{
|
||||
$this->app->singleton(DateFactory::class, function ($app) {
|
||||
$appTimezone = $app->config->get('app.timezone');
|
||||
$cacheTimezone = $app->config->get('cachet.timezone');
|
||||
|
||||
return new DateFactory($appTimezone, $cacheTimezone);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace CachetHQ\Tests\Cachet\Api;
|
||||
|
||||
use CachetHQ\Tests\Cachet\AbstractTestCase;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
|
||||
class MetricPointTest extends AbstractTestCase
|
||||
@@ -72,6 +73,28 @@ class MetricPointTest extends AbstractTestCase
|
||||
$this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]);
|
||||
}
|
||||
|
||||
public function testPostMetricPointTimestampTimezone()
|
||||
{
|
||||
if (defined('HHVM_VERSION')) {
|
||||
$this->markTestSkipped('Timezones are broken on HHVM.');
|
||||
}
|
||||
|
||||
$this->beUser();
|
||||
|
||||
$timezone = 'America/Mexico_City';
|
||||
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
|
||||
$timestamp = Carbon::now()->timezone($timezone)->timestamp;
|
||||
$datetime = Carbon::now()->toDateTimeString();
|
||||
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
|
||||
'metric_id' => $metric->id,
|
||||
]);
|
||||
$postData = $metricPoint->toArray();
|
||||
$postData['timestamp'] = $timestamp;
|
||||
|
||||
$this->post("/api/v1/metrics/{$metric->id}/points", $postData, ['Time-Zone' => $timezone]);
|
||||
$this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]);
|
||||
}
|
||||
|
||||
public function testPutMetricPoint()
|
||||
{
|
||||
$this->beUser();
|
||||
|
||||
Reference in New Issue
Block a user