MakeTaskCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 85
c 3
b 0
f 0
dl 0
loc 167
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B interact() 0 54 8
A getClassTemplate() 0 3 1
A createClass() 0 5 1
A getTaskBody() 0 21 1
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file MakeEntityCommand.php
34
 *
35
 *  The Make Entity Command class
36
 *
37
 *  @package    Platine\Framework\Console\Command
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Console\Command;
49
50
use Exception;
51
use Platine\Console\Input\Reader;
52
use Platine\Console\Output\Writer;
53
use Platine\Filesystem\Filesystem;
54
use Platine\Framework\App\Application;
55
use Platine\Framework\Console\MakeCommand;
56
use Platine\Framework\Task\Cron;
57
use Platine\Stdlib\Helper\Str;
58
59
/**
60
 * @class MakeTaskCommand
61
 * @package Platine\Framework\Console\Command
62
 */
63
class MakeTaskCommand extends MakeCommand
64
{
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected string $type = 'task';
69
70
    /**
71
     * The name of task
72
     * @var string
73
     */
74
    protected string $name = '';
75
76
    /**
77
     * The task execution expression
78
     * @var string
79
     */
80
    protected string $expression = '* * * * *';
81
82
    /**
83
     * Create new instance
84
     * @param Application $application
85
     * @param Filesystem $filesystem
86
     */
87
    public function __construct(
88
        Application $application,
89
        Filesystem $filesystem
90
    ) {
91
        parent::__construct($application, $filesystem);
92
93
        $this->setName('make:task')
94
               ->setDescription('Command to generate new task class');
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function interact(Reader $reader, Writer $writer): void
101
    {
102
        parent::interact($reader, $writer);
103
104
        $io = $this->io();
105
        $this->name = $io->prompt('Enter the task name', '');
106
107
        $expression = $io->prompt('Enter the cron expression', '* * * * *');
108
        while (true) {
109
            try {
110
                $time = Cron::parse($expression);
111
                if ($time === 0) {
112
                    $expression = $io->prompt(
113
                        'Invalid expression timestamp, please enter the cron expression',
114
                        '* * * * *'
115
                    );
116
                } else {
117
                    break;
118
                }
119
            } catch (Exception $ex) {
120
                $expression = $io->prompt(
121
                    'Invalid expression format, please enter the cron expression',
122
                    '* * * * *'
123
                );
124
            }
125
        }
126
        $this->expression = $expression;
127
128
        $properties = [];
129
130
        $writer->boldYellow('Enter the properties list (empty value to finish):', true);
131
        $value = '';
132
        while ($value !== null) {
133
            $value = $io->prompt('Property full class name', null, null, false);
134
135
            if (!empty($value)) {
136
                $value = trim($value);
137
                if (!class_exists($value) && !interface_exists($value)) {
138
                    $writer->boldWhiteBgRed(sprintf('The class [%s] does not exists', $value), true);
139
                } else {
140
                    $shortClass = $this->getClassBaseName($value);
141
                    $name = Str::camel($shortClass, true);
142
                    //replace "interface", "abstract"
143
                    $nameClean = str_ireplace(['interface', 'abstract'], '', $name);
144
145
                    $properties[$value] = [
146
                        'name' => $nameClean,
147
                        'short' => $shortClass,
148
                    ];
149
                }
150
            }
151
        }
152
153
        $this->properties = $properties;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getClassTemplate(): string
160
    {
161
        return <<<EOF
162
        <?php
163
        
164
        declare(strict_types=1);
165
        
166
        namespace %namespace%;
167
        
168
        use Platine\Framework\Task\TaskInterface;
169
        %uses%
170
171
        /**
172
        * @class %classname%
173
        * @package %namespace%
174
        */
175
        class %classname% implements TaskInterface
176
        {
177
            %properties%
178
            %constructor%
179
        
180
            /**
181
            * {@inheritdoc}
182
            */
183
            public function run(): void
184
            {
185
                %method_body%
186
            }
187
        
188
            %task_body%
189
        }
190
        
191
        EOF;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    protected function createClass(): string
198
    {
199
        $content = parent::createClass();
200
201
        return $this->getTaskBody($content);
202
    }
203
204
    /**
205
     * Return the task body
206
     * @param string $content
207
     * @return string
208
     */
209
    protected function getTaskBody(string $content): string
210
    {
211
        $result = <<<EOF
212
        /**
213
            * {@inheritdoc}
214
            */
215
            public function expression(): string
216
            {
217
                return '$this->expression';
218
            }
219
                    
220
            /**
221
            * {@inheritdoc}
222
            */
223
            public function name(): string
224
            {
225
                return '$this->name';
226
            }
227
        EOF;
228
229
        return str_replace('%task_body%', $result, $content);
230
    }
231
}
232