Passed
Push — master ( 1402e3...740cbb )
by Vinicius Lourenço
03:23
created

SchedulerModelTrait::parseToCarbon()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
c 0
b 0
f 0
rs 9.6111
cc 5
nc 4
nop 2
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
58
	 *                                                  ou em int(sendo considerado os minutos de duração).
59
	 * @param  int|null $status	Status desse horário ao ser agendado.
60
	 * @return \H4ad\Scheduler\Models\Schedule
61
	 */
62
	public function addSchedule($start_at, $end_at = null, $status = null)
63
	{
64
		$data = Scheduler::validateSchedule(self::class, $start_at, $end_at, $status);
0 ignored issues
show
Unused Code introduced by
The call to H4ad\Scheduler\Facades\S...ler::validateSchedule() has too many arguments starting with $status. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
		/** @scrutinizer ignore-call */ 
65
  $data = Scheduler::validateSchedule(self::class, $start_at, $end_at, $status);

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.

Loading history...
65
		$data['model_id'] = $this->getKey();
66
67
		return Schedule::create($data);
68
	}
69
70
	/**
71
	 * Exibe uma lista dos horários do dia de hoje.
72
	 *
73
	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
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.
77
	 * @return array
78
	 */
79
	public function availableToday($duration = 0, $openingTime = null)
80
	{
81
		return Scheduler::availableToday(self::class, $duration, $openingTime);
82
	}
83
84
	/**
85
	 * Lista os horários livres em um determinado dia.
86
	 *
87
	 * @param  string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca.
88
	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
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.
92
	 * @return array
93
	 */
94
	public function availableOn($date, $duration = 0, $openingTime = null)
95
	{
96
		return Scheduler::availableOn(self::class, $date, $duration, $openingTime);
97
	}
98
99
	/**
100
	 * Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado.
101
	 * Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção
102
	 * se for tentado remover um horário agendado pela data de quando foi marcado.
103
	 *
104
	 * @param  int|string|\Carbon\Carbon $schedule    Horário agendado.
105
	 * @return bool|null
106
	 *
107
	 * @throws \H4ad\Scheduler\Exceptions\DoesNotBelong
108
	 * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate
109
	 * @throws \H4ad\Scheduler\Exceptions\ModelNotFound
110
	 */
111
	public function removeSchedule($schedule)
112
	{
113
		if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule))
114
			throw new CantRemoveByDate;
115
116
		$schedule = Scheduler::parseToSchedule($schedule);
117
118
		if(!($schedule instanceof Model))
119
			throw (new ModelNotFound)->setValues(Schedule::class);
120
121
		if($schedule->model_type != self::class || $schedule->model_id != $this->getKey())
122
			throw new DoesNotBelong;
123
124
		return $schedule->delete();
125
	}
126
}