Passed
Push — master ( 566365...3d5699 )
by Vinicius Lourenço
04:03
created
src/Scheduler/Contracts/SchedulerModelTrait.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -11,28 +11,28 @@  discard block
 block discarded – undo
11 11
 interface SchedulerModelInterface
12 12
 {
13 13
 	/**
14
-     * Define an inverse one-to-one or many relationship.
15
-     *
16
-     * @param  string  $related
17
-     * @param  string  $foreignKey
18
-     * @param  string  $ownerKey
19
-     * @param  string  $relation
20
-     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21
-     */
14
+	 * Define an inverse one-to-one or many relationship.
15
+	 *
16
+	 * @param  string  $related
17
+	 * @param  string  $foreignKey
18
+	 * @param  string  $ownerKey
19
+	 * @param  string  $relation
20
+	 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21
+	 */
22 22
 	abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null);
23 23
 
24 24
 	/**
25
-     * Get the value of the model's primary key.
26
-     *
27
-     * @return mixed
28
-     */
25
+	 * Get the value of the model's primary key.
26
+	 *
27
+	 * @return mixed
28
+	 */
29 29
 	abstract public function getKey();
30 30
 
31 31
 	/**
32
-     * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait].
33
-     *
34
-     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35
-     */
32
+	 * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait].
33
+	 *
34
+	 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35
+	 */
36 36
 	public function schedules();
37 37
 
38 38
 	/**
@@ -55,8 +55,8 @@  discard block
 block discarded – undo
55 55
 	 *
56 56
 	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
57 57
 	 *                          que precisem ter uma certa duração.
58
-     * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
59
-     *                                         Se for nulo, ele busca a referencia da config.
58
+	 * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
59
+	 *                                         Se for nulo, ele busca a referencia da config.
60 60
 	 * @return array
61 61
 	 */
62 62
 	public function availableToday($duration = 0, $openingTime = null);
@@ -67,8 +67,8 @@  discard block
 block discarded – undo
67 67
 	 * @param  string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca.
68 68
 	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
69 69
 	 *                          que precisem ter uma certa duração.
70
-     * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
71
-     *                                         Se for nulo, ele busca a referencia da config.
70
+	 * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
71
+	 *                                         Se for nulo, ele busca a referencia da config.
72 72
 	 * @return array
73 73
 	 */
74 74
 	public function availableOn($date, $duration = 0, $openingTime = null);
Please login to merge, or discard this patch.
src/Scheduler/Traits/SchedulerModelTrait.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -28,28 +28,28 @@  discard block
 block discarded – undo
28 28
 trait SchedulerModelTrait
29 29
 {
30 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
-     */
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 39
 	abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null);
40 40
 
41 41
 	/**
42
-     * Get the value of the model's primary key.
43
-     *
44
-     * @return mixed
45
-     */
42
+	 * Get the value of the model's primary key.
43
+	 *
44
+	 * @return mixed
45
+	 */
46 46
 	abstract public function getKey();
47 47
 
48 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
-     */
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 53
 	public function schedules()
