Passed
Push — develop ( 39e219...b400fa )
by nguereza
03:00
created

MakeTaskCommand::interact()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 40
rs 8.9137
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   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Console\Command;
49
50
use Platine\Console\Input\Reader;
51
use Platine\Console\Output\Writer;
52
use Platine\Filesystem\Filesystem;
53
use Platine\Framework\App\Application;
54
use Platine\Framework\Console\MakeCommand;
55
use Platine\Framework\Task\Cron;
56
use Platine\Stdlib\Helper\Str;
57
58
/**
59
 * @class MakeTaskCommand
60
 * @package Platine\Framework\Console\Command
61
 */
62
class MakeTaskCommand extends MakeCommand
63
{
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected string $type = 'task';
68
69
    /**
70
     * The name of task
71
     * @var string
72
     */
73
    protected string $name = '';
74
75
    /**
76
     * The task execution expression
77
     * @var string
78
     */
79
    protected string $expression = '* * * * *';
80
81
    /**
82
     * Create new instance
83
     * @param Application $application
84
     * @param Filesystem $filesystem
85
     */
86
    public function __construct(
87
        Application $application,
88
        Filesystem $filesystem
89
    ) {
90
        parent::__construct($application, $filesystem);
91
        $this->setName('make:task')
92
               ->setDescription('Command to generate new task class');
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function interact(Reader $reader, Writer $writer): void
99
    {
100
        parent::interact($reader, $writer);
101
102
103
        $io = $this->io();
104
        $this->name = $io->prompt('Enter the task name', '');
105
106
107
        $expression = $io->prompt('Enter the cron expression', '* * * * *');
108
        while (Cron::parse($expression) === 0) {
109
            $expression = $io->prompt('Invalid expression, please enter the cron expression', '* * * * *');
110
        }
111
        $this->expression = $expression;
112
113
        $properties = [];
114
115
        $writer->boldYellow('Enter the properties list (empty value to finish):', true);
116
        $value = '';
117
        while ($value !== null) {
118
            $value = $io->prompt('Property full class name', null, null, false);
119
120
            if (!empty($value)) {
121
                $value = trim($value);
122
                if (!class_exists($value) && !interface_exists($value)) {
123
                    $writer->boldWhiteBgRed(sprintf('The class [%s] does not exists', $value), true);
124
                } else {
125
                    $shortClass = basename($value);
126
                    $name = Str::camel($shortClass, true);
127
                    //replace"interface", "abstract"
128
                    $nameClean = str_ireplace(['interface', 'abstract'], '', $name);
129
130
                    $properties[$value] = [
131
                        'name' => $nameClean,
132
                        'short' => $shortClass,
133
                    ];
134
                }
135
            }
136
        }
137
        $this->properties = $properties;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getClassTemplate(): string
144
    {
145
        return <<<EOF
146
        <?php
147
        
148
        declare(strict_types=1);
149
        
150
        namespace %namespace%;
151
        
152
        use Platine\Framework\Task\TaskInterface;
153
        %uses%
154
155
        /**
156
        * @class %classname%
157
        * @package %namespace%
158
        */
159
        class %classname% implements TaskInterface
160
        {
161
            
162
            %properties%
163
        
164
            %constructor%
165
        
166
            /**
167
            * {@inheritdoc}
168
            */
169
            public function run(): void
170
            {
171
                
172
            }
173
        
174
            %task_body%
175
        }
176
        
177
        EOF;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    protected function createClass(): string
184
    {
185
        $content = parent::createClass();
186
187
        return $this->getTaskBody($content);
188
    }
189
190
    /**
191
     * Return the task body
192
     * @param string $content
193
     * @return string
194
     */
195
    protected function getTaskBody(string $content): string
196
    {
197
        $result = <<<EOF
198
            /**
199
            * {@inheritdoc}
200
            */
201
            public function expression(): string
202
            {
203
                return '$this->expression';
204
            }
205
                    
206
            /**
207
            * {@inheritdoc}
208
            */
209
            public function name(): string
210
            {
211
                return '$this->name';
212
            }
213
        EOF;
214
215
        return str_replace('%task_body%', $result, $content);
216
    }
217
}
218