Completed
Push — master ( 56e121...0ac51f )
by Timur
02:34
created

CreateJobCommand::runnable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Laravel\Forge\Jobs\Commands;
4
5
use InvalidArgumentException;
6
7
class CreateJobCommand extends JobCommand
8
{
9
    /**
10
     * Determines if command can run.
11
     *
12
     * @return bool
13
     */
14
    public function runnable()
15
    {
16
        if ($this->getPayloadData('frequency') !== 'custom') {
17
            return true;
18
        }
19
20
        // If we're using 'custom' frequency,
21
        // all listed fields are required.
22
23
        return $this->hasPayloadData(['minute', 'hour', 'day', 'month', 'weekday']);
24
    }
25
26
    /**
27
     * Set job command.
28
     *
29
     * @param string $command
30
     *
31
     * @return static
32
     */
33
    public function schedule(string $command)
34
    {
35
        return $this->attachPayload('command', $command);
36
    }
37
38
    /**
39
     * Set job user.
40
     *
41
     * @param string $user
42
     *
43
     * @return static
44
     */
45
    public function runningAs(string $user)
46
    {
47
        return $this->attachPayload('user', $user);
48
    }
49
50
    /**
51
     * Indicates that job should run every minute.
52
     *
53
     * @return static
54
     */
55
    public function everyMinute()
56
    {
57
        return $this->attachPayload('frequency', 'minutely');
58
    }
59
60
    /**
61
     * Indicates that job should run every hour.
62
     *
63
     * @return static
64
     */
65
    public function hourly()
66
    {
67
        return $this->attachPayload('frequency', 'hourly');
68
    }
69
70
    /**
71
     * Indicates that job should run every day at midnight.
72
     *
73
     * @return static
74
     */
75
    public function nightly()
76
    {
77
        return $this->attachPayload('frequency', 'nightly');
78
    }
79
80
    /**
81
     * Indicates that job should run every week.
82
     *
83
     * @return static
84
     */
85
    public function weekly()
86
    {
87
        return $this->attachPayload('frequency', 'weekly');
88
    }
89
90
    /**
91
     * Indicates that job should run every month.
92
     *
93
     * @return static
94
     */
95
    public function monthly()
96
    {
97
        return $this->attachPayload('frequency', 'monthly');
98
    }
99
100
    /**
101
     * Schedule job at hour:minute.
102
     *
103
     * @param string $time
104
     *
105
     * @throws \InvalidArgumentException
106
     *
107
     * @return static
108
     */
109
    public function atTime(string $time)
110
    {
111
        $exploded = explode(':', $time);
112
113
        if (sizeof($exploded) !== 2) {
114
            throw new InvalidArgumentException('Given argument "'.$time.'" is not a valid time.');
115
        }
116
117
        list($hour, $minute) = $exploded;
118
119
        return $this->attachPayload('frequency', 'custom')
120
            ->attachPayload('hour', $hour)
121
            ->attachPayload('minute', $minute);
122
    }
123
124
    /**
125
     * Schedule job at given day.
126
     *
127
     * @param string|int $day
128
     *
129
     * @return static
130
     */
131
    public function atDay($day)
132
    {
133
        return $this->attachPayload('frequency', 'custom')
134
            ->attachPayload('day', $day);
135
    }
136
137
    /**
138
     * Schedule job at given month.
139
     *
140
     * @param string|int $month
141
     *
142
     * @return static
143
     */
144
    public function atMonth($month)
145
    {
146
        return $this->attachPayload('frequency', 'custom')
147
            ->attachPayload('month', $month);
148
    }
149
150
    /**
151
     * Schedule job at given weekday.
152
     *
153
     * @param string|int $weekday
154
     *
155
     * @return static
156
     */
157
    public function atWeekday($weekday)
158
    {
159
        return $this->attachPayload('frequency', 'custom')
160
            ->attachPayload('weekday', $weekday);
161
    }
162
}
163