GeneratorPublishCommand   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 23
eloc 80
c 4
b 0
f 0
dl 0
loc 238
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fillTemplate() 0 14 1
A publishBaseRepository() 0 19 3
A publishTestCases() 0 34 5
A handle() 0 11 3
A publishLocaleFiles() 0 7 1
A publishBaseController() 0 17 3
A fillLicense() 0 13 2
A getOptions() 0 4 1
A getArguments() 0 3 1
A publishBaseContract() 0 19 3
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use InfyOm\Generator\Commands\Publish\PublishBaseCommand;
6
use InfyOm\Generator\Utils\FileUtil;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class GeneratorPublishCommand extends PublishBaseCommand
10
{
11
    /**
12
     * ALL REFERENCES TO (get_template\(\')([a-z_\.]+)(', 'laravel-generator'\))
13
     * REPLACED WITH: get_artomator_template('$2').
14
     */
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'artomator:publish';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Publishes & init api routes, base controller, base test cases traits.';
29
30
    /**
31
     * Execute the command.
32
     *
33
     * @return void
34
     */
35
    public function handle()
36
    {
37
        $this->publishTestCases();
38
        $this->publishBaseController();
39
        $repositoryPattern = config('infyom.laravel_generator.options.repository_pattern', true);
40
        if (true === $repositoryPattern) {
41
            $this->publishBaseRepository();
42
            $this->publishBaseContract();
43
        }
44
        if (true === $this->option('localized')) {
45
            $this->publishLocaleFiles();
46
        }
47
    }
48
49
    /**
50
     * Replaces dynamic variables of template.
51
     * THIS IS A NEW FUNCTION ADDED.
52
     *
53
     * @param string $templateData Template Data.
54
     *
55
     * @return string
56
     */
57
    private function fillLicense($templateData)
58
    {
59
        $replacements = [
60
            '$LICENSE_PACKAGE$' => config('pwweb.artomator.license.package', 'boo'),
61
            '$LICENSE_AUTHORS$' => license_authors(config('pwweb.artomator.license.authors')),
62
            '$LICENSE_COPYRIGHT$' => config('pwweb.artomator.license.copyright'),
63
            '$LICENSE$' => config('pwweb.artomator.license.license'),
64
        ];
65
        foreach ($replacements as $key => $replacement) {
66
            $templateData = str_replace($key, $replacement, $templateData);
67
        }
68
69
        return $templateData;
70
    }
71
72
    /**
73
     * Replaces dynamic variables of template.
74
     *
75
     * @param string $templateData Template Data.
76
     *
77
     * @return string
78
     */
79
    protected function fillTemplate($templateData)
80
    {
81
        $apiVersion = config('infyom.laravel_generator.api_version', 'v1');
82
        $apiPrefix = config('infyom.laravel_generator.api_prefix', 'api');
83
84
        $templateData = str_replace('$API_VERSION$', $apiVersion, $templateData);
85
        $templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData);
86
        $appNamespace = $this->getLaravel()->getNamespace();
87
        $appNamespace = substr($appNamespace, 0, (strlen($appNamespace) - 1));
88
        $templateData = str_replace('$NAMESPACE_APP$', $appNamespace, $templateData);
89
90
        // return $templateData;
91
        // ADDED THE FOLLOWING LINE:
92
        return $this->fillLicense($templateData);
93
    }
94
95
    /**
96
     * Publish Test Cases.
97
     *
98
     * @return void
99
     */
100
    private function publishTestCases()
101
    {
102
        $testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));
103
        $testsNameSpace = config('infyom.laravel_generator.namespace.tests', 'Tests');
104
        $createdAtField = config('infyom.laravel_generator.timestamps.created_at', 'created_at');
105
        $updatedAtField = config('infyom.laravel_generator.timestamps.updated_at', 'updated_at');
106
107
        $templateData = get_artomator_template('test.api_test_trait');
108
109
        $templateData = str_replace('$NAMESPACE_TESTS$', $testsNameSpace, $templateData);
110
        $templateData = str_replace('$TIMESTAMPS$', "['$createdAtField', '$updatedAtField']", $templateData);
111
112
        // ADDED THE FOLLOWING LINE:
113
        $templateData = $this->fillLicense($templateData);
114
115
        $fileName = 'ApiTestTrait.php';
116
117
        if (true === file_exists($testsPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
118
            return;
119
        }
120
121
        FileUtil::createFile($testsPath, $fileName, $templateData);
122
        $this->info('ApiTestTrait created');
123
124
        $testAPIsPath = config('infyom.laravel_generator.path.api_test', base_path('tests/APIs/'));
125
        if (false === file_exists($testAPIsPath)) {
126
            FileUtil::createDirectoryIfNotExist($testAPIsPath);
127
            $this->info('APIs Tests directory created');
128
        }
129
130
        $testRepositoriesPath = config('infyom.laravel_generator.path.repository_test', base_path('tests/Repositories/'));
131
        if (false === file_exists($testRepositoriesPath)) {
132
            FileUtil::createDirectoryIfNotExist($testRepositoriesPath);
133
            $this->info('Repositories Tests directory created');
134
        }
135
    }
136
137
    /**
138
     * Publish Base Controller.
139
     *
140
     * @return void
141
     */
142
    private function publishBaseController()
143
    {
144
        $templateData = get_artomator_template('app_base_controller');
145
146
        $templateData = $this->fillTemplate($templateData);
147
148
        $controllerPath = app_path('Http/Controllers/');
149
150
        $fileName = 'AppBaseController.php';
151
152
        if (true === file_exists($controllerPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
153
            return;
154
        }
155
156
        FileUtil::createFile($controllerPath, $fileName, $templateData);
157
158
        $this->info('AppBaseController created');
159
    }
160
161
    /**
162
     * Publish Base Repository.
163
     *
164
     * @return void
165
     */
166
    private function publishBaseRepository()
167
    {
168
        $templateData = get_artomator_template('base_repository');
169
170
        $templateData = $this->fillTemplate($templateData);
171
172
        $repositoryPath = app_path('Repositories/');
173
174
        FileUtil::createDirectoryIfNotExist($repositoryPath);
175
176
        $fileName = 'BaseRepository.php';
177
178
        if (true === file_exists($repositoryPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
179
            return;
180
        }
181
182
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
183
184
        $this->info('BaseRepository created');
185
    }
186
187
    /**
188
     * Publish Base Contract.
189
     *
190
     * @return void
191
     */
192
    private function publishBaseContract()
193
    {
194
        $templateData = get_artomator_template('base_contract');
195
196
        $templateData = $this->fillTemplate($templateData);
197
198
        $contractPath = app_path('Contracts/');
199
200
        FileUtil::createDirectoryIfNotExist($contractPath);
201
202
        $fileName = 'BaseRepositoryContract.php';
203
204
        if (true === file_exists($contractPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
205
            return;
206
        }
207
208
        FileUtil::createFile($contractPath, $fileName, $templateData);
209
210
        $this->info('BaseContract created');
211
    }
212
213
    /**
214
     * Publish Locale Files.
215
     *
216
     * @return void
217
     */
218
    private function publishLocaleFiles()
219
    {
220
        $localesDir = __DIR__.'/../../../locale/';
221
222
        $this->publishDirectory($localesDir, resource_path('lang'), 'lang', true);
223
224
        $this->comment('Locale files published');
225
    }
226
227
    /**
228
     * Get the console command options.
229
     *
230
     * @return array
231
     */
232
    public function getOptions()
233
    {
234
        return [
235
            ['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
236
        ];
237
    }
238
239
    /**
240
     * Get the console command arguments.
241
     *
242
     * @return array
243
     */
244
    protected function getArguments()
245
    {
246
        return [];
247
    }
248
}
249