Skip to content

Commit

Permalink
Moves timezones from model to service
Browse files Browse the repository at this point in the history
  • Loading branch information
artstorm committed Jul 15, 2015
1 parent 50bab1b commit 857d116
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
15 changes: 13 additions & 2 deletions api/app/Http/Controllers/TimezoneController.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?php namespace App\Http\Controllers;

use App\Models\Timezones;
use App\Services\Timezones;

class TimezoneController extends BaseController
{
/**
* Assign dependencies.
*
* @param Timezones $timezones
* @return void
*/
public function __construct(Timezones $timezones)
{
$this->timezones = $timezones;
}

/**
* Get all entities.
*
* @return Response
*/
public function getAll()
{
return $this->collection(Timezones::getTimezones());
return $this->collection($this->timezones->all());
}
}
40 changes: 19 additions & 21 deletions api/app/Models/Timezones.php → api/app/Services/Timezones.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
<?php namespace App\Models;
<?php namespace App\Services;

use DateTime;
use DateTimeZone;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;

/**
* Class Timezones
* @package App\Models
*
* Note this model does not have an associated database table as it is an abstract
* data model with generated token data.
*/
class Timezones extends Model

class Timezones
{
/**
* Get the collection of available timezones.
* Get all timezones.
*
* @return Collection
*/
public static function getTimezones()
public function all()
{
$allTimezones = \DateTimeZone::listIdentifiers();
$allTimezones = DateTimeZone::listIdentifiers();

$timezones = new Collection();
$now = time();

foreach ($allTimezones as $timezoneIdentifier) {
$dateTimeZone = new \DateTimeZone($timezoneIdentifier);
$dateTimeZone = new DateTimeZone($timezoneIdentifier);

//Read the current transition to get if the timezone is currently in DST
$transitions = $dateTimeZone->getTransitions($now, $now);

$timezones->push(new Collection([
'timezone_identifier' => $dateTimeZone->getName(),
'offset' => $offset = $dateTimeZone->getOffset(new \DateTime()),
'offset' => $offset = $dateTimeZone->getOffset(new DateTime()),
'is_dst' => $transitions[0]['isdst'], //only use the first transition
'display_offset' => self::getDisplayOffset($offset),
'display_offset' => $this->formatDisplayOffset($offset),
]));
}

return $timezones;
}

private static function getDisplayOffset($offset)
/**
* Format the time offset for display.
*
* @param int $offset
* @return string
*/
protected function formatDisplayOffset($offset)
{
$inital = new \DateTime();
$inital = new DateTime();
$inital->setTimestamp(abs($offset));
$hoursFormatted = $inital->format('H:i');


return ($offset >= 0 ? '+':'-') . $hoursFormatted;
}
}
2 changes: 1 addition & 1 deletion api/database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'last_name' => $faker->lastName,
'phone' => $faker->optional(0.5)->phoneNumber,
'mobile' => $faker->optional(0.5)->phoneNumber,
'timezone_identifier' => $faker->randomElement(array_pluck(\App\Models\Timezones::getTimezones(), 'timezone_identifier')),
'timezone_identifier' => $faker->randomElement(array_pluck((new \App\Services\Timezones)->all(), 'timezone_identifier')),
'user_type' => $faker->randomElement(App\Models\User::$userTypes),
];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ have their emails dispatched.

+ Body

{!! $factory->get(\App\Models\Timezones::getTimezones())->count(5)->toJson() !!}
{!! $factory->get((new \App\Services\Timezones)->all())->count(5)->toJson() !!}

0 comments on commit 857d116

Please sign in to comment.