54 54
 	{
55 55
 		return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id');
@@ -96,8 +96,8 @@  discard block
 block discarded – undo
96 96
 	 *
97 97
 	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
98 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.
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 101
 	 * @return array
102 102
 	 */
103 103
 	public function availableToday($duration = 0, $openingTime = null)
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
 	 * @param  string|\Carbon\Carbon $date Data para o qual ele irá fazer a busca.
112 112
 	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
113 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.
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 116
 	 * @return array
117 117
 	 */
118 118
 	public function availableOn($date, $duration = 0, $openingTime = null)
Please login to merge, or discard this patch.
src/Scheduler/Models/Schedule.php 1 patch
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -19,64 +19,64 @@
 block discarded – undo
19 19
  */
20 20
 class Schedule extends Model
21 21
 {
22
-    use SoftDeletes;
22
+	use SoftDeletes;
23 23
 
24 24
 	/**
25
-     * Os atributos que podem ser atribuíveis em massa.
26
-     *
27
-     * @var array
28
-     */
29
-    protected $fillable = [
30
-    	'model_type', 'model_id', 'start_at', 'end_at', 'status'
31
-    ];
25
+	 * Os atributos que podem ser atribuíveis em massa.
26
+	 *
27
+	 * @var array
28
+	 */
29
+	protected $fillable = [
30
+		'model_type', 'model_id', 'start_at', 'end_at', 'status'
31
+	];
32 32
 
33
-    /**
34
-     * Os atributos que devem ser transformados para data.
35
-     *
36
-     * @var array
37
-     */
38
-    protected $dates = [
39
-    	'start_at', 'end_at', 'deleted_at'
40
-    ];
33
+	/**
34
+	 * Os atributos que devem ser transformados para data.
35
+	 *
36
+	 * @var array
37
+	 */
38
+	protected $dates = [
39
+		'start_at', 'end_at', 'deleted_at'
40
+	];
41 41
 
42
-    /**
43
-     * Seta um status para o horário agendado.
44
-     *
45
-     * @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário.
46
-     */
47
-    public function setStatus($name)
48
-    {
49
-    	$this->fill($this->parseStatusKey($name))->save();
50
-    }
42
+	/**
43
+	 * Seta um status para o horário agendado.
44
+	 *
45
+	 * @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário.
46
+	 */
47
+	public function setStatus($name)
48
+	{
49
+		$this->fill($this->parseStatusKey($name))->save();
50
+	}
51 51
 
52
-    /**
53
-     * Retorna o ID do status caso passem o nome do status.
54
-     *
55
-     * @param  int|string $key ID ou o nome do status.
56
-     * @return array
57
-     */
58
-    public function parseStatusKey($key)
59
-    {
60
-    	if(is_int($key))
61
-    		return ['status' => $key];
52
+	/**
53
+	 * Retorna o ID do status caso passem o nome do status.
54
+	 *
55
+	 * @param  int|string $key ID ou o nome do status.
56
+	 * @return array
57
+	 */
58
+	public function parseStatusKey($key)
59
+	{
60
+		if(is_int($key))
61
+			return ['status' => $key];
62 62
 
63
-    	$status = ScheduleStatus::where('name', $key)->first();
63
+		$status = ScheduleStatus::where('name', $key)->first();
64 64
 
65
-    	if(is_null($status))
66
-    		throw (new ModelNotFound)->setValues(ScheduleStatus::class);
65
+		if(is_null($status))
66
+			throw (new ModelNotFound)->setValues(ScheduleStatus::class);
67 67
 
68
-    	return ['status' => $status->id];
69
-    }
68
+		return ['status' => $status->id];
69
+	}
70 70
 
71
-    /**
72
-     * Escopo de uma consulta que busca horarios pela data de início.
73
-     *
74
-     * @param \Illuminate\Database\Eloquent\Builder $query
75
-     * @param \Carbon\Carbon|string $start_at
76
-     * @return \Illuminate\Database\Eloquent\Builder
77
-     */
78
-    public function scopeByStartAt($query, $start_at)
79
-    {
80
-        return $query->where('start_at', $start_at);
81
-    }
71
+	/**
72
+	 * Escopo de uma consulta que busca horarios pela data de início.
73
+	 *
74
+	 * @param \Illuminate\Database\Eloquent\Builder $query
75
+	 * @param \Carbon\Carbon|string $start_at
76
+	 * @return \Illuminate\Database\Eloquent\Builder
77
+	 */
78
+	public function scopeByStartAt($query, $start_at)
79
+	{
80
+		return $query->where('start_at', $start_at);
81
+	}
82 82
 }
83 83
\ No newline at end of file
Please login to merge, or discard this patch.
src/Scheduler/Scheduler.php 1 patch
Indentation   +112 added lines, -112 removed lines patch added patch discarded remove patch
@@ -14,116 +14,116 @@
 block discarded – undo
14 14
 
15 15
 class Scheduler
16 16
 {
17
-    /**
18
-     * Laravel application
19
-     *
20
-     * @var \Illuminate\Foundation\Application
21
-     */
22
-    public $app;
23
-
24
-    /**
25
-     * Create a new confide instance.
26
-     *
27
-     * @param \Illuminate\Foundation\Application $app
28
-     *
29
-     * @return void
30
-     */
31
-    public function __construct($app)
32
-    {
33
-        $this->app = $app;
34
-    }
35
-
36
-    /**
37
-     * Escopo de uma consulta que busca horarios pela data de início.
38
-     *
39
-     * @param string $model_type
40
-     * @param string|\Carbon\Carbon $start_at
41
-     * @param string|\Carbon\Carbon $end_at
42
-     * @return bool
43
-     */
44
-    public function hasScheduleBetween($model_type, $start_at, $end_at)
45
-    {
46
-        if(!Config::get('scheduler.enable_schedule_conflict'))
47
-            return false;
48
-
49
-        return !is_null(
50
-            Schedule::latest()
51
-                ->where('model_type', $model_type)
52
-                ->where('start_at', '>=', $start_at)
53
-                ->where('end_at', '<=', $end_at)
54
-                ->first()
55
-        );
56
-    }
57
-
58
-    /**
59
-     * Retorna os horários disponiveis hoje para uma determinada model.
60
-     * .
61
-     * @param  string  $model_type Tipo da model
62
-     * @param  int    $duration Serve para facilitar na hora de buscar horários livres
63
-     *                          que precisem ter uma certa duração.
64
-     * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
65
-     *                                         Se for nulo, ele busca a referencia da config.
66
-     * @return array
67
-     */
68
-    public function availableToday($model_type, $duration, $openingTime = null)
69
-    {
70
-        return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime);
71
-    }
72
-
73
-    /**
74
-     * Retorna os horários disponiveis em um determinado dia para uma certa model.
75
-     *
76
-     * @param  string  $model_type Tipo da model
77
-     * @param  \Carbon\Carbon $today Data para o qual ele irá fazer a busca.
78
-     * @param  int    $durationMinutes Serve para facilitar na hora de buscar horários livres
79
-     *                          que precisem ter uma certa duração.
80
-     * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
81
-     *                                         Se for nulo, ele busca a referencia da config.
82
-     * @return array
83
-     */
84
-    public function availableOn($model_type, $today, $durationMinutes, $openingTime = null)
85
-    {
86
-        //TODO: Melhorar a performance desse método.
87
-        if(is_null($openingTime))
88
-            $openingTime = Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today);
89
-
90
-        $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today);
91
-
92
-        $livres = [];
93
-        $today = Carbon::parse($today->toDateString());
94
-        while($openingTime <= $closingTime)
95
-        {
96
-            $add = true;
97
-
98
-            foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) {
99
-            	if($schedule->model_type != $model_type)
100
-            		continue;
101
-
102
-                $start = Carbon::parse($schedule->start_at);
103
-                $begin = Carbon::parse($start->toDateString());
104
-
105
-                if($begin->greaterThan($today))
106
-                    break;
107
-
108
-                if($begin->notEqualTo($today))
109
-                    continue;
110
-
111
-                $end = Carbon::parse($schedule->end_at);
112
-                if($start <= Carbon::parse($openingTime->toDateTimeString())
113
-                && $end >= Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes))
114
-                    $add = false;
115
-            }
116
-
117
-            $endTime = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes);
118
-            if($add && $endTime->lessThanOrEqualTo($closingTime))
119
-                $livres[] = [
120
-                    'start_at' => Carbon::parse($openingTime->toDateTimeString()),
121
-                    'end_at' => $endTime
122
-                ];
123
-
124
-            $openingTime->addMinutes($durationMinutes);
125
-        }
126
-
127
-        return $livres;
128
-    }
17
+	/**
18
+	 * Laravel application
19
+	 *
20
+	 * @var \Illuminate\Foundation\Application
21
+	 */
22
+	public $app;
23
+
24
+	/**
25
+	 * Create a new confide instance.
26
+	 *
27
+	 * @param \Illuminate\Foundation\Application $app
28
+	 *
29
+	 * @return void
30
+	 */
31
+	public function __construct($app)
32
+	{
33
+		$this->app = $app;
34
+	}
35
+
36
+	/**
37
+	 * Escopo de uma consulta que busca horarios pela data de início.
38
+	 *
39
+	 * @param string $model_type
40
+	 * @param string|\Carbon\Carbon $start_at
41
+	 * @param string|\Carbon\Carbon $end_at
42
+	 * @return bool
43
+	 */
44
+	public function hasScheduleBetween($model_type, $start_at, $end_at)
45
+	{
46
+		if(!Config::get('scheduler.enable_schedule_conflict'))
47
+			return false;
48
+
49
+		return !is_null(
50
+			Schedule::latest()
51
+				->where('model_type', $model_type)
52
+				->where('start_at', '>=', $start_at)
53
+				->where('end_at', '<=', $end_at)
54
+				->first()
55
+		);
56
+	}
57
+
58
+	/**
59
+	 * Retorna os horários disponiveis hoje para uma determinada model.
60
+	 * .
61
+	 * @param  string  $model_type Tipo da model
62
+	 * @param  int    $duration Serve para facilitar na hora de buscar horários livres
63
+	 *                          que precisem ter uma certa duração.
64
+	 * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
65
+	 *                                         Se for nulo, ele busca a referencia da config.
66
+	 * @return array
67
+	 */
68
+	public function availableToday($model_type, $duration, $openingTime = null)
69
+	{
70
+		return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime);
71
+	}
72
+
73
+	/**
74
+	 * Retorna os horários disponiveis em um determinado dia para uma certa model.
75
+	 *
76
+	 * @param  string  $model_type Tipo da model
77
+	 * @param  \Carbon\Carbon $today Data para o qual ele irá fazer a busca.
78
+	 * @param  int    $durationMinutes Serve para facilitar na hora de buscar horários livres
79
+	 *                          que precisem ter uma certa duração.
80
+	 * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres.
81
+	 *                                         Se for nulo, ele busca a referencia da config.
82
+	 * @return array
83
+	 */
84
+	public function availableOn($model_type, $today, $durationMinutes, $openingTime = null)
85
+	{
86
+		//TODO: Melhorar a performance desse método.
87
+		if(is_null($openingTime))
88
+			$openingTime = Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today);
89
+
90
+		$closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today);
91
+
92
+		$livres = [];
93
+		$today = Carbon::parse($today->toDateString());
94
+		while($openingTime <= $closingTime)
95
+		{
96
+			$add = true;
97
+
98
+			foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) {
99
+				if($schedule->model_type != $model_type)
100
+					continue;
101
+
102
+				$start = Carbon::parse($schedule->start_at);
103
+				$begin = Carbon::parse($start->toDateString());
104
+
105
+				if($begin->greaterThan($today))
106
+					break;
107
+
108
+				if($begin->notEqualTo($today))
109
+					continue;
110
+
111
+				$end = Carbon::parse($schedule->end_at);
112
+				if($start <= Carbon::parse($openingTime->toDateTimeString())
113
+				&& $end >= Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes))
114
+					$add = false;
115
+			}
116
+
117
+			$endTime = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes);
118
+			if($add && $endTime->lessThanOrEqualTo($closingTime))
119
+				$livres[] = [
120
+					'start_at' => Carbon::parse($openingTime->toDateTimeString()),
121
+					'end_at' => $endTime
122
+				];
123
+
124
+			$openingTime->addMinutes($durationMinutes);
125
+		}
126
+
127
+		return $livres;
128
+	}
129 129
 }
130 130
\ No newline at end of file
Please login to merge, or discard this patch.