Passed
Pull Request — master (#32)
by Jitendra
01:59
created

Extension   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 146
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A doHandle() 0 7 2
A addTask() 0 13 3
A schedule() 0 7 2
A initTasks() 0 12 3
A initialize() 0 11 1
A app() 0 3 1
A getTaskParameters() 0 22 4
A scheduled() 0 3 1
A argv() 0 3 1
A getTaskClasses() 0 7 3
A __construct() 0 9 1
A handle() 0 6 1
1
<?php
2
3
namespace PhalconExt\Cli;
4
5
use Ahc\Cli\Application;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Application 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\Cli\Helper\OutputHelper;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Helper\OutputHelper 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...
7
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...
8
use Ahc\Cli\IO\Interactor;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\IO\Interactor 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...
9
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...
10
use Phalcon\DiInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\DiInterface 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...
11
use PhalconExt\Cli\Task\ScheduleTask;
12
use PhalconExt\Di\ProvidesDi;
13
14
trait Extension
15
{
16
    use ProvidesDi;
17
    use MiddlewareTrait;
18
19
    /** @var array Tasks namespaces */
20
    protected $namespaces = [];
21
22
    /** @var array Tasks provided by package already */
23
    protected $factoryTasks = [];
24
25
    /** @var array Scheduled taskIds mapped to schedule time  */
26
    protected $scheduled = [];
27
28
    /** @var array Raw argv sent to handle() [OR read from $_SERVER] */
29
    protected $argv = [];
30
31
    /** @var string */
32
    protected $lastTask;
33
34
    public function __construct(DiInterface $di, string $name, string $version = '0.0.1')
35
    {
36
        parent::__construct($di);
37
38
        $di->setShared('console', $this);
39
        $di->setShared('interactor', Interactor::class);
40
41
        $this->initialize($name, $version);
42
        $this->bindEvents($this);
43
    }
44
45
    protected function initialize(string $name, string $version)
46
    {
47
        $this->app = new Application($name, $version, function () {
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
            return false;
49
        });
50
51
        $this->factoryTasks = [
52
            'schedule' => ScheduleTask::class,
53
        ];
54
55
        $this->initTasks();
56
    }
57
58
    public function app()
59
    {
60
        return $this->app;
61
    }
62
63
    public function argv(): array
64
    {
65
        return $this->argv;
66
    }
67
68
    public function handle(array $argv = null)
69
    {
70
        $this->argv = $argv ?? $_SERVER['argv'];
71
        $parameters = $this->getTaskParameters($this->argv);
72
73
        return $this->doHandle($parameters);
74
    }
75
76
    public function doHandle(array $parameters)
77
    {
78
        if (isset($this->namespaces[$parameters['task']])) {
79
            $parameters['task'] = $this->namespaces[$parameters['task']];
80
        }
81
82
        return parent::handle($parameters);
83
    }
84
85
    public function addTask(string $task, string $descr = '', bool $allowUnknown = false): Command
86
    {
87
        $this->lastTask = $taskId = \str_ireplace(['task', 'action'], '', $task);
88
89
        if (\strpos($task, ':main')) {
90
            $alias = \str_replace(':main', '', $taskId);
91
        }
92
93
        if (\strpos($task, ':') === false) {
94
            $alias = $taskId . ':main';
95
        }
96
97
        return $this->app->command($taskId, $descr, $allowUnknown, $alias ?? '');
98
    }
99
100
    public function schedule(string $cronExpr, string $taskId = ''): self
101
    {
102
        $taskId = $taskId ?: $this->lastTask;
103
104
        $this->scheduled[$taskId] = $cronExpr;
105
106
        return $this;
107
    }
108
109
    public function scheduled(): array
110
    {
111
        return $this->scheduled;
112
    }
113
114
    protected function getTaskParameters(array $argv)
115
    {
116
        $taskAction = [];
117
        \array_shift($argv);
118
119
        foreach ($argv as $i => $value) {
120
            if ($value[0] === '-' || isset($taskAction[1])) {
121
                break;
122
            }
123
124
            $taskAction = \array_merge($taskAction, \explode(':', $value, 2));
125
            unset($argv[$i]);
126
        }
127
128
        // Respect phalcon default.
129
        $taskAction += ['main', 'main'];
130
131
        return [
132
            'task'   => $taskAction[0],
133
            'action' => $taskAction[1],
134
            // For BC, still send params to handle()
135
            'params' => \array_values($argv),
136
        ];
137
    }
138
139
    public function initTasks()
140
    {
141
        foreach ($this->getTaskClasses() as $name => $class) {
142
            if (!$this->di()->has($class)) {
143
                // Force load!
144
                $this->di($class);
145
            }
146
147
            $this->namespaces[$name] = \preg_replace('#Task$#', '', $class);
148
        }
149
150
        return $this;
151
    }
152
153
    protected function getTaskClasses(): array
154
    {
155
        if ($tasks = $this->di('config')->path('console.tasks')) {
156
            $tasks = $tasks->toArray();
157
        }
158
159
        return $this->factoryTasks + ($tasks ?: []);
160
    }
161
}
162