ScheduleService::isDue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php namespace Indatus\Dispatcher\Drivers\DateTime;
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
use Exception;
14
use Indatus\Dispatcher\Scheduling\Schedulable;
15
use Indatus\Dispatcher\Scheduling\ScheduledCommandInterface;
16
17
class ScheduleService extends \Indatus\Dispatcher\Services\ScheduleService
18
{
19
    /** @var \Indatus\Dispatcher\Table */
20
    protected $table;
21
22
    /**
23
     * Determine if a schedule is due to be run.
24
     *
25
     * @param \Indatus\Dispatcher\Scheduling\Schedulable $scheduler
26
     *
27
     * @return bool
28
     */
29 3
    public function isDue(Schedulable $scheduler)
30
    {
31
        /** @var \Indatus\Dispatcher\Drivers\DateTime\ScheduleInterpreter $interpreter */
32 3
        $interpreter = App::make('Indatus\Dispatcher\Drivers\DateTime\ScheduleInterpreter', [$scheduler]);
33
34
        /** @var \Illuminate\Contracts\Logging\Log $logger */
35 3
        $logger = App::make('Illuminate\Contracts\Logging\Log');
36
37
        try {
38 3
            return $interpreter->isDue();
39 1
        } catch (Exception $e) {
40 1
            $logger->error($e);
41
        }
42
43 1
        return false;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 1
    public function printSummary()
50
    {
51 1
        $this->table = App::make('Indatus\Dispatcher\Table');
52
53
        $headers = [
54 1
            'Environment(s)',
55 1
            'Name',
56 1
            'Args/Opts',
57 1
            'Month',
58 1
            'Week',
59 1
            'Day of Month',
60 1
            'Day of Week',
61 1
            'Hour',
62 1
            'Minute',
63 1
            'Run as',
64 1
        ];
65
66 1
        $this->table->setHeaders($headers);
67
        /** @var \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $command */
68 1
        foreach ($this->getScheduledCommands() as $command) {
69
            /** @var \Indatus\Dispatcher\Scheduling\Schedulable $scheduler */
70 1
            $scheduler = $command->schedule(App::make('Indatus\Dispatcher\Scheduling\Schedulable'));
71
72
            //if there's only one schedule, print just the command
73 1
            if (!is_array($scheduler)) {
74 1
                $this->printCommand($command, $scheduler);
75 1
            } else {
76 1
                if ($this->printCommandLabel($command)) {
77
                    /** @var \Indatus\Dispatcher\Scheduling\Schedulable $schedule */
78 1
                    foreach ($scheduler as $schedule) {
79 1
                        $this->printSchedule($command, $schedule);
80 1
                    }
81 1
                }
82
            }
83 1
        }
84
85 1
        $this->table->display();
86 1
    }
87
88 1
    protected function printCommand(ScheduledCommandInterface $command, Schedulable $scheduler)
89
    {
90 1
        $this->table->addRow([
91 1
                is_array($command->environment()) ? implode(',', $command->environment()) : $command->environment(),
92 1
                $command->getName(),
93 1
                '',
94 1
                $scheduler->getScheduleMonth(),
95 1
                $scheduler->getScheduleWeek(),
96 1
                $scheduler->getScheduleDayOfMonth(),
97 1
                $scheduler->getScheduleDayOfWeek(),
98 1
                $scheduler->getScheduleHour(),
99 1
                $scheduler->getScheduleMinute(),
100 1
                $command->user(),
101 1
            ]);
102 1
    }
103
104 1
    protected function printCommandLabel(ScheduledCommandInterface $command)
105
    {
106 1
        $this->table->addRow([
107 1
                is_array($command->environment()) ? implode(',', $command->environment()) : $command->environment(),
108 1
                $command->getName(),
109 1
                '',
110 1
                '',
111 1
                '',
112 1
                '',
113 1
                '',
114 1
                '',
115 1
                '',
116 1
                $command->user(),
117 1
            ]);
118
119 1
        return true;
120
    }
121
122 1
    protected function printSchedule(ScheduledCommandInterface $command, Schedulable $scheduler)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124 1
        $commandService = App::make('Indatus\Dispatcher\Services\CommandService');
125
126 1
        $arguments = $commandService->prepareArguments($scheduler->getArguments());
127 1
        $options = $commandService->prepareOptions($scheduler->getOptions());
128
129 1
        $this->table->addRow([
130 1
                '',
131 1
                '',
132 1
                trim($arguments).' '.$options,
133 1
                $scheduler->getScheduleMonth(),
134 1
                $scheduler->getScheduleWeek(),
135 1
                $scheduler->getScheduleDayOfMonth(),
136 1
                $scheduler->getScheduleDayOfWeek(),
137 1
                $scheduler->getScheduleHour(),
138 1
                $scheduler->getScheduleMinute(),
139 1
                '',
140 1
            ]);
141 1
    }
142
}
143