Passed
Pull Request — master (#32)
by Jitendra
02:52
created

ScheduleTask   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 32
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dueTasks() 0 17 4
A onConstruct() 0 6 1
A listAction() 0 16 3
A runAction() 0 16 3
1
<?php
2
3
namespace PhalconExt\Cli\Task;
4
5
use Ahc\Cli\Input\Command;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Input\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Ahc\Cron\Expression;
7
use Phalcon\Cli\Task;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Task was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PhalconExt\Di\ProvidesDi;
9
10
class ScheduleTask extends Task
11
{
12
    use ProvidesDi;
13
14
    public function onConstruct()
15
    {
16
        ($console = $this->di('console'))
17
            ->addTask('schedule:list', 'List scheduled tasks (if any)', false)
18
                ->tap($console)
19
            ->addTask('schedule:run', 'Run scheduled tasks that are due', true);
20
    }
21
22
    public function listAction()
23
    {
24
        $io = $this->di('interactor');
25
26
        if ([] === $tasks = $this->di('console')->scheduled()) {
27
            $io->infoBgRed('No scheduled tasks', true);
28
29
            return;
30
        }
31
32
        $io->boldGreen('Schedules:', true);
33
34
        $maxLen = \max(\array_map('strlen', \array_keys($tasks)));
35
36
        foreach ($tasks as $taskId => $schedule) {
37
            $io->bold('  ' . \str_pad($taskId, $maxLen + 2))->comment($schedule, true);
38
        }
39
    }
40
41
    public function runAction()
42
    {
43
        $io = $this->di('interactor');
44
45
        if ([] === $tasks =  $this->dueTasks()) {
46
            return $io->infoBgRed('No due tasks for now', true);
47
        }
48
49
        $params = $this->di(Command::class)->values();
50
51
        foreach ($tasks as list($task, $action)) {
52
            $io->line('--------------------', true)
53
                ->yellow($task . ':' . $action, true)
54
                ->line('--------------------', true);
55
56
            $this->di('console')->doHandle(\compact('task', 'action', 'params'));
57
        }
58
    }
59
60
    protected function dueTasks(): array
61
    {
62
        if ([] === $tasks = $this->console->scheduled()) {
63
            return [];
64
        }
65
66
        $dues = [];
67
        $now  = \time();
68
        $cron = new Expression;
69
70
        foreach ($tasks as $taskId => $schedule) {
71
            if ($cron->isCronDue($schedule, $now)) {
72
                $dues[] = \explode(':', $taskId) + ['', 'main'];
73
            }
74
        }
75
76
        return $dues;
77
    }
78
}
79