ScheduleTask::onConstruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the PHALCON-EXT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace PhalconExt\Cli\Task;
13
14
use Ahc\Cli\Input\Command;
15
use Ahc\Cron\Expression;
16
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...
17
use PhalconExt\Di\ProvidesDi;
18
19
class ScheduleTask extends Task
20
{
21
    use ProvidesDi;
22
23
    public function onConstruct()
24
    {
25
        ($console = $this->di('console'))
26
            ->command('schedule:list', 'List scheduled tasks (if any)', false)
27
                ->tap($console)
28
            ->command('schedule:run', 'Run scheduled tasks that are due', true);
29
    }
30
31
    public function listAction()
32
    {
33
        $io = $this->di('interactor');
34
35
        if ([] === $tasks = $this->di('console')->scheduled()) {
36
            $io->infoBgRed('No scheduled tasks', true);
37
38
            return;
39
        }
40
41
        $io->boldGreen('Schedules:', true);
42
43
        $maxLen = \max(\array_map('strlen', \array_keys($tasks)));
44
45
        foreach ($tasks as $taskId => $schedule) {
46
            $io->bold('  ' . \str_pad($taskId, $maxLen + 2))->comment($schedule, true);
47
        }
48
    }
49
50
    public function runAction()
51
    {
52
        $io = $this->di('interactor');
53
54
        if ([] === $tasks =  $this->dueTasks()) {
55
            return $io->infoBgRed('No due tasks for now', true);
56
        }
57
58
        $params = $this->di(Command::class)->values();
59
60
        foreach ($tasks as list($task, $action)) {
61
            $io->line('--------------------', true)
62
                ->yellow($task . ':' . $action, true)
63
                ->line('--------------------', true);
64
65
            $this->di('console')->doHandle(\compact('task', 'action', 'params'));
66
        }
67
    }
68
69
    protected function dueTasks(): array
70
    {
71
        if ([] === $tasks = $this->console->scheduled()) {
72
            return [];
73
        }
74
75
        $dues = [];
76
        $now  = \time();
77
78
        foreach ((new Expression)->filter($tasks, $now) as $taskId) {
79
            $dues[] = \explode(':', $taskId) + ['', 'main'];
80
        }
81
82
        return $dues;
83
    }
84
}
85