Skip to content

Commit 980d9e8

Browse files
authored
refactor(traits) : slim down models by splitting them into traits
1 parent 279543c commit 980d9e8

File tree

8 files changed

+262
-146
lines changed

8 files changed

+262
-146
lines changed

resources/views/tasks/view.blade.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
<li>
2727
<span class="uk-text-muted uk-float-right">Cron Expression</span>
2828
<span class="uk-float-left">
29-
<span>{{$task->expression}}</span>
30-
<span class="uk-margin-left">( <em>{{$task->frequencies->pluck('label')->implode(', ')}}</em> )</span>
29+
<span>{{$task->getCronExpression()}}</span>
3130
</span>
3231
</li>
3332
<li>

src/Frequency.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Studio\Totem;
44

5+
use Studio\Totem\Traits\HasParameters;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Frequency extends Model
89
{
10+
use HasParameters;
11+
912
protected $table = 'task_frequencies';
1013

1114
protected $fillable = [
@@ -20,12 +23,4 @@ public function task()
2023
{
2124
return $this->belongsTo(Task::class);
2225
}
23-
24-
/**
25-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
26-
*/
27-
public function parameters()
28-
{
29-
return $this->hasMany(Parameter::class);
30-
}
3126
}

src/Http/Requests/TaskRequest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function rules()
2626
return [
2727
'description' => 'required',
2828
'command' => 'required',
29-
'expression' => 'required_if:type,expression|cron_expression',
29+
'expression' => 'nullable|required_if:type,expression|cron_expression',
3030
'frequencies' => 'required_if:type,frequency|array',
3131
'notification_email_address' => 'nullable|email',
3232
'notification_phone_number' => 'nullable|digits_between:11,13',
@@ -53,4 +53,18 @@ public function messages()
5353
'notification_slack_webhook.url' => 'Slack Webhook must be a valid url',
5454
];
5555
}
56+
57+
/**
58+
* Get data to be validated from the request.
59+
*
60+
* @return array
61+
*/
62+
protected function validationData()
63+
{
64+
if ($this->input('type') == 'frequency') {
65+
$this->merge(['expression' => null]);
66+
}
67+
68+
return $this->all();
69+
}
5670
}

src/Repositories/EloquentTaskRepository.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,6 @@ public function store(array $input)
8686

8787
$task->fill(array_only($input, $task->getFillable()))->save();
8888

89-
if ($input['type'] == 'frequency') {
90-
foreach ($input['frequencies'] as $_frequency) {
91-
$frequency = $task->frequencies()->create($_frequency);
92-
93-
if (isset($_frequency['parameters'])) {
94-
$frequency->parameters()->createMany($_frequency['parameters']);
95-
}
96-
}
97-
}
98-
9989
Created::dispatch($task);
10090

10191
return $task;
@@ -118,22 +108,6 @@ public function update(array $input, $task)
118108

119109
$task->fill(array_only($input, $task->getFillable()))->save();
120110

121-
if ($input['type'] == 'frequency') {
122-
foreach ($task->frequencies as $frequency) {
123-
if (! in_array($frequency->interval, collect($input['frequencies'])->pluck('interval')->toArray())) {
124-
$frequency->delete();
125-
}
126-
}
127-
foreach ($input['frequencies'] as $_frequency) {
128-
$frequency = $task->frequencies()->updateOrCreate(array_only($_frequency, ['task_id', 'label', 'interval']));
129-
130-
$parameters = isset($_frequency['parameters']) ? $_frequency['parameters'] : [];
131-
foreach ($parameters as $_parameter) {
132-
$frequency->parameters()->updateOrCreate($_parameter);
133-
}
134-
}
135-
}
136-
137111
Updated::dispatch($task);
138112

139113
return $task;
@@ -151,11 +125,6 @@ public function destroy($id)
151125

152126
if ($task) {
153127
Deleted::dispatch($task);
154-
155-
$task->frequencies()->delete();
156-
157-
$task->results()->delete();
158-
159128
$task->delete();
160129

161130
return true;

src/Task.php

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,14 @@
22

33
namespace Studio\Totem;
44

5-
use Closure;
65
use Cron\CronExpression;
76
use Illuminate\Database\Eloquent\Model;
7+
use Studio\Totem\Traits\HasFrequencies;
88
use Illuminate\Notifications\Notifiable;
9-
use Illuminate\Console\Scheduling\ManagesFrequencies;
109

1110
class Task extends Model
1211
{
13-
use Notifiable, ManagesFrequencies;
14-
15-
/**
16-
* The array of filter callbacks.
17-
*
18-
* @var array
19-
*/
20-
protected $filters = [];
21-
22-
/**
23-
* The array of reject callbacks.
24-
*
25-
* @var array
26-
*/
27-
protected $rejects = [];
12+
use Notifiable, HasFrequencies;
2813

2914
/**
3015
* The attributes that are mass assignable.
@@ -75,16 +60,6 @@ public function getUpcomingAttribute()
7560
return CronExpression::factory($this->getCronExpression())->getNextRunDate()->format('Y-m-d H:i:s');
7661
}
7762

78-
/**
79-
* Frequencies Relation.
80-
*
81-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
82-
*/
83-
public function frequencies()
84-
{
85-
return $this->hasMany(Frequency::class, 'task_id', 'id')->with('parameters');
86-
}
87-
8863
/**
8964
* Results Relation.
9065
*
@@ -124,80 +99,4 @@ public function routeNotificationForSlack()
12499
{
125100
return $this->notification_slack_webhook;
126101
}
127-
128-
/**
129-
* Generate a cron expression from frequencies.
130-
*
131-
* @return string
132-
*/
133-
public function getCronExpression()
134-
{
135-
if (! $this->expression) {
136-
$this->expression = '* * * * * *';
137-
138-
foreach ($this->frequencies as $frequency) {
139-
call_user_func_array([$this, $frequency->interval], $frequency->parameters->pluck('value')->toArray());
140-
}
141-
}
142-
143-
return $this->expression;
144-
}
145-
146-
/**
147-
* Get the mutex name for the scheduled task.
148-
*
149-
* @return string
150-
*/
151-
public function getMutexName()
152-
{
153-
return 'logs'.DIRECTORY_SEPARATOR.'schedule-'.sha1($this->expression.$this->command);
154-
}
155-
156-
/**
157-
* Determine if the filters pass for the event.
158-
*
159-
* @param \Illuminate\Contracts\Foundation\Application $app
160-
* @return bool
161-
*/
162-
public function filtersPass($app)
163-
{
164-
foreach ($this->filters as $callback) {
165-
if (! $app->call($callback)) {
166-
return false;
167-
}
168-
}
169-
170-
foreach ($this->rejects as $callback) {
171-
if ($app->call($callback)) {
172-
return false;
173-
}
174-
}
175-
176-
return true;
177-
}
178-
179-
/**
180-
* Register a callback to further filter the schedule.
181-
*
182-
* @param \Closure $callback
183-
* @return $this
184-
*/
185-
public function when(Closure $callback)
186-
{
187-
$this->filters[] = $callback;
188-
189-
return $this;
190-
}
191-
192-
/**
193-
* Schedule the event to run between start and end time.
194-
*
195-
* @param string $startTime
196-
* @param string $endTime
197-
* @return $this
198-
*/
199-
public function between($startTime, $endTime)
200-
{
201-
return $this->when($this->inTimeInterval($startTime, $endTime));
202-
}
203102
}

0 commit comments

Comments
 (0)