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 | |||||
20 | /** |
||||
21 | * Implementação do contrato [SchedulerModelTrait]. |
||||
22 | * @see \H4ad\Scheduler\Contracts\SchedulerModelTrait |
||||
23 | */ |
||||
24 | trait SchedulerModelTrait |
||||
25 | { |
||||
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); |
||||
35 | |||||
36 | /** |
||||
37 | * Get the value of the model's primary key. |
||||
38 | * |
||||
39 | * @return mixed |
||||
40 | */ |
||||
41 | abstract public function getKey(); |
||||
42 | |||||
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 | */ |
||||
48 | public function schedules() |
||||
49 | { |
||||
50 | return $this->hasOne(Config::get('scheduler.schedules_table'), 'model_id'); |
||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * Agenda um horário para esta model. |
||||
55 | * |
||||
56 | * @param \Carbon\Carbon|string $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
||||
57 | * @param \Carbon\Carbon|string|int|null $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon ou em int(sendo considerado os minutos de duração). |
||||
58 | * @param int|null $status Status desse horário ao ser agendado. |
||||
59 | * @param array|null $data Informações opcionais que podem ser anexadas ao horário cadastrado. |
||||
60 | * @return \H4ad\Scheduler\Models\Schedule |
||||
61 | */ |
||||
62 | public function addSchedule($start_at, $end_at = null, int $status = null, array $data = null) |
||||
63 | { |
||||
64 | $schedule = Scheduler::setModelType(self::class)->byModel()->validateSchedule($start_at, $end_at, $status); |
||||
65 | |||||
66 | $schedule['model_id'] = $this->getKey(); |
||||
67 | $schedule['data'] = $data; |
||||
68 | |||||
69 | return Schedule::create($schedule); |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
||||
74 | * Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção |
||||
75 | * se for tentado remover um horário agendado pela data de quando foi marcado. |
||||
76 | * |
||||
77 | * @param \Carbon\Carbon|string|int $schedule |
||||
78 | * @return boolean|null |
||||
79 | * |
||||
80 | * @throws \H4ad\Scheduler\Exceptions\DoesNotBelong |
||||
81 | * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
||||
82 | * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
||||
83 | */ |
||||
84 | public function removeSchedule($schedule) |
||||
85 | { |
||||
86 | if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
||||
87 | throw new CantRemoveByDate; |
||||
88 | |||||
89 | $schedule = Scheduler::parseToSchedule($schedule); |
||||
90 | |||||
91 | if(!($schedule instanceof Model)) |
||||
92 | throw (new ModelNotFound)->setValues(Schedule::class); |
||||
93 | |||||
94 | if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
||||
95 | throw new DoesNotBelong; |
||||
96 | |||||
97 | return $schedule->delete(); |
||||
98 | } |
||||
99 | |||||
100 | /** |
||||
101 | * Retorna os horários disponiveis hoje para uma determinada model. |
||||
102 | * . |
||||
103 | * @param int $duration Serve para facilitar na hora de buscar horários livres que precisem ter uma certa duração. |
||||
104 | * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. Se for nulo, ele busca a referencia da config. |
||||
105 | * @return array |
||||
106 | */ |
||||
107 | public function availableToday(int $duration, Carbon $openingTime = null) |
||||
108 | { |
||||
109 | return Scheduler::byModel(self::class)->availableToday($duration, $openingTime); |
||||
0 ignored issues
–
show
|
|||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * Retorna os horários disponiveis em um determinado dia para uma certa model. |
||||
114 | * |
||||
115 | * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
||||
116 | * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres que precisem ter uma certa duração. |
||||
117 | * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. Se for nulo, ele busca a referencia da config. |
||||
118 | * @return array |
||||
119 | */ |
||||
120 | public function availableOn(Carbon $today, int $durationMinutes, Carbon $openingTime = null) |
||||
121 | { |
||||
122 | return Scheduler::byModel(self::class)->availableOn($today, $durationMinutes, $openingTime); |
||||
0 ignored issues
–
show
The call to
H4ad\Scheduler\Facades\Scheduler::byModel() has too many arguments starting with self::class .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
123 | } |
||||
124 | } |
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.