Lister   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 37 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of BlitzPHP Tasks.
7
 *
8
 * (c) 2025 Dimitri Sitchet Tomkeu <[email protected]>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13
14
namespace BlitzPHP\Tasks\Commands;
15
16
use BlitzPHP\Tasks\CronExpression;
17
use BlitzPHP\Tasks\Scheduler;
18
use BlitzPHP\Utilities\Date;
19
20
/**
21
 * Répertorie les tâches actuellement planifiées.
22
 */
23
class Lister extends TaskCommand
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected $name = 'tasks:list';
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected $description = 'Répertorie les tâches actuellement configurées pour être exécutées.';
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * @return void
39
     */
40
    public function execute(array $params)
41
    {
42
        helper('preference');
43
44
        if (parametre('tasks.enabled') === false) {
45
            $this->writer->error('WARNING: L\'exécution de la tâche est actuellement désactivée.', true);
46
            $this->writer->write("Pour réactiver les tâches, exécutez\u{a0}: `tasks:enable`", true);
47
            $this->newLine();
48
        }
49
50
        /** @var Scheduler */
51
        $scheduler = service('scheduler');
52
53
        call_user_func(config('tasks.init'), $scheduler);
0 ignored issues
show
Bug introduced by
config('tasks.init') of type BlitzPHP\Config\Config|null is incompatible with the type callable expected by parameter $callback of call_user_func(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        call_user_func(/** @scrutinizer ignore-type */ config('tasks.init'), $scheduler);
Loading history...
54
55
        $tasks = [];
56
57
        foreach ($scheduler->getTasks() as $task) {
58
            /** @var CronExpression */
59
            $cron = service('cronExpression');
60
61
            $nextRun = $cron->nextRun($task->getExpression());
62
            $lastRun = $task->lastRun();
63
64
            $tasks[] = [
65
                'Nom'                 => $task->name ?: $task->getAction(),
66
                'Type'                => $task->getType(),
67
                'Schedule'            => $task->getExpression(),
68
                'Derniere exécution'  => $lastRun instanceof Date ? $lastRun->toDateTimeString() : $lastRun,
69
                'Prochaine exécution' => $nextRun,
70
                // 'runs_in'  => $nextRun->getDate(),
71
            ];
72
        }
73
74
        usort($tasks, static fn ($a, $b) => ($a['Prochaine exécution'] < $b['Prochaine exécution']) ? -1 : 1);
75
76
        $this->table($tasks);
77
    }
78
}
79