1
|
|
|
<?php namespace Indatus\Dispatcher\Scheduling; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Dispatcher |
5
|
|
|
* |
6
|
|
|
* (c) Ben Kuhl <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use App; |
13
|
|
|
|
14
|
|
|
abstract class Schedulable |
15
|
|
|
{ |
16
|
|
|
/** @var array $arguments */ |
17
|
|
|
protected $arguments = []; |
18
|
|
|
|
19
|
|
|
/** @var array $options */ |
20
|
|
|
protected $options = []; |
21
|
|
|
|
22
|
|
|
/** @var bool Instantiate a new instance when using args() or opts() */ |
23
|
|
|
protected $instantiateNew = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Define arguments for this schedule when it runs. |
27
|
|
|
* |
28
|
|
|
* @param array $arguments |
29
|
|
|
* |
30
|
|
|
* @return \Indatus\Dispatcher\Scheduling\Schedulable |
31
|
|
|
*/ |
32
|
3 |
View Code Duplication |
public function args(array $arguments) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
// provide a new instance of the scheduler class if this |
35
|
|
|
// is the first method that was called. This allows for |
36
|
|
|
// $scheduler->opts() to return a new instance of the |
37
|
|
|
// scheduler when it's first called |
38
|
3 |
|
if (count($this->options) == 0) { |
39
|
3 |
|
$scheduler = App::make(get_called_class()); |
40
|
3 |
|
$scheduler->setArguments($arguments); |
41
|
|
|
|
42
|
3 |
|
return $scheduler; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
$this->setArguments($arguments); |
46
|
|
|
|
47
|
2 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the arguments for this schedule. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
3 |
|
public function getArguments() |
56
|
|
|
{ |
57
|
3 |
|
return $this->arguments; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the schedule's arguments |
62
|
|
|
* |
63
|
|
|
* This method is only to be used internally by Dispatcher. |
64
|
|
|
* |
65
|
|
|
* @param array $arguments |
66
|
|
|
*/ |
67
|
3 |
|
public function setArguments($arguments) |
68
|
|
|
{ |
69
|
3 |
|
$this->arguments = $arguments; |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Define options for this schedule when it runs. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return \Indatus\Dispatcher\Scheduling\Schedulable |
78
|
|
|
*/ |
79
|
2 |
View Code Duplication |
public function opts(array $options) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
// provide a new instance of the scheduler class if this |
82
|
|
|
// is the first method that was called. This allows for |
83
|
|
|
// $scheduler->opts() to return a new instance of the |
84
|
|
|
// scheduler when it's first called |
85
|
2 |
|
if (count($this->arguments) == 0) { |
86
|
2 |
|
$scheduler = App::make(get_called_class()); |
87
|
2 |
|
$scheduler->setOptions($options); |
88
|
|
|
|
89
|
2 |
|
return $scheduler; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$this->setOptions($options); |
93
|
|
|
|
94
|
2 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the options for this schedule. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
2 |
|
public function getOptions() |
103
|
|
|
{ |
104
|
2 |
|
return $this->options; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the schedule's options |
109
|
|
|
* |
110
|
|
|
* This method is only to be used internally by Dispatcher. |
111
|
|
|
* |
112
|
|
|
* @param array $options |
113
|
|
|
*/ |
114
|
2 |
|
public function setOptions($options) |
115
|
|
|
{ |
116
|
2 |
|
$this->options = $options; |
117
|
2 |
|
$this->setEnvironmentOption(); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Propagate scheduled:run environment |
122
|
|
|
* to scheduled commands, only if 'env' option was not specified |
123
|
|
|
*/ |
124
|
2 |
|
private function setEnvironmentOption() |
125
|
|
|
{ |
126
|
2 |
|
if (!array_key_exists('env', $this->options)) { |
127
|
1 |
|
$this->options = array_merge( |
128
|
1 |
|
$this->options, |
129
|
1 |
|
['env' => App::environment()] |
130
|
1 |
|
); |
131
|
1 |
|
} |
132
|
2 |
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.