Passed
Push — develop ( d3c53a...e28085 )
by nguereza
14:20
created

MakeDatabaseConfigCommand::getClassTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 9.424
1
<?php
2
3
/**
4
 * Platine PHP
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 PHP
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
declare(strict_types=1);
33
34
namespace Platine\Framework\Console\Command;
35
36
use Platine\Filesystem\Filesystem;
37
use Platine\Framework\App\Application;
38
use Platine\Framework\Config\AppDatabaseConfig;
39
use Platine\Framework\Console\MakeCommand;
40
use Platine\Orm\Entity;
41
use Platine\Stdlib\Helper\Str;
42
43
/**
44
 * @class MakeDatabaseConfigCommand
45
 * @package Platine\Framework\Console\Command
46
 */
47
class MakeDatabaseConfigCommand extends MakeCommand
48
{
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected string $type = 'database config';
53
54
    /**
55
     * Create new instance
56
     * @param Application $application
57
     * @param Filesystem $filesystem
58
     * @param AppDatabaseConfig $dbConfig
59
     */
60
    public function __construct(
61
        Application $application,
62
        Filesystem $filesystem,
63
        protected AppDatabaseConfig $dbConfig,
64
    ) {
65
        parent::__construct($application, $filesystem);
66
67
        $this->setName('make:dbconfig')
68
               ->setDescription('Command to generate class that hold database configuration value');
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getClassTemplate(): string
75
    {
76
        return <<<EOF
77
        <?php
78
        
79
        declare(strict_types=1);
80
        
81
        namespace %namespace%;
82
        
83
        use Platine\Framework\Config\AppDatabaseConfig;
84
        %uses%
85
86
        /**
87
        * +=+=+ NOTE: THIS IS THE GENERATED CLASS DO NOT MODIFY IT +=+=+
88
        *
89
        * @class %classname%
90
        * @package %namespace%
91
        * @template TDbConfigurationEntity as \Platine\App\Model\Entity\Configuration
92
        */
93
        class %classname%
94
        {
95
            /**
96
            * Create new instance
97
            * @param AppDatabaseConfig<TDbConfigurationEntity> \$dbConfig
98
            */
99
            public function __construct(protected AppDatabaseConfig \$dbConfig)
100
            {
101
            }
102
        
103
            %config_content%
104
        }
105
        
106
        EOF;
107
    }
108
109
    /**
110
     * Generate the method of the given entity
111
     * @param Entity $entity
112
     * @return string
113
     */
114
    protected function getConfigMethod(Entity $entity): string
115
    {
116
        $types = $this->getDataTypeMaps();
117
        $methodTemplate = $this->getMethodTemplate();
118
        $methodName = Str::camel(sprintf('%s_%s', $entity->module, $entity->name), false);
119
120
        $strMethodName = str_replace('%method_name%', $methodName, $methodTemplate);
121
        $strType = str_replace('%type%', $types[$entity->type][0], $strMethodName);
122
        $strDefault = str_replace('%default_value%', $types[$entity->type][1], $strType);
123
        $strModule = str_replace('%module%', $entity->module, $strDefault);
124
        $strKey = str_replace('%key%', $entity->name, $strModule);
125
126
        return $strKey;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected function createClass(): string
133
    {
134
        $content = parent::createClass();
135
136
        $methods = '';
137
        $results = $this->dbConfig->getLoader()->all();
138
        foreach ($results as $row) {
139
            $methods .= $this->getConfigMethod($row);
140
        }
141
142
        return str_replace('%config_content%', $methods, $content);
143
    }
144
145
    /**
146
     * Return the data type mapping
147
     * @return array<string, array{0:string, 1:string}>
148
     */
149
    protected function getDataTypeMaps(): array
150
    {
151
        return [
152
            'integer' => ['int', '0'],
153
            'double' => ['float', '0.0'],
154
            'float' => ['float', '0.0'],
155
            'array' => ['array', '[]'],
156
            'object' => ['object', 'null'],
157
            'boolean' => ['bool', 'false'],
158
            'string' => ['string', '\'\''],
159
        ];
160
    }
161
162
    /**
163
     * Return the configuration method template
164
     * @return string
165
     */
166
    public function getMethodTemplate(): string
167
    {
168
        return <<<EOF
169
        /**
170
            * Return the configuration value for module "%module%" and key "%key%"
171
            * @param %type%|null \$default the default value
172
            *
173
            * @return %type% the configuration value if exist or default
174
            */
175
            public function get%method_name%(?%type% \$default = %default_value%): %type%
176
            {
177
                return \$this->dbConfig->get('%module%.%key%', \$default);
178
            }
179
            
180
            
181
        EOF;
182
    }
183
}
184