@@ -18,196 +18,196 @@ |
||
18 | 18 | |
19 | 19 | class Scheduler |
20 | 20 | { |
21 | - /** |
|
22 | - * Laravel application |
|
23 | - * |
|
24 | - * @var \Illuminate\Foundation\Application |
|
25 | - */ |
|
26 | - public $app; |
|
27 | - |
|
28 | - /** |
|
29 | - * Create a new confide instance. |
|
30 | - * |
|
31 | - * @param \Illuminate\Foundation\Application $app |
|
32 | - * |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function __construct($app) |
|
36 | - { |
|
37 | - $this->app = $app; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Escopo de uma consulta que busca horarios pela data de início. |
|
42 | - * |
|
43 | - * @param string $model_type |
|
44 | - * @param string|\Carbon\Carbon $start_at |
|
45 | - * @param string|\Carbon\Carbon $end_at |
|
46 | - * @return bool |
|
47 | - */ |
|
48 | - public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
49 | - { |
|
50 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
51 | - return false; |
|
52 | - |
|
53 | - return !is_null( |
|
54 | - Schedule::latest() |
|
55 | - ->where('model_type', $model_type) |
|
56 | - ->where('start_at', '>=', $start_at) |
|
57 | - ->where('end_at', '<=', $end_at) |
|
58 | - ->first() |
|
59 | - ); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Retorna os horários disponiveis hoje para uma determinada model. |
|
64 | - * . |
|
65 | - * @param string $model_type Tipo da model |
|
66 | - * @param int $duration Serve para facilitar na hora de buscar horários livres |
|
67 | - * que precisem ter uma certa duração. |
|
68 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
69 | - * Se for nulo, ele busca a referencia da config. |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function availableToday($model_type, $duration, $openingTime = null) |
|
73 | - { |
|
74 | - return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
79 | - * |
|
80 | - * @param string $model_type Tipo da model |
|
81 | - * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
82 | - * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
83 | - * que precisem ter uma certa duração. |
|
84 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
85 | - * Se for nulo, ele busca a referencia da config. |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
89 | - { |
|
90 | - $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
91 | - $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
92 | - |
|
93 | - $livres = []; |
|
94 | - $today = Carbon::parse($today->toDateString()); |
|
95 | - while($openingTime <= $closingTime) |
|
96 | - { |
|
97 | - $add = true; |
|
98 | - |
|
99 | - $opening = Carbon::parse($openingTime->toDateTimeString()); |
|
100 | - $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
|
101 | - |
|
102 | - foreach (Schedule::where('model_type', $model_type)->orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
103 | - $start = Carbon::parse($schedule->start_at); |
|
104 | - $begin = Carbon::parse($start->toDateString()); |
|
105 | - |
|
106 | - if($begin->greaterThan($today)) |
|
107 | - break; |
|
108 | - |
|
109 | - if($begin->notEqualTo($today)) |
|
110 | - continue; |
|
111 | - |
|
112 | - $end = Carbon::parse($schedule->end_at); |
|
113 | - |
|
114 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
115 | - $add = false; |
|
116 | - } |
|
117 | - |
|
118 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
119 | - $livres[] = [ |
|
120 | - 'start_at' => $opening, |
|
121 | - 'end_at' => $closing |
|
122 | - ]; |
|
123 | - |
|
124 | - $openingTime->addMinutes($durationMinutes); |
|
125 | - } |
|
126 | - |
|
127 | - return $livres; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Verifica se ele não deve ser adicionado ao array de horários livres. |
|
132 | - * |
|
133 | - * @param \Carbon\Carbon $opening |
|
134 | - * @param \Carbon\Carbon $closing |
|
135 | - * @param \Carbon\Carbon $start |
|
136 | - * @param \Carbon\Carbon $end |
|
137 | - * @return boolean |
|
138 | - */ |
|
139 | - private function isShouldntAdd($opening, $closing, $start, $end) |
|
140 | - { |
|
141 | - return $start <= $opening && $end >= $closing; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Valida e retorna os dados formatados de forma correta em um [array]. |
|
146 | - * |
|
147 | - * @param \Carbon\Carbon|string $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
|
148 | - * @param \Carbon\Carbon|string|int|null $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
|
149 | - * ou em int(sendo considerado os minutos de duração). |
|
150 | - * @param int|null $status Status desse horário ao ser agendado. |
|
151 | - * @return array |
|
152 | - * |
|
153 | - * @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
|
154 | - * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
|
155 | - * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
|
156 | - */ |
|
157 | - public function validateSchedule($model_type, $start_at, $end_at = null, $status = null) |
|
158 | - { |
|
159 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
160 | - throw new CantAddWithoutEnd; |
|
161 | - |
|
162 | - $start_at = $this->parseToCarbon($start_at); |
|
163 | - |
|
164 | - if(!is_null($end_at)) { |
|
165 | - $end_at = $this->parseToCarbon($end_at, $start_at); |
|
166 | - |
|
167 | - if($start_at->greaterThan($end_at)) |
|
168 | - throw new EndCantBeforeStart; |
|
169 | - } |
|
170 | - |
|
171 | - if($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) |
|
172 | - throw new CantAddWithSameStartAt; |
|
173 | - |
|
174 | - return compact('model_type', 'start_at', 'end_at', 'status'); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Faz um parse na data e retorna uma instância em Carbon. |
|
179 | - * |
|
180 | - * @param \Carbon\Carbon|string|int $date Data final que será transformada numa instancia Carbon. |
|
181 | - * @param \Carbon\Carbon $reference Data de referencia quando o [date] é inteiro. |
|
182 | - * @return \Carbon\Carbon |
|
183 | - * |
|
184 | - * @throws \H4ad\Scheduler\Exceptions\IntInvalidArgument |
|
185 | - */ |
|
186 | - public function parseToCarbon($date, $reference = null) |
|
187 | - { |
|
188 | - if($date instanceof Carbon) |
|
189 | - return $date; |
|
190 | - |
|
191 | - if(is_string($date)) |
|
192 | - return Carbon::parse($date); |
|
193 | - |
|
194 | - if(is_int($date) && !is_null($reference)) |
|
195 | - return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
196 | - |
|
197 | - throw new IntInvalidArgument; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Faz um parse e retorna um Schedule. |
|
202 | - * |
|
203 | - * @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
|
204 | - * @return \H4ad\Scheduler\Models\Schedule|null |
|
205 | - */ |
|
206 | - public function parseToSchedule($value) |
|
207 | - { |
|
208 | - if(is_int($value)) |
|
209 | - return Schedule::find($value); |
|
210 | - |
|
211 | - return Schedule::byStartAt($value)->first(); |
|
212 | - } |
|
21 | + /** |
|
22 | + * Laravel application |
|
23 | + * |
|
24 | + * @var \Illuminate\Foundation\Application |
|
25 | + */ |
|
26 | + public $app; |
|
27 | + |
|
28 | + /** |
|
29 | + * Create a new confide instance. |
|
30 | + * |
|
31 | + * @param \Illuminate\Foundation\Application $app |
|
32 | + * |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function __construct($app) |
|
36 | + { |
|
37 | + $this->app = $app; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Escopo de uma consulta que busca horarios pela data de início. |
|
42 | + * |
|
43 | + * @param string $model_type |
|
44 | + * @param string|\Carbon\Carbon $start_at |
|
45 | + * @param string|\Carbon\Carbon $end_at |
|
46 | + * @return bool |
|
47 | + */ |
|
48 | + public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
49 | + { |
|
50 | + if(!Config::get('scheduler.enable_schedule_conflict')) |
|
51 | + return false; |
|
52 | + |
|
53 | + return !is_null( |
|
54 | + Schedule::latest() |
|
55 | + ->where('model_type', $model_type) |
|
56 | + ->where('start_at', '>=', $start_at) |
|
57 | + ->where('end_at', '<=', $end_at) |
|
58 | + ->first() |
|
59 | + ); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Retorna os horários disponiveis hoje para uma determinada model. |
|
64 | + * . |
|
65 | + * @param string $model_type Tipo da model |
|
66 | + * @param int $duration Serve para facilitar na hora de buscar horários livres |
|
67 | + * que precisem ter uma certa duração. |
|
68 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
69 | + * Se for nulo, ele busca a referencia da config. |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function availableToday($model_type, $duration, $openingTime = null) |
|
73 | + { |
|
74 | + return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
79 | + * |
|
80 | + * @param string $model_type Tipo da model |
|
81 | + * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
82 | + * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
83 | + * que precisem ter uma certa duração. |
|
84 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
85 | + * Se for nulo, ele busca a referencia da config. |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
89 | + { |
|
90 | + $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
91 | + $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
92 | + |
|
93 | + $livres = []; |
|
94 | + $today = Carbon::parse($today->toDateString()); |
|
95 | + while($openingTime <= $closingTime) |
|
96 | + { |
|
97 | + $add = true; |
|
98 | + |
|
99 | + $opening = Carbon::parse($openingTime->toDateTimeString()); |
|
100 | + $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
|
101 | + |
|
102 | + foreach (Schedule::where('model_type', $model_type)->orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
103 | + $start = Carbon::parse($schedule->start_at); |
|
104 | + $begin = Carbon::parse($start->toDateString()); |
|
105 | + |
|
106 | + if($begin->greaterThan($today)) |
|
107 | + break; |
|
108 | + |
|
109 | + if($begin->notEqualTo($today)) |
|
110 | + continue; |
|
111 | + |
|
112 | + $end = Carbon::parse($schedule->end_at); |
|
113 | + |
|
114 | + if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
115 | + $add = false; |
|
116 | + } |
|
117 | + |
|
118 | + if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
119 | + $livres[] = [ |
|
120 | + 'start_at' => $opening, |
|
121 | + 'end_at' => $closing |
|
122 | + ]; |
|
123 | + |
|
124 | + $openingTime->addMinutes($durationMinutes); |
|
125 | + } |
|
126 | + |
|
127 | + return $livres; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Verifica se ele não deve ser adicionado ao array de horários livres. |
|
132 | + * |
|
133 | + * @param \Carbon\Carbon $opening |
|
134 | + * @param \Carbon\Carbon $closing |
|
135 | + * @param \Carbon\Carbon $start |
|
136 | + * @param \Carbon\Carbon $end |
|
137 | + * @return boolean |
|
138 | + */ |
|
139 | + private function isShouldntAdd($opening, $closing, $start, $end) |
|
140 | + { |
|
141 | + return $start <= $opening && $end >= $closing; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Valida e retorna os dados formatados de forma correta em um [array]. |
|
146 | + * |
|
147 | + * @param \Carbon\Carbon|string $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
|
148 | + * @param \Carbon\Carbon|string|int|null $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
|
149 | + * ou em int(sendo considerado os minutos de duração). |
|
150 | + * @param int|null $status Status desse horário ao ser agendado. |
|
151 | + * @return array |
|
152 | + * |
|
153 | + * @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
|
154 | + * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
|
155 | + * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
|
156 | + */ |
|
157 | + public function validateSchedule($model_type, $start_at, $end_at = null, $status = null) |
|
158 | + { |
|
159 | + if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
160 | + throw new CantAddWithoutEnd; |
|
161 | + |
|
162 | + $start_at = $this->parseToCarbon($start_at); |
|
163 | + |
|
164 | + if(!is_null($end_at)) { |
|
165 | + $end_at = $this->parseToCarbon($end_at, $start_at); |
|
166 | + |
|
167 | + if($start_at->greaterThan($end_at)) |
|
168 | + throw new EndCantBeforeStart; |
|
169 | + } |
|
170 | + |
|
171 | + if($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) |
|
172 | + throw new CantAddWithSameStartAt; |
|
173 | + |
|
174 | + return compact('model_type', 'start_at', 'end_at', 'status'); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Faz um parse na data e retorna uma instância em Carbon. |
|
179 | + * |
|
180 | + * @param \Carbon\Carbon|string|int $date Data final que será transformada numa instancia Carbon. |
|
181 | + * @param \Carbon\Carbon $reference Data de referencia quando o [date] é inteiro. |
|
182 | + * @return \Carbon\Carbon |
|
183 | + * |
|
184 | + * @throws \H4ad\Scheduler\Exceptions\IntInvalidArgument |
|
185 | + */ |
|
186 | + public function parseToCarbon($date, $reference = null) |
|
187 | + { |
|
188 | + if($date instanceof Carbon) |
|
189 | + return $date; |
|
190 | + |
|
191 | + if(is_string($date)) |
|
192 | + return Carbon::parse($date); |
|
193 | + |
|
194 | + if(is_int($date) && !is_null($reference)) |
|
195 | + return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
196 | + |
|
197 | + throw new IntInvalidArgument; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Faz um parse e retorna um Schedule. |
|
202 | + * |
|
203 | + * @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
|
204 | + * @return \H4ad\Scheduler\Models\Schedule|null |
|
205 | + */ |
|
206 | + public function parseToSchedule($value) |
|
207 | + { |
|
208 | + if(is_int($value)) |
|
209 | + return Schedule::find($value); |
|
210 | + |
|
211 | + return Schedule::byStartAt($value)->first(); |
|
212 | + } |
|
213 | 213 | } |
214 | 214 | \ No newline at end of file |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return void |
34 | 34 | */ |
35 | - public function __construct($app) |
|
35 | + public function __construct ($app) |
|
36 | 36 | { |
37 | 37 | $this->app = $app; |
38 | 38 | } |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | * @param string|\Carbon\Carbon $end_at |
46 | 46 | * @return bool |
47 | 47 | */ |
48 | - public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
48 | + public function hasScheduleBetween ($model_type, $start_at, $end_at) |
|
49 | 49 | { |
50 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
50 | + if (!Config::get('scheduler.enable_schedule_conflict')) |
|
51 | 51 | return false; |
52 | 52 | |
53 | 53 | return !is_null( |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * Se for nulo, ele busca a referencia da config. |
70 | 70 | * @return array |
71 | 71 | */ |
72 | - public function availableToday($model_type, $duration, $openingTime = null) |
|
72 | + public function availableToday ($model_type, $duration, $openingTime = null) |
|
73 | 73 | { |
74 | 74 | return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
75 | 75 | } |
@@ -85,14 +85,14 @@ discard block |
||
85 | 85 | * Se for nulo, ele busca a referencia da config. |
86 | 86 | * @return array |
87 | 87 | */ |
88 | - public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
88 | + public function availableOn ($model_type, $today, $durationMinutes, $openingTime = null) |
|
89 | 89 | { |
90 | 90 | $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
91 | 91 | $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
92 | 92 | |
93 | - $livres = []; |
|
93 | + $livres = [ ]; |
|
94 | 94 | $today = Carbon::parse($today->toDateString()); |
95 | - while($openingTime <= $closingTime) |
|
95 | + while ($openingTime <= $closingTime) |
|
96 | 96 | { |
97 | 97 | $add = true; |
98 | 98 | |
@@ -103,20 +103,20 @@ discard block |
||
103 | 103 | $start = Carbon::parse($schedule->start_at); |
104 | 104 | $begin = Carbon::parse($start->toDateString()); |
105 | 105 | |
106 | - if($begin->greaterThan($today)) |
|
106 | + if ($begin->greaterThan($today)) |
|
107 | 107 | break; |
108 | 108 | |
109 | - if($begin->notEqualTo($today)) |
|
109 | + if ($begin->notEqualTo($today)) |
|
110 | 110 | continue; |
111 | 111 | |
112 | 112 | $end = Carbon::parse($schedule->end_at); |
113 | 113 | |
114 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
114 | + if ($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
115 | 115 | $add = false; |
116 | 116 | } |
117 | 117 | |
118 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
119 | - $livres[] = [ |
|
118 | + if ($add && $closing->lessThanOrEqualTo($closingTime)) |
|
119 | + $livres[ ] = [ |
|
120 | 120 | 'start_at' => $opening, |
121 | 121 | 'end_at' => $closing |
122 | 122 | ]; |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | * @param \Carbon\Carbon $end |
137 | 137 | * @return boolean |
138 | 138 | */ |
139 | - private function isShouldntAdd($opening, $closing, $start, $end) |
|
139 | + private function isShouldntAdd ($opening, $closing, $start, $end) |
|
140 | 140 | { |
141 | 141 | return $start <= $opening && $end >= $closing; |
142 | 142 | } |
@@ -154,21 +154,21 @@ discard block |
||
154 | 154 | * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
155 | 155 | * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
156 | 156 | */ |
157 | - public function validateSchedule($model_type, $start_at, $end_at = null, $status = null) |
|
157 | + public function validateSchedule ($model_type, $start_at, $end_at = null, $status = null) |
|
158 | 158 | { |
159 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
159 | + if (!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
160 | 160 | throw new CantAddWithoutEnd; |
161 | 161 | |
162 | - $start_at = $this->parseToCarbon($start_at); |
|
162 | + $start_at = $this->parseToCarbon($start_at); |
|
163 | 163 | |
164 | - if(!is_null($end_at)) { |
|
164 | + if (!is_null($end_at)) { |
|
165 | 165 | $end_at = $this->parseToCarbon($end_at, $start_at); |
166 | 166 | |
167 | - if($start_at->greaterThan($end_at)) |
|
167 | + if ($start_at->greaterThan($end_at)) |
|
168 | 168 | throw new EndCantBeforeStart; |
169 | 169 | } |
170 | 170 | |
171 | - if($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) |
|
171 | + if ($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) |
|
172 | 172 | throw new CantAddWithSameStartAt; |
173 | 173 | |
174 | 174 | return compact('model_type', 'start_at', 'end_at', 'status'); |
@@ -183,15 +183,15 @@ discard block |
||
183 | 183 | * |
184 | 184 | * @throws \H4ad\Scheduler\Exceptions\IntInvalidArgument |
185 | 185 | */ |
186 | - public function parseToCarbon($date, $reference = null) |
|
186 | + public function parseToCarbon ($date, $reference = null) |
|
187 | 187 | { |
188 | - if($date instanceof Carbon) |
|
188 | + if ($date instanceof Carbon) |
|
189 | 189 | return $date; |
190 | 190 | |
191 | - if(is_string($date)) |
|
191 | + if (is_string($date)) |
|
192 | 192 | return Carbon::parse($date); |
193 | 193 | |
194 | - if(is_int($date) && !is_null($reference)) |
|
194 | + if (is_int($date) && !is_null($reference)) |
|
195 | 195 | return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
196 | 196 | |
197 | 197 | throw new IntInvalidArgument; |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | * @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
204 | 204 | * @return \H4ad\Scheduler\Models\Schedule|null |
205 | 205 | */ |
206 | - public function parseToSchedule($value) |
|
206 | + public function parseToSchedule ($value) |
|
207 | 207 | { |
208 | - if(is_int($value)) |
|
208 | + if (is_int($value)) |
|
209 | 209 | return Schedule::find($value); |
210 | 210 | |
211 | 211 | return Schedule::byStartAt($value)->first(); |
@@ -47,8 +47,9 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public function hasScheduleBetween($model_type, $start_at, $end_at) |
49 | 49 | { |
50 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
51 | - return false; |
|
50 | + if(!Config::get('scheduler.enable_schedule_conflict')) { |
|
51 | + return false; |
|
52 | + } |
|
52 | 53 | |
53 | 54 | return !is_null( |
54 | 55 | Schedule::latest() |
@@ -103,23 +104,27 @@ discard block |
||
103 | 104 | $start = Carbon::parse($schedule->start_at); |
104 | 105 | $begin = Carbon::parse($start->toDateString()); |
105 | 106 | |
106 | - if($begin->greaterThan($today)) |
|
107 | - break; |
|
107 | + if($begin->greaterThan($today)) { |
|
108 | + break; |
|
109 | + } |
|
108 | 110 | |
109 | - if($begin->notEqualTo($today)) |
|
110 | - continue; |
|
111 | + if($begin->notEqualTo($today)) { |
|
112 | + continue; |
|
113 | + } |
|
111 | 114 | |
112 | 115 | $end = Carbon::parse($schedule->end_at); |
113 | 116 | |
114 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
115 | - $add = false; |
|
117 | + if($this->isShouldntAdd($opening, $closing, $start, $end)) { |
|
118 | + $add = false; |
|
119 | + } |
|
116 | 120 | } |
117 | 121 | |
118 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
119 | - $livres[] = [ |
|
122 | + if($add && $closing->lessThanOrEqualTo($closingTime)) { |
|
123 | + $livres[] = [ |
|
120 | 124 | 'start_at' => $opening, |
121 | 125 | 'end_at' => $closing |
122 | 126 | ]; |
127 | + } |
|
123 | 128 | |
124 | 129 | $openingTime->addMinutes($durationMinutes); |
125 | 130 | } |
@@ -156,20 +161,23 @@ discard block |
||
156 | 161 | */ |
157 | 162 | public function validateSchedule($model_type, $start_at, $end_at = null, $status = null) |
158 | 163 | { |
159 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
160 | - throw new CantAddWithoutEnd; |
|
164 | + if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) { |
|
165 | + throw new CantAddWithoutEnd; |
|
166 | + } |
|
161 | 167 | |
162 | 168 | $start_at = $this->parseToCarbon($start_at); |
163 | 169 | |
164 | 170 | if(!is_null($end_at)) { |
165 | 171 | $end_at = $this->parseToCarbon($end_at, $start_at); |
166 | 172 | |
167 | - if($start_at->greaterThan($end_at)) |
|
168 | - throw new EndCantBeforeStart; |
|
173 | + if($start_at->greaterThan($end_at)) { |
|
174 | + throw new EndCantBeforeStart; |
|
175 | + } |
|
169 | 176 | } |
170 | 177 | |
171 | - if($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) |
|
172 | - throw new CantAddWithSameStartAt; |
|
178 | + if($this->hasScheduleBetween($model_type, $start_at, $end_at ?? $start_at)) { |
|
179 | + throw new CantAddWithSameStartAt; |
|
180 | + } |
|
173 | 181 | |
174 | 182 | return compact('model_type', 'start_at', 'end_at', 'status'); |
175 | 183 | } |
@@ -185,14 +193,17 @@ discard block |
||
185 | 193 | */ |
186 | 194 | public function parseToCarbon($date, $reference = null) |
187 | 195 | { |
188 | - if($date instanceof Carbon) |
|
189 | - return $date; |
|
196 | + if($date instanceof Carbon) { |
|
197 | + return $date; |
|
198 | + } |
|
190 | 199 | |
191 | - if(is_string($date)) |
|
192 | - return Carbon::parse($date); |
|
200 | + if(is_string($date)) { |
|
201 | + return Carbon::parse($date); |
|
202 | + } |
|
193 | 203 | |
194 | - if(is_int($date) && !is_null($reference)) |
|
195 | - return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
204 | + if(is_int($date) && !is_null($reference)) { |
|
205 | + return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
206 | + } |
|
196 | 207 | |
197 | 208 | throw new IntInvalidArgument; |
198 | 209 | } |
@@ -205,8 +216,9 @@ discard block |
||
205 | 216 | */ |
206 | 217 | public function parseToSchedule($value) |
207 | 218 | { |
208 | - if(is_int($value)) |
|
209 | - return Schedule::find($value); |
|
219 | + if(is_int($value)) { |
|
220 | + return Schedule::find($value); |
|
221 | + } |
|
210 | 222 | |
211 | 223 | return Schedule::byStartAt($value)->first(); |
212 | 224 | } |
@@ -24,27 +24,27 @@ discard block |
||
24 | 24 | trait SchedulerModelTrait |
25 | 25 | { |
26 | 26 | /** |
27 | - * Define a one-to-one relationship. |
|
28 | - * |
|
29 | - * @param string $related |
|
30 | - * @param string $foreignKey |
|
31 | - * @param string $localKey |
|
32 | - * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
33 | - */ |
|
34 | - abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
27 | + * Define a one-to-one relationship. |
|
28 | + * |
|
29 | + * @param string $related |
|
30 | + * @param string $foreignKey |
|
31 | + * @param string $localKey |
|
32 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
33 | + */ |
|
34 | + abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
35 | 35 | |
36 | 36 | /** |
37 | - * Get the value of the model's primary key. |
|
38 | - * |
|
39 | - * @return mixed |
|
40 | - */ |
|
37 | + * Get the value of the model's primary key. |
|
38 | + * |
|
39 | + * @return mixed |
|
40 | + */ |
|
41 | 41 | abstract public function getKey(); |
42 | 42 | |
43 | 43 | /** |
44 | - * Retorna apenas o horário que possui o mesmo [model_id] do [parent] dessa [trait]. |
|
45 | - * |
|
46 | - * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
47 | - */ |
|
44 | + * Retorna apenas o horário que possui o mesmo [model_id] do [parent] dessa [trait]. |
|
45 | + * |
|
46 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
47 | + */ |
|
48 | 48 | public function schedules() |
49 | 49 | { |
50 | 50 | return $this->hasOne(Config::get('scheduler.schedules_table'), 'model_id'); |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @param int $duration Serve para facilitar na hora de buscar horários livres |
74 | 74 | * que precisem ter uma certa duração. |
75 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
76 | - * Se for nulo, ele busca a referencia da config. |
|
75 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
76 | + * Se for nulo, ele busca a referencia da config. |
|
77 | 77 | * @return array |
78 | 78 | */ |
79 | 79 | public function availableToday($duration = 0, $openingTime = null) |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | * @param string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca. |
88 | 88 | * @param int $duration Serve para facilitar na hora de buscar horários livres |
89 | 89 | * que precisem ter uma certa duração. |
90 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
91 | - * Se for nulo, ele busca a referencia da config. |
|
90 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
91 | + * Se for nulo, ele busca a referencia da config. |
|
92 | 92 | * @return array |
93 | 93 | */ |
94 | 94 | public function availableOn($date, $duration = 0, $openingTime = null) |
@@ -31,21 +31,21 @@ discard block |
||
31 | 31 | * @param string $localKey |
32 | 32 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
33 | 33 | */ |
34 | - abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
34 | + abstract public function hasOne ($related, $foreignKey = null, $localKey = null); |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Get the value of the model's primary key. |
38 | 38 | * |
39 | 39 | * @return mixed |
40 | 40 | */ |
41 | - abstract public function getKey(); |
|
41 | + abstract public function getKey (); |
|
42 | 42 | |
43 | 43 | /** |
44 | 44 | * Retorna apenas o horário que possui o mesmo [model_id] do [parent] dessa [trait]. |
45 | 45 | * |
46 | 46 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
47 | 47 | */ |
48 | - public function schedules() |
|
48 | + public function schedules () |
|
49 | 49 | { |
50 | 50 | return $this->hasOne(Config::get('scheduler.schedules_table'), 'model_id'); |
51 | 51 | } |
@@ -59,10 +59,10 @@ discard block |
||
59 | 59 | * @param int|null $status Status desse horário ao ser agendado. |
60 | 60 | * @return \H4ad\Scheduler\Models\Schedule |
61 | 61 | */ |
62 | - public function addSchedule($start_at, $end_at = null, $status = null) |
|
62 | + public function addSchedule ($start_at, $end_at = null, $status = null) |
|
63 | 63 | { |
64 | 64 | $data = Scheduler::validateSchedule(self::class, $start_at, $end_at, $status); |
65 | - $data['model_id'] = $this->getKey(); |
|
65 | + $data[ 'model_id' ] = $this->getKey(); |
|
66 | 66 | |
67 | 67 | return Schedule::create($data); |
68 | 68 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * Se for nulo, ele busca a referencia da config. |
77 | 77 | * @return array |
78 | 78 | */ |
79 | - public function availableToday($duration = 0, $openingTime = null) |
|
79 | + public function availableToday ($duration = 0, $openingTime = null) |
|
80 | 80 | { |
81 | 81 | return Scheduler::availableToday(self::class, $duration, $openingTime); |
82 | 82 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * Se for nulo, ele busca a referencia da config. |
92 | 92 | * @return array |
93 | 93 | */ |
94 | - public function availableOn($date, $duration = 0, $openingTime = null) |
|
94 | + public function availableOn ($date, $duration = 0, $openingTime = null) |
|
95 | 95 | { |
96 | 96 | return Scheduler::availableOn(self::class, $date, $duration, $openingTime); |
97 | 97 | } |
@@ -108,17 +108,17 @@ discard block |
||
108 | 108 | * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
109 | 109 | * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
110 | 110 | */ |
111 | - public function removeSchedule($schedule) |
|
111 | + public function removeSchedule ($schedule) |
|
112 | 112 | { |
113 | - if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
113 | + if (!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
114 | 114 | throw new CantRemoveByDate; |
115 | 115 | |
116 | 116 | $schedule = Scheduler::parseToSchedule($schedule); |
117 | 117 | |
118 | - if(!($schedule instanceof Model)) |
|
118 | + if (!($schedule instanceof Model)) |
|
119 | 119 | throw (new ModelNotFound)->setValues(Schedule::class); |
120 | 120 | |
121 | - if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
121 | + if ($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
122 | 122 | throw new DoesNotBelong; |
123 | 123 | |
124 | 124 | return $schedule->delete(); |
@@ -110,16 +110,19 @@ |
||
110 | 110 | */ |
111 | 111 | public function removeSchedule($schedule) |
112 | 112 | { |
113 | - if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
114 | - throw new CantRemoveByDate; |
|
113 | + if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) { |
|
114 | + throw new CantRemoveByDate; |
|
115 | + } |
|
115 | 116 | |
116 | 117 | $schedule = Scheduler::parseToSchedule($schedule); |
117 | 118 | |
118 | - if(!($schedule instanceof Model)) |
|
119 | - throw (new ModelNotFound)->setValues(Schedule::class); |
|
119 | + if(!($schedule instanceof Model)) { |
|
120 | + throw (new ModelNotFound)->setValues(Schedule::class); |
|
121 | + } |
|
120 | 122 | |
121 | - if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
122 | - throw new DoesNotBelong; |
|
123 | + if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) { |
|
124 | + throw new DoesNotBelong; |
|
125 | + } |
|
123 | 126 | |
124 | 127 | return $schedule->delete(); |
125 | 128 | } |
@@ -11,27 +11,27 @@ discard block |
||
11 | 11 | interface SchedulerModelInterface |
12 | 12 | { |
13 | 13 | /** |
14 | - * Define a one-to-one relationship. |
|
15 | - * |
|
16 | - * @param string $related |
|
17 | - * @param string $foreignKey |
|
18 | - * @param string $localKey |
|
19 | - * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
20 | - */ |
|
21 | - abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
14 | + * Define a one-to-one relationship. |
|
15 | + * |
|
16 | + * @param string $related |
|
17 | + * @param string $foreignKey |
|
18 | + * @param string $localKey |
|
19 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
20 | + */ |
|
21 | + abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
22 | 22 | |
23 | 23 | /** |
24 | - * Get the value of the model's primary key. |
|
25 | - * |
|
26 | - * @return mixed |
|
27 | - */ |
|
24 | + * Get the value of the model's primary key. |
|
25 | + * |
|
26 | + * @return mixed |
|
27 | + */ |
|
28 | 28 | abstract public function getKey(); |
29 | 29 | |
30 | 30 | /** |
31 | - * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
|
32 | - * |
|
33 | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
34 | - */ |
|
31 | + * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
|
32 | + * |
|
33 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
34 | + */ |
|
35 | 35 | public function schedules(); |
36 | 36 | |
37 | 37 | /** |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @param int $duration Serve para facilitar na hora de buscar horários livres |
52 | 52 | * que precisem ter uma certa duração. |
53 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
54 | - * Se for nulo, ele busca a referencia da config. |
|
53 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
54 | + * Se for nulo, ele busca a referencia da config. |
|
55 | 55 | * @return array |
56 | 56 | */ |
57 | 57 | public function availableToday($duration = 0, $openingTime = null); |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | * @param string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca. |
63 | 63 | * @param int $duration Serve para facilitar na hora de buscar horários livres |
64 | 64 | * que precisem ter uma certa duração. |
65 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
66 | - * Se for nulo, ele busca a referencia da config. |
|
65 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
66 | + * Se for nulo, ele busca a referencia da config. |
|
67 | 67 | * @return array |
68 | 68 | */ |
69 | 69 | public function availableOn($date, $duration = 0, $openingTime = null); |
@@ -18,21 +18,21 @@ discard block |
||
18 | 18 | * @param string $localKey |
19 | 19 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
20 | 20 | */ |
21 | - abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
21 | + abstract public function hasOne ($related, $foreignKey = null, $localKey = null); |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Get the value of the model's primary key. |
25 | 25 | * |
26 | 26 | * @return mixed |
27 | 27 | */ |
28 | - abstract public function getKey(); |
|
28 | + abstract public function getKey (); |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
32 | 32 | * |
33 | 33 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
34 | 34 | */ |
35 | - public function schedules(); |
|
35 | + public function schedules (); |
|
36 | 36 | |
37 | 37 | /** |
38 | 38 | * Agenda um horário para esta model. |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * @param int $status Status desse horário ao ser agendado. |
44 | 44 | * @return \H4ad\Scheduler\Models\Schedule |
45 | 45 | */ |
46 | - public function addSchedule($start_at, $end_at = null, $status = null); |
|
46 | + public function addSchedule ($start_at, $end_at = null, $status = null); |
|
47 | 47 | |
48 | 48 | /** |
49 | 49 | * Exibe uma lista dos horários do dia de hoje. |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | * Se for nulo, ele busca a referencia da config. |
55 | 55 | * @return array |
56 | 56 | */ |
57 | - public function availableToday($duration = 0, $openingTime = null); |
|
57 | + public function availableToday ($duration = 0, $openingTime = null); |
|
58 | 58 | |
59 | 59 | /** |
60 | 60 | * Lista os horários livres em um determinado dia. |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * Se for nulo, ele busca a referencia da config. |
67 | 67 | * @return array |
68 | 68 | */ |
69 | - public function availableOn($date, $duration = 0, $openingTime = null); |
|
69 | + public function availableOn ($date, $duration = 0, $openingTime = null); |
|
70 | 70 | |
71 | 71 | /** |
72 | 72 | * Faz um parse e retorna um Schedule. |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | * @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
75 | 75 | * @return \H4ad\Scheduler\Models\Schedule|null |
76 | 76 | */ |
77 | - public function parseToSchedule($value); |
|
77 | + public function parseToSchedule ($value); |
|
78 | 78 | |
79 | 79 | /** |
80 | 80 | * Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
@@ -88,5 +88,5 @@ discard block |
||
88 | 88 | * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
89 | 89 | * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
90 | 90 | */ |
91 | - public function removeSchedule($schedule); |
|
91 | + public function removeSchedule ($schedule); |
|
92 | 92 | } |
93 | 93 | \ No newline at end of file |