AbstractCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 133
ccs 45
cts 54
cp 0.8333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B listTasks() 0 68 6
B getTaskPlanner() 0 25 4
A loadTaskPlanner() 0 14 3
1
<?php
2
namespace Mistletoe\Application\Commands;
3
4
use Mistletoe\CronSchedule;
5
use Mistletoe\TaskBag;
6
use Mistletoe\TaskPlanner;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class AbstractCommand
13
 * @package Mistletoe\Application\Commands
14
 */
15
abstract class AbstractCommand extends Command
16
{
17
    /** @var TaskPlanner */
18
    protected $taskPlanner;
19
20
    /**
21
     * @param OutputInterface $output
22
     * @param $tasks
23
     * @return Table
24
     */
25 1
    protected function listTasks(OutputInterface $output, $tasks)
26
    {
27 1
        $rows = [];
28
29 1
        $verbose = ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE);
30
31
        /**
32
         * @var TaskBag $task
33
         */
34 1
        $i = 1;
35 1
        foreach ($tasks as $task) {
36 1
            $row = [];
37
38
            // Number
39 1
            $row[] = $i;
40
41
            // Name
42 1
            $row[] = $task->getTask();
43
44 1
            if ($verbose) {
45
                // Schedule
46
                $schedule = CronSchedule::fromCronString((string)$task->getCronExpression());
47
                $row[] = $schedule->asNaturalLanguage();
48
            }
49
50
            // Cron
51 1
            $row[] = (string)$task->getCronExpression();
52
53
            // Next Due
54 1
            $row[] = (string)$task->getCronExpression()->getNextRunDate()->format('H:i');
55
56
            // Environment
57 1
            $environments = $task->getEnvironments();
58 1
            if ($environments === ['PRODUCTION', 'DEVELOPMENT']) {
59 1
                $row[] = 'ALL';
60 1
            } else {
61
                $row[] = implode(', ', $environments);
62
            }
63
64
            // Followed By
65 1
            $row[] = implode(" -> ", array_map(function ($task) {
66 1
                if ($task instanceof \Mistletoe\Command) {
67
                    return $task->getCommand();
68
                } else {
69 1
                    return $task;
70
                }
71 1
            }, $task->getFollowedBy()));
72
73 1
            $rows[] = $row;
74
75 1
            $i++;
76 1
        }
77
78 1
        $table = new Table($output);
79
80 1
        if ($verbose) {
81
            $headers = ['#', 'Task Name', 'Schedule', 'Cron', 'Next Due', 'Environment', 'Followed By'];
82
        } else {
83 1
            $headers = ['#', 'Task Name', 'Cron', 'Next Due', 'Environment', 'Followed By'];
84
        }
85
86
        $table
87 1
            ->setHeaders($headers)
88 1
            ->setRows($rows);
89 1
        $table->render();
90
91 1
        return $table;
92
    }
93
94
    /**
95
     * @param string|null $path
96
     * @return TaskPlanner
97
     * @throws \Exception
98
     */
99 5
    public function getTaskPlanner($path = null)
100
    {
101
        // Return a cached instance
102 5
        if ($this->taskPlanner) {
103
            return $this->taskPlanner;
104
        }
105
106
        // Load the config file (which returns a TaskPlanner)
107 5
        $cwd = getcwd();
108
109 5
        if ($path !== null) {
110
            // Were we were given a valid relative path?
111
            // If not, then we must assume it is an absolute path
112 4
            if (file_exists("{$cwd}/{$path}")) {
113 1
                $path = "{$cwd}/{$path}";
114 1
            }
115
116 4
        } else {
117
            // If no path specified, try the default
118 1
            $path = "{$cwd}/mistletoe.php";
119
        }
120
121
        // Load the TaskPlanner and cache before returning
122 5
        return $this->taskPlanner = $this->loadTaskPlanner($path);
123
    }
124
125
    /**
126
     * Validate a return a TaskPlanner instance from the path
127
     * of a mistletoe config file.
128
     *
129
     * @param $path
130
     * @return TaskPlanner
131
     * @throws \Exception
132
     */
133 5
    private function loadTaskPlanner($path)
134
    {
135 5
        if (file_exists($path)) {
136 4
            $planner = include($path);
137
138 4
            if (!$planner instanceof TaskPlanner) {
139
                throw new \Exception("Config file $path did not return a valid TaskPlanner.");
140
            }
141
142 4
            return $planner;
143
        }
144
145 1
        throw new \Exception("Mistletoe config file: {$path} not found.");
146
    }
147
}
148