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\EndCantBeforeStart; |
21
|
|
|
use H4ad\Scheduler\Exceptions\CantAddWithSameStartAt; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implementação do contrato [SchedulerModelTrait]. |
25
|
|
|
* @see \H4ad\Scheduler\Contracts\SchedulerModelTrait |
26
|
|
|
*/ |
27
|
|
|
trait SchedulerModelTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Define an inverse one-to-one or many relationship. |
31
|
|
|
* |
32
|
|
|
* @param string $related |
33
|
|
|
* @param string $foreignKey |
34
|
|
|
* @param string $ownerKey |
35
|
|
|
* @param string $relation |
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
37
|
|
|
*/ |
38
|
|
|
abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the value of the model's primary key. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
abstract public function getKey(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
51
|
|
|
*/ |
52
|
|
|
public function schedules() |
53
|
|
|
{ |
54
|
|
|
return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Agenda um horário para esta model. |
59
|
|
|
* |
60
|
|
|
* @param string|\Carbon\Carbon $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
61
|
|
|
* @param string|\Carbon\Carbon|int $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
62
|
|
|
* ou em int(sendo considerado os minutos de duração). |
63
|
|
|
* @param int $status Status desse horário ao ser agendado. |
64
|
|
|
* @return \H4ad\Scheduler\Models\Schedule |
65
|
|
|
* |
66
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
67
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
68
|
|
|
* @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
69
|
|
|
*/ |
70
|
|
|
public function addSchedule($start_at, $end_at = null, $status = null) |
71
|
|
|
{ |
72
|
|
|
if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
73
|
|
|
throw new CantAddWithoutEnd; |
74
|
|
|
|
75
|
|
|
$start_at = $this->parseToCarbon($start_at); |
76
|
|
|
$end_at = $this->parseToCarbon($end_at, $start_at); |
77
|
|
|
|
78
|
|
|
if(Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) |
|
|
|
|
79
|
|
|
throw new CantAddWithSameStartAt; |
80
|
|
|
|
81
|
|
|
if($start_at->greaterThan($end_at) && !is_null($end_at)) |
82
|
|
|
throw new EndCantBeforeStart; |
83
|
|
|
|
84
|
|
|
$model_id = $this->getKey(); |
85
|
|
|
$model_type = self::class; |
86
|
|
|
|
87
|
|
|
return Schedule::create(compact('start_at', 'end_at', 'status', 'model_id', 'model_type')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Faz um parse na data e retorna uma instância em Carbon. |
92
|
|
|
* |
93
|
|
|
* @param string|int $date Data final que será transformada numa instancia Carbon. |
94
|
|
|
* @param \Carbon\Carbon $reference Data de referencia quando o [date] é inteiro. |
95
|
|
|
* @return \Carbon\Carbon |
96
|
|
|
*/ |
97
|
|
|
public function parseToCarbon($date, $reference = null) |
98
|
|
|
{ |
99
|
|
|
if(is_string($date)) |
100
|
|
|
return Carbon::parse($date); |
101
|
|
|
|
102
|
|
|
if(is_int($date)) |
|
|
|
|
103
|
|
|
return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
|
|
|
104
|
|
|
|
105
|
|
|
return $date; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Faz um parse e retorna um Schedule. |
110
|
|
|
* |
111
|
|
|
* @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
112
|
|
|
* @return H4ad\Scheduler\Models\Schedule|null |
|
|
|
|
113
|
|
|
*/ |
114
|
|
|
public function parseToSchedule($value) |
115
|
|
|
{ |
116
|
|
|
if(is_int($value)) |
117
|
|
|
return Schedule::find($value); |
|
|
|
|
118
|
|
|
|
119
|
|
|
if(is_string($value) || $value instanceof Carbon) |
|
|
|
|
120
|
|
|
return Schedule::byStartAt($value)->first(); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
127
|
|
|
* Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção |
128
|
|
|
* se for tentado remover um horário agendado pela data de quando foi marcado. |
129
|
|
|
* |
130
|
|
|
* @param int|string|\Carbon\Carbon $schedule Horário agendado. |
131
|
|
|
* @return bool|null |
132
|
|
|
* |
133
|
|
|
* @throws \H4ad\Scheduler\Exceptions\DoesNotBelong |
134
|
|
|
* @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
135
|
|
|
* @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
136
|
|
|
*/ |
137
|
|
|
public function removeSchedule($schedule) |
138
|
|
|
{ |
139
|
|
|
if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
140
|
|
|
throw new CantRemoveByDate; |
141
|
|
|
|
142
|
|
|
$schedule = $this->parseToSchedule($schedule); |
143
|
|
|
|
144
|
|
|
if(!($schedule instanceof Model)) |
145
|
|
|
throw (new ModelNotFound)->setValues(Schedule::class); |
146
|
|
|
|
147
|
|
|
if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
148
|
|
|
throw new DoesNotBelong; |
149
|
|
|
|
150
|
|
|
return $schedule->delete(); |
151
|
|
|
} |
152
|
|
|
} |
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.