Test Failed
Push — develop ( 469c4c...b50904 )
by nguereza
03:16
created

MakeEnumCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 144
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnumerationBody() 0 8 2
A getClassTemplate() 0 3 1
A getPropertyTemplate() 0 3 1
A getEnumerationTemplate() 0 4 1
A getUsesContent() 0 3 1
B interact() 0 30 6
A __construct() 0 7 1
A createClass() 0 5 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 MakeEnumCommand.php
34
 *
35
 *  The Make Enumeration 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\Stdlib\Helper\Str;
56
57
/**
58
 * @class MakeEnumCommand
59
 * @package Platine\Framework\Console\Command
60
 */
61
class MakeEnumCommand extends MakeCommand
62
{
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected string $type = 'enum';
67
    
68
    /**
69
     * The enumerations values
70
     * @var array<string, mixed>
71
     */
72
    protected array $enumerations = [];
73
74
    /**
75
     * Create new instance
76
     * @param Application $application
77
     * @param Filesystem $filesystem
78
     */
79
    public function __construct(
80
        Application $application,
81
        Filesystem $filesystem
82
    ) {
83
        parent::__construct($application, $filesystem);
84
        $this->setName('make:enum')
85
               ->setDescription('Command to generate new enumeration class');
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function interact(Reader $reader, Writer $writer): void
92
    {
93
        parent::interact($reader, $writer);
94
95
        $properties = [];
96
97
        $io = $this->io();
98
        $writer->boldYellow('Enter the enumeration list (empty value to finish):', true);
99
        $value = '';
100
        while ($value !== null) {
101
            $value = $io->prompt('Enum name', null, null, false);
102
103
            if (!empty($value)) {
104
                $value = trim($value);
105
                $name = Str::snake($value);
106
107
                $properties[] = Str::upper($name);
108
            }
109
        }
110
111
        if (!empty($properties)) {
112
            foreach ($properties as $name) {
113
                $value = $io->prompt(
114
                    sprintf('Enumeration value for [%s] (just enter to ignore)', $name),
115
                    null,
116
                    null,
117
                    false
118
                );
119
                if (!empty($value)) {
120
                    $this->enumerations[$name] = $value;
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getClassTemplate(): string
130
    {
131
        return <<<EOF
132
        <?php
133
        
134
        declare(strict_types=1);
135
        
136
        namespace %namespace%;
137
        
138
        %uses%
139
140
        /**
141
        * @class %classname%
142
        * @package %namespace%
143
        */
144
        class %classname%
145
        {
146
            %enumerations%
147
        }
148
        
149
        EOF;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    protected function createClass(): string
156
    {
157
        $content = parent::createClass();
158
159
        return $this->getEnumerationBody($content);
160
    }
161
162
    /**
163
     * Return the enumerations body
164
     * @param string $content
165
     * @return string
166
     */
167
    protected function getEnumerationBody(string $content): string
168
    {
169
        $result = '';
170
        foreach ($this->enumerations as $name => $value) {
171
            $result .= $this->getEnumerationTemplate($name, $value);
172
        }
173
174
        return str_replace('%enumerations%', $result, $content);
175
    }
176
177
    /**
178
     * Return the enumeration template
179
     * @param string $name
180
     * @param string $value
181
     * @return string
182
     */
183
    protected function getEnumerationTemplate(string $name, string $value): string
184
    {
185
        return <<<EOF
186
           public const $name = '$value';           
187
           
188
        EOF;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    protected function getPropertyTemplate(string $className, array $info): string
195
    {
196
        return '';
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    protected function getUsesContent(): string
203
    {
204
        return '';
205
    }
206
}
207