Skip to content

Commit

Permalink
refs #10 add reservation time service
Browse files Browse the repository at this point in the history
  • Loading branch information
mass-min committed Apr 28, 2019
1 parent ed43c91 commit e02914e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/Reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -22,12 +23,18 @@ class Reservation extends Model
'user_id', 'reserve_time',
];

// 7時〜11時台は20分単位の予約
const SHORT_RESERVATION_TIME_START_HOUR = 7;
const SHORT_RESERVATION_TIME_END_HOUR = 11;
// 予約する時間枠の長さ
const DEFAULT_RESERVATION_TIME_SPAN = 30;
const SHORT_RESERVATION_TIME_SPAN = 20;

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
return $this->belongsTo('App\User', 'user_id', 'id');
}

}
53 changes: 53 additions & 0 deletions app/Services/ReserveTimeOptionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Services;

use App\Reservation;
use Carbon\Carbon;

/**
* 予約時刻作成Service
*
* Class ReserveTimeOptionService
* @package App\Services
*/
class ReserveTimeOptionService
{
public function makeTimeOptions(Carbon $time)
{
$timeOptions = [];
$targetTime = $time;

// 24時間後までの予約時間の選択肢を作成する
for ($index = 0; $index < 24; $index++) {
if ($index !== 0) {
$targetTime = $time->copy()->addHour($index)->setMinute(0);
}
$timeOptions = array_merge($timeOptions, $this->makeTimeOptionsForTargetHour($targetTime));
}

return $timeOptions;
}

public function makeTimeOptionsForTargetHour(Carbon $targetTime)
{
$timeOptions = [];

if ($targetTime->hour >= Reservation::SHORT_RESERVATION_TIME_START_HOUR ||
$targetTime->hour <= Reservation::SHORT_RESERVATION_TIME_END_HOUR) {
// 作成しようとした予約開始時刻が朝の時間帯の範囲の場合は、20分単位で選択肢を作成
$startMinutes = [0, 20, 40];
} else {
$startMinutes = [0, 30];
}

foreach ($startMinutes as $startMinute) {
if ($targetTime->minute <= $startMinute) {
$timeOptions[] = $targetTime->format('Y-m-d') . ' '
. sprintf('%2d', $targetTime->hour) . ':'
. sprintf('%2d', $startMinute) . ':00';
}
}
return $timeOptions;
}
}

0 comments on commit e02914e

Please sign in to comment.