Passed
Push — feature/lg ( 3c06af...b2e7fb )
by Richard
03:07
created

GeneratorPublishCommand::publishBaseRepository()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 19
rs 9.9666
cc 3
nc 2
nop 0
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use InfyOm\Generator\Utils\FileUtil;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class GeneratorPublishCommand extends PublishBaseCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'artomator:publish';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Publishes & init api routes, base controller, base test cases traits.';
23
24
    /**
25
     * Execute the command.
26
     *
27
     * @return void
28
     */
29
    public function handle()
30
    {
31
        $this->publishTestCases();
32
        $this->publishBaseController();
33
        $repositoryPattern = config('infyom.laravel_generator.options.repository_pattern', true);
34
        if ($repositoryPattern) {
35
            $this->publishBaseRepository();
36
        }
37
        if ($this->option('localized')) {
38
            $this->publishLocaleFiles();
39
        }
40
    }
41
42
    /**
43
     * Replaces dynamic variables of template.
44
     *
45
     * @param string $templateData
46
     *
47
     * @return string
48
     */
49
    private function fillTemplate($templateData)
50
    {
51
        $apiVersion = config('infyom.laravel_generator.api_version', 'v1');
52
        $apiPrefix = config('infyom.laravel_generator.api_prefix', 'api');
53
54
        $templateData = str_replace('$API_VERSION$', $apiVersion, $templateData);
55
        $templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData);
56
        $appNamespace = $this->getLaravel()->getNamespace();
57
        $appNamespace = substr($appNamespace, 0, strlen($appNamespace) - 1);
58
        $templateData = str_replace('$NAMESPACE_APP$', $appNamespace, $templateData);
59
60
        return $templateData;
61
    }
62
63
    private function publishTestCases()
64
    {
65
        $testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));
66
        $testsNameSpace = config('infyom.laravel_generator.namespace.tests', 'Tests');
67
        $createdAtField = config('infyom.laravel_generator.timestamps.created_at', 'created_at');
68
        $updatedAtField = config('infyom.laravel_generator.timestamps.updated_at', 'updated_at');
69
70
        $templateData = get_template('test.api_test_trait', 'laravel-generator');
71
72
        $templateData = str_replace('$NAMESPACE_TESTS$', $testsNameSpace, $templateData);
73
        $templateData = str_replace('$TIMESTAMPS$', "['$createdAtField', '$updatedAtField']", $templateData);
74
75
        $fileName = 'ApiTestTrait.php';
76
77
        if (file_exists($testsPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
78
            return;
79
        }
80
81
        FileUtil::createFile($testsPath, $fileName, $templateData);
82
        $this->info('ApiTestTrait created');
83
84
        $testAPIsPath = config('infyom.laravel_generator.path.api_test', base_path('tests/APIs/'));
85
        if (! file_exists($testAPIsPath)) {
86
            FileUtil::createDirectoryIfNotExist($testAPIsPath);
87
            $this->info('APIs Tests directory created');
88
        }
89
90
        $testRepositoriesPath = config('infyom.laravel_generator.path.repository_test', base_path('tests/Repositories/'));
91
        if (! file_exists($testRepositoriesPath)) {
92
            FileUtil::createDirectoryIfNotExist($testRepositoriesPath);
93
            $this->info('Repositories Tests directory created');
94
        }
95
    }
96
97
    private function publishBaseController()
98
    {
99
        $templateData = get_template('app_base_controller', 'laravel-generator');
100
101
        $templateData = $this->fillTemplate($templateData);
102
103
        $controllerPath = app_path('Http/Controllers/');
104
105
        $fileName = 'AppBaseController.php';
106
107
        if (file_exists($controllerPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
108
            return;
109
        }
110
111
        FileUtil::createFile($controllerPath, $fileName, $templateData);
112
113
        $this->info('AppBaseController created');
114
    }
115
116
    private function publishBaseRepository()
117
    {
118
        $templateData = get_template('base_repository', 'laravel-generator');
119
120
        $templateData = $this->fillTemplate($templateData);
121
122
        $repositoryPath = app_path('Repositories/');
123
124
        FileUtil::createDirectoryIfNotExist($repositoryPath);
125
126
        $fileName = 'BaseRepository.php';
127
128
        if (file_exists($repositoryPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
129
            return;
130
        }
131
132
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
133
134
        $this->info('BaseRepository created');
135
    }
136
137
    private function publishLocaleFiles()
138
    {
139
        $localesDir = __DIR__.'/../../../locale/';
140
141
        $this->publishDirectory($localesDir, resource_path('lang'), 'lang', true);
142
143
        $this->comment('Locale files published');
144
    }
145
146
    /**
147
     * Get the console command options.
148
     *
149
     * @return array
150
     */
151
    public function getOptions()
152
    {
153
        return [
154
            ['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
155
        ];
156
    }
157
158
    /**
159
     * Get the console command arguments.
160
     *
161
     * @return array
162
     */
163
    protected function getArguments()
164
    {
165
        return [];
166
    }
167
}
168