ViewServiceProviderGenerator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 16
eloc 136
c 4
b 2
f 0
dl 0
loc 286
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addProperty() 0 19 1
A addViewVariables() 0 19 2
A addViewComposer() 0 21 1
A addBoot() 0 55 2
A addSelect() 0 27 2
A addNamespace() 0 21 2
A generate() 0 15 2
A addCustomProvider() 0 16 3
1
<?php
2
3
namespace PWWEB\Artomator\Generators;
4
5
use Illuminate\Support\Facades\File;
6
use InfyOm\Generator\Generators\BaseGenerator;
7
use PWWEB\Artomator\Common\CommandData;
8
9
/**
10
 * Class ViewServiceProviderGenerator.
11
 */
12
class ViewServiceProviderGenerator extends BaseGenerator
13
{
14
    /**
15
     * Command Data.
16
     *
17
     * @var CommandData
18
     */
19
    private $commandData;
20
21
    /**
22
     * ViewServiceProvider constructor.
23
     *
24
     * @param CommandData $commandData Command data.
25
     */
26
    public function __construct(CommandData $commandData)
27
    {
28
        $this->commandData = $commandData;
29
    }
30
31
    /**
32
     * Generate ViewServiceProvider.
33
     *
34
     * @return void
35
     */
36
    public function generate()
37
    {
38
        $templateData = get_artomator_template('view_service_provider');
39
40
        $destination = $this->commandData->config->pathViewProvider;
41
42
        $fileName = basename($this->commandData->config->pathViewProvider);
43
44
        if (true === File::exists($destination)) {
45
            return;
46
        }
47
        file_put_contents($destination, $templateData);
48
49
        $this->commandData->commandComment($fileName.' published');
50
        $this->commandData->commandInfo($fileName);
51
    }
52
53
    /**
54
     * Add View Variables.
55
     *
56
     * @param string      $views        Views.
57
     * @param string      $variableName Variable Name.
58
     * @param string      $columns      Columns.
59
     * @param string      $tableName    Table name.
60
     * @param string|null $modelName    Model name.
61
     *
62
     * @return void
63
     */
64
    public function addViewVariables($views, $variableName, $columns, $tableName, $modelName = null)
0 ignored issues
show
Unused Code introduced by
The parameter $variableName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    public function addViewVariables($views, /** @scrutinizer ignore-unused */ $variableName, $columns, $tableName, $modelName = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        if (false === empty($modelName)) {
67
            $model = $modelName;
68
        } else {
69
            $model = model_name_from_table_name($tableName);
70
        }
71
72
        $this->commandData->addDynamicVariable('$COMPOSER_VIEWS$', $views);
73
        $this->commandData->addDynamicVariable('$COMPOSER_VIEW_MODEL$', $model);
74
        $this->commandData->addDynamicVariable('$COMPOSER_VIEW_COLUMNS$', $columns);
75
76
        $mainViewContent = $this->addViewComposer();
77
        $mainViewContent = $this->addNamespace($model, $mainViewContent);
78
        $mainViewContent = $this->addSelect($model, $views, $mainViewContent);
79
        $this->addCustomProvider();
80
81
        file_put_contents($this->commandData->config->pathViewProvider, $mainViewContent);
82
        $this->commandData->commandComment('View service provider file updated.');
83
    }
84
85
    /**
86
     * Add View Composer.
87
     *
88
     * @return string
89
     */
90
    public function addViewComposer()
91
    {
92
        $mainViewContent = file_get_contents($this->commandData->config->pathViewProvider);
93
        $newViewStatement = get_artomator_template('scaffold.view_composer');
94
        $newViewStatement = fill_template($this->commandData->dynamicVars, $newViewStatement);
95
96
        $newViewStatement = infy_nl(1).$newViewStatement;
97
        preg_match_all('/}(\s)}/', $mainViewContent, $matches);
98
99
        $totalMatches = count($matches[0]);
100
        $lastSeederStatement = $matches[0][($totalMatches - 1)];
101
102
        $replacePosition = strpos($mainViewContent, $lastSeederStatement);
103
        $mainViewContent = substr_replace(
104
            $mainViewContent,
105
            $newViewStatement,
106
            $replacePosition,
107
            0
108
        );
109
110
        return $mainViewContent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mainViewContent also could return the type array which is incompatible with the documented return type string.
Loading history...
111
    }
112
113
    /**
114
     * Add Custom Provider.
115
     *
116
     * @return void
117
     */
118
    public function addCustomProvider()
119
    {
120
        $configFile = base_path().'/config/app.php';
121
        $file = file_get_contents($configFile);
122
        $searchFor = 'Illuminate\View\ViewServiceProvider::class,';
123
        $customProviders = strpos($file, $searchFor);
124
125
        $isExist = strpos($file, "App\Providers\ViewServiceProvider::class");
126
        if (true === $customProviders && false === $isExist) {
127
            $newChanges = substr_replace(
128
                $file,
129
                infy_nl().infy_tab(8).'\App\Providers\ViewServiceProvider::class,',
130
                ($customProviders + strlen($searchFor)),
131
                0
132
            );
133
            file_put_contents($configFile, $newChanges);
134
        }
135
    }
136
137
    /**
138
     * Add Namespace.
139
     *
140
     * @param string $model           Model.
141
     * @param string $mainViewContent Main View Content.
142
     *
143
     * @return string
144
     */
145
    public function addNamespace($model, $mainViewContent)
146
    {
147
        $newModelStatement = 'use '.$this->commandData->config->nsContract.'\\'.$model.'RepositoryContract as '.$model.';';
148
        $isNameSpaceExist = strpos($mainViewContent, $newModelStatement);
149
        $newModelStatement = infy_nl().$newModelStatement;
150
        if (false === $isNameSpaceExist) {
151
            preg_match_all('/namespace(.*)/', $mainViewContent, $matches);
152
            $totalMatches = count($matches[0]);
153
            $nameSpaceStatement = $matches[0][($totalMatches - 1)];
154
            $replacePosition = strpos($mainViewContent, $nameSpaceStatement);
155
            $mainViewContent = substr_replace(
156
                $mainViewContent,
157
                $newModelStatement,
158
                ($replacePosition + strlen($nameSpaceStatement)),
159
                0
160
            );
161
            $mainViewContent = $this->addProperty($model, $mainViewContent);
0 ignored issues
show
Bug introduced by
It seems like $mainViewContent can also be of type array; however, parameter $mainViewContent of PWWEB\Artomator\Generato...enerator::addProperty() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
            $mainViewContent = $this->addProperty($model, /** @scrutinizer ignore-type */ $mainViewContent);
Loading history...
162
            $mainViewContent = $this->addBoot($model, $mainViewContent);
163
        }
164
165
        return $mainViewContent;
166
    }
167
168
    /**
169
     * Add Property.
170
     *
171
     * @param string $model           Model.
172
     * @param string $mainViewContent Main View Content.
173
     *
174
     * @return string
175
     */
176
    public function addProperty($model, $mainViewContent)
177
    {
178
        $newPropertyStatement = "\n\n\t/**\n\t * The "
179
            .ucfirst($model)." repository.\n\t *\n\t * @var "
180
            .ucfirst($model)."\n\t */\n\tprivate \$"
181
            .lcfirst($model)."Repository;\n\n";
182
183
        preg_match_all('/\{/', $mainViewContent, $matches);
184
        $totalMatches = count($matches[0]);
0 ignored issues
show
Unused Code introduced by
The assignment to $totalMatches is dead and can be removed.
Loading history...
185
        $propertyStatement = $matches[0][0];
186
        $replacePosition = strpos($mainViewContent, $propertyStatement);
187
        $mainViewContent = substr_replace(
188
            $mainViewContent,
189
            $newPropertyStatement,
190
            ($replacePosition + strlen($propertyStatement)),
191
            0
192
        );
193
194
        return $mainViewContent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mainViewContent also could return the type array which is incompatible with the documented return type string.
Loading history...
195
    }
196
197
    /**
198
     * Add Boot.
199
     *
200
     * @param string $model           Model.
201
     * @param string $mainViewContent Main view content.
202
     *
203
     * @return string
204
     */
205
    public function addBoot($model, $mainViewContent)
206
    {
207
        $newBootStatement = "\n\t\t"
208
            .ucfirst($model).' $'
209
            .lcfirst($model).'Repo,';
210
211
        preg_match_all('/boot\(.*?\)/mis', $mainViewContent, $matches);
212
        $totalMatches = count($matches[0]);
213
        $bootStatement = $matches[0][($totalMatches - 1)];
214
        $replacePosition = strpos($mainViewContent, $bootStatement);
215
        $mainViewContent = substr_replace(
216
            $mainViewContent,
217
            $newBootStatement,
218
            ($replacePosition + strlen($bootStatement) - 1),
219
            0
220
        );
221
222
        $newBootStatement = "\n\t\t\$this->"
223
            .lcfirst($model).'Repository = $'
224
            .lcfirst($model).'Repo;';
225
226
        preg_match_all('/Repo\;$/', $mainViewContent, $matches);
227
        $totalMatches = count($matches[0]);
228
        if ($totalMatches <= 0) {
229
            preg_match_all('/boot\(.*?\{/s', $mainViewContent, $matches);
230
            $totalMatches = count($matches[0]);
231
        }
232
        $bootStatement = $matches[0][($totalMatches - 1)];
233
        $replacePosition = strpos($mainViewContent, $bootStatement);
0 ignored issues
show
Bug introduced by
It seems like $mainViewContent can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
        $replacePosition = strpos(/** @scrutinizer ignore-type */ $mainViewContent, $bootStatement);
Loading history...
234
        $mainViewContent = substr_replace(
235
            $mainViewContent,
236
            $newBootStatement,
237
            ($replacePosition + strlen($bootStatement)),
238
            0
239
        );
240
241
        $newBootStatement = "\t * @param "
242
            .ucfirst($model).' $'
243
            .lcfirst($model).'Repo '
244
            .ucfirst($model).' repo';
245
246
        preg_match_all('/Bootstrap(.*)services\./mis', $mainViewContent, $matches);
247
        $totalMatches = count($matches[0]);
248
        echo $totalMatches."\n";
249
        $newBootStatement = "\n\n".$newBootStatement;
250
        $bootStatement = $matches[0][($totalMatches - 1)];
251
        $replacePosition = strpos($mainViewContent, $bootStatement);
252
        $mainViewContent = substr_replace(
253
            $mainViewContent,
254
            $newBootStatement,
255
            ($replacePosition + strlen($bootStatement)),
256
            0
257
        );
258
259
        return $mainViewContent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mainViewContent also could return the type array which is incompatible with the documented return type string.
Loading history...
260
    }
261
262
    /**
263
     * Add Select.
264
     *
265
     * @param string $model           Model.
266
     * @param string $view            View.
267
     * @param string $mainViewContent Main View Content.
268
     *
269
     * @return string
270
     */
271
    public function addSelect($model, $view, $mainViewContent)
272
    {
273
        $newModelStatement = '\''.$model.'\' => [';
274
        $length = strlen($newModelStatement);
275
        $isNameSpaceExist = strpos($mainViewContent, $newModelStatement);
276
        $newModelStatement = infy_nl().$newModelStatement.infy_nl_tab().'\''.$view.'\','.infy_nl();
277
        if (false === $isNameSpaceExist) {
278
            preg_match_all('/selects = \[/', $mainViewContent, $matches);
279
            $totalMatches = count($matches[0]);
280
            $nameSpaceStatement = $matches[0][($totalMatches - 1)];
281
            $replacePosition = strpos($mainViewContent, $nameSpaceStatement);
282
            $mainViewContent = substr_replace(
283
                $mainViewContent,
284
                $newModelStatement.'],'.infy_nl(),
285
                ($replacePosition + strlen($nameSpaceStatement)),
286
                0
287
            );
288
        } else {
289
            $mainViewContent = substr_replace(
290
                $mainViewContent,
291
                $newModelStatement,
292
                $isNameSpaceExist,
293
                $length
294
            );
295
        }
296
297
        return $mainViewContent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mainViewContent also could return the type array which is incompatible with the documented return type string.
Loading history...
298
    }
299
}
300