|
1
|
|
|
<?php namespace H4ad\Scheduler\Traits; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Esse arquivo faz parte do Scheduler, |
|
5
|
|
|
* uma biblioteca para auxiliar com agendamentos. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @package H4ad\Scheduler |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use H4ad\Scheduler\Models\Schedule; |
|
13
|
|
|
use H4ad\Scheduler\Facades\Scheduler; |
|
14
|
|
|
use Illuminate\Support\Facades\Config; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
16
|
|
|
use H4ad\Scheduler\Exceptions\DoesNotBelong; |
|
17
|
|
|
use H4ad\Scheduler\Exceptions\ModelNotFound; |
|
18
|
|
|
use H4ad\Scheduler\Exceptions\CantRemoveByDate; |
|
19
|
|
|
use H4ad\Scheduler\Exceptions\CantAddWithoutEnd; |
|
20
|
|
|
use H4ad\Scheduler\Exceptions\IntInvalidArgument; |
|
21
|
|
|
use H4ad\Scheduler\Exceptions\EndCantBeforeStart; |
|
22
|
|
|
use H4ad\Scheduler\Exceptions\CantAddWithSameStartAt; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Implementação do contrato [SchedulerModelTrait]. |
|
26
|
|
|
* @see \H4ad\Scheduler\Contracts\SchedulerModelTrait |
|
27
|
|
|
*/ |
|
28
|
|
|
trait SchedulerModelTrait |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Define an inverse one-to-one or many relationship. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $related |
|
34
|
|
|
* @param string $foreignKey |
|
35
|
|
|
* @param string $ownerKey |
|
36
|
|
|
* @param string $relation |
|
37
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the value of the model's primary key. |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract public function getKey(); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
52
|
|
|
*/ |
|
53
|
|
|
public function schedules() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Agenda um horário para esta model. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Carbon\Carbon|string $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
|
62
|
|
|
* @param \Carbon\Carbon|string|int|null $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
|
63
|
|
|
* ou em int(sendo considerado os minutos de duração). |
|
64
|
|
|
* @param int|null $status Status desse horário ao ser agendado. |
|
65
|
|
|
* @return \H4ad\Scheduler\Models\Schedule |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
|
68
|
|
|
* @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
|
69
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
|
70
|
|
|
*/ |
|
71
|
|
|
public function addSchedule($start_at, $end_at = null, $status = null) |
|
72
|
|
|
{ |
|
73
|
|
|
if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
74
|
|
|
throw new CantAddWithoutEnd; |
|
75
|
|
|
|
|
76
|
|
|
$start_at = $this->parseToCarbon($start_at); |
|
77
|
|
|
|
|
78
|
|
|
if(!is_null($end_at)) { |
|
79
|
|
|
$end_at = $this->parseToCarbon($end_at, $start_at); |
|
80
|
|
|
|
|
81
|
|
|
if($start_at->greaterThan($end_at)) |
|
82
|
|
|
throw new EndCantBeforeStart; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if(Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) |
|
|
|
|
|
|
86
|
|
|
throw new CantAddWithSameStartAt; |
|
87
|
|
|
|
|
88
|
|
|
$model_id = $this->getKey(); |
|
89
|
|
|
$model_type = self::class; |
|
90
|
|
|
|
|
91
|
|
|
return Schedule::create(compact('start_at', 'end_at', 'status', 'model_id', 'model_type')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Exibe uma lista dos horários do dia de hoje. |
|
96
|
|
|
* |
|
97
|
|
|
* @param int $duration Serve para facilitar na hora de buscar horários livres |
|
98
|
|
|
* que precisem ter uma certa duração. |
|
99
|
|
|
* @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
100
|
|
|
* Se for nulo, ele busca a referencia da config. |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function availableToday($duration = 0, $openingTime = null) |
|
104
|
|
|
{ |
|
105
|
|
|
return Scheduler::availableToday(self::class, $duration, $openingTime); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Lista os horários livres em um determinado dia. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca. |
|
112
|
|
|
* @param int $duration Serve para facilitar na hora de buscar horários livres |
|
113
|
|
|
* que precisem ter uma certa duração. |
|
114
|
|
|
* @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
115
|
|
|
* Se for nulo, ele busca a referencia da config. |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function availableOn($date, $duration = 0, $openingTime = null) |
|
119
|
|
|
{ |
|
120
|
|
|
return Scheduler::availableOn(self::class, $date, $duration, $openingTime); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Faz um parse na data e retorna uma instância em Carbon. |
|
125
|
|
|
* |
|
126
|
|
|
* @param \Carbon\Carbon|string|int $date Data final que será transformada numa instancia Carbon. |
|
127
|
|
|
* @param \Carbon\Carbon $reference Data de referencia quando o [date] é inteiro. |
|
128
|
|
|
* @return \Carbon\Carbon |
|
129
|
|
|
* |
|
130
|
|
|
* @throws \H4ad\Scheduler\Exceptions\IntInvalidArgument |
|
131
|
|
|
*/ |
|
132
|
|
|
public function parseToCarbon($date, $reference = null) |
|
133
|
|
|
{ |
|
134
|
|
|
if($date instanceof Carbon) |
|
135
|
|
|
return $date; |
|
136
|
|
|
|
|
137
|
|
|
if(is_string($date)) |
|
138
|
|
|
return Carbon::parse($date); |
|
139
|
|
|
|
|
140
|
|
|
if(is_int($date) && !is_null($reference)) |
|
141
|
|
|
return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
142
|
|
|
|
|
143
|
|
|
throw new IntInvalidArgument; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Faz um parse e retorna um Schedule. |
|
148
|
|
|
* |
|
149
|
|
|
* @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
|
150
|
|
|
* @return \H4ad\Scheduler\Models\Schedule|null |
|
151
|
|
|
*/ |
|
152
|
|
|
public function parseToSchedule($value) |
|
153
|
|
|
{ |
|
154
|
|
|
if(is_int($value)) |
|
155
|
|
|
return Schedule::find($value); |
|
156
|
|
|
|
|
157
|
|
|
return Schedule::byStartAt($value)->first(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
|
162
|
|
|
* Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção |
|
163
|
|
|
* se for tentado remover um horário agendado pela data de quando foi marcado. |
|
164
|
|
|
* |
|
165
|
|
|
* @param int|string|\Carbon\Carbon $schedule Horário agendado. |
|
166
|
|
|
* @return bool|null |
|
167
|
|
|
* |
|
168
|
|
|
* @throws \H4ad\Scheduler\Exceptions\DoesNotBelong |
|
169
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
|
170
|
|
|
* @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
|
171
|
|
|
*/ |
|
172
|
|
|
public function removeSchedule($schedule) |
|
173
|
|
|
{ |
|
174
|
|
|
if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
175
|
|
|
throw new CantRemoveByDate; |
|
176
|
|
|
|
|
177
|
|
|
$schedule = $this->parseToSchedule($schedule); |
|
178
|
|
|
|
|
179
|
|
|
if(!($schedule instanceof Model)) |
|
180
|
|
|
throw (new ModelNotFound)->setValues(Schedule::class); |
|
181
|
|
|
|
|
182
|
|
|
if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
183
|
|
|
throw new DoesNotBelong; |
|
184
|
|
|
|
|
185
|
|
|
return $schedule->delete(); |
|
186
|
|
|
} |
|
187
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.