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