Passed
Push — master ( 9de3df...49c81a )
by Richard
11:22 queued 11s
created

GeneratorPublishCommand::publishTestCases()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 34
rs 9.2728
cc 5
nc 5
nop 0
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 ($repositoryPattern) {
41
            $this->publishBaseRepository();
42
        }
43
        if ($this->option('localized')) {
44
            $this->publishLocaleFiles();
45
        }
46
    }
47
48
    /**
49
     * Replaces dynamic variables of template.
50
     * THIS IS A NEW FUNCTION ADDED.
51
     *
52
     * @param string $templateData
53
     *
54
     * @return string
55
     */
56
    private function fillLicense($templateData)
57
    {
58
        $replacements = [
59
            '$LICENSE_PACKAGE$' => config('pwweb.artomator.license.package', 'boo'),
60
            '$LICENSE_AUTHORS$' => license_authors(config('pwweb.artomator.license.authors')),
61
            '$LICENSE_COPYRIGHT$' => config('pwweb.artomator.license.copyright'),
62
            '$LICENSE$' => config('pwweb.artomator.license.license'),
63
        ];
64
        foreach ($replacements as $key => $replacement) {
65
            $templateData = str_replace($key, $replacement, $templateData);
66
        }
67
68
        return $templateData;
69
    }
70
71
    /**
72
     * Replaces dynamic variables of template.
73
     *
74
     * @param string $templateData
75
     *
76
     * @return string
77
     */
78
    protected function fillTemplate($templateData)
79
    {
80
        $apiVersion = config('infyom.laravel_generator.api_version', 'v1');
81
        $apiPrefix = config('infyom.laravel_generator.api_prefix', 'api');
82
83
        $templateData = str_replace('$API_VERSION$', $apiVersion, $templateData);
84
        $templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData);
85
        $appNamespace = $this->getLaravel()->getNamespace();
86
        $appNamespace = substr($appNamespace, 0, strlen($appNamespace) - 1);
87
        $templateData = str_replace('$NAMESPACE_APP$', $appNamespace, $templateData);
88
89
        // return $templateData;
90
        // ADDED THE FOLLOWING LINE:
91
        return $this->fillLicense($templateData);
92
    }
93
94
    private function publishTestCases()
95
    {
96
        $testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));
97
        $testsNameSpace = config('infyom.laravel_generator.namespace.tests', 'Tests');
98
        $createdAtField = config('infyom.laravel_generator.timestamps.created_at', 'created_at');
99
        $updatedAtField = config('infyom.laravel_generator.timestamps.updated_at', 'updated_at');
100
101
        $templateData = get_artomator_template('test.api_test_trait');
102
103
        $templateData = str_replace('$NAMESPACE_TESTS$', $testsNameSpace, $templateData);
104
        $templateData = str_replace('$TIMESTAMPS$', "['$createdAtField', '$updatedAtField']", $templateData);
105
106
        // ADDED THE FOLLOWING LINE:
107
        $templateData = $this->fillLicense($templateData);
108
109
        $fileName = 'ApiTestTrait.php';
110
111
        if (file_exists($testsPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
112
            return;
113
        }
114
115
        FileUtil::createFile($testsPath, $fileName, $templateData);
116
        $this->info('ApiTestTrait created');
117
118
        $testAPIsPath = config('infyom.laravel_generator.path.api_test', base_path('tests/APIs/'));
119
        if (false === file_exists($testAPIsPath)) {
120
            FileUtil::createDirectoryIfNotExist($testAPIsPath);
121
            $this->info('APIs Tests directory created');
122
        }
123
124
        $testRepositoriesPath = config('infyom.laravel_generator.path.repository_test', base_path('tests/Repositories/'));
125
        if (false === file_exists($testRepositoriesPath)) {
126
            FileUtil::createDirectoryIfNotExist($testRepositoriesPath);
127
            $this->info('Repositories Tests directory created');
128
        }
129
    }
130
131
    private function publishBaseController()
132
    {
133
        $templateData = get_artomator_template('app_base_controller');
134
135
        $templateData = $this->fillTemplate($templateData);
136
137
        $controllerPath = app_path('Http/Controllers/');
138
139
        $fileName = 'AppBaseController.php';
140
141
        if (file_exists($controllerPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
142
            return;
143
        }
144
145
        FileUtil::createFile($controllerPath, $fileName, $templateData);
146
147
        $this->info('AppBaseController created');
148
    }
149
150
    private function publishBaseRepository()
151
    {
152
        $templateData = get_artomator_template('base_repository');
153
154
        $templateData = $this->fillTemplate($templateData);
155
156
        $repositoryPath = app_path('Repositories/');
157
158
        FileUtil::createDirectoryIfNotExist($repositoryPath);
159
160
        $fileName = 'BaseRepository.php';
161
162
        if (file_exists($repositoryPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
163
            return;
164
        }
165
166
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
167
168
        $this->info('BaseRepository created');
169
    }
170
171
    private function publishLocaleFiles()
172
    {
173
        $localesDir = __DIR__.'/../../../locale/';
174
175
        $this->publishDirectory($localesDir, resource_path('lang'), 'lang', true);
176
177
        $this->comment('Locale files published');
178
    }
179
180
    /**
181
     * Get the console command options.
182
     *
183
     * @return array
184
     */
185
    public function getOptions()
186
    {
187
        return [
188
            ['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
189
        ];
190
    }
191
192
    /**
193
     * Get the console command arguments.
194
     *
195
     * @return array
196
     */
197
    protected function getArguments()
198
    {
199
        return [];
200
    }
201
}
202