Passed
Branch master (d88b3b)
by Prateek
03:56
created

Observer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 41 4
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Backend;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
7
class Observer extends BaseGenerator implements GeneratorInterface
8
{
9
    protected static $initializeFlag = 0;
10
    public function generate()
11
    {   
12
        $generatedFiles=[]; 
13
        
14
        if($this::$initializeFlag == 0){
15
            $laragen = app('laragen');
16
            $modules = $laragen->getModules();
0 ignored issues
show
Bug introduced by
The method getModules() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

16
            /** @scrutinizer ignore-call */ 
17
            $modules = $laragen->getModules();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
            $models = [];
18
            foreach ($modules as $module) {
19
                $models[] = $module->getModelName(); 
20
            }
21
22
            $modelsCode = '';
23
            $usedClasses = '';
24
            foreach ($models as $model) {
25
                $modelsCode .= $model ."::observe(". $model ."Observer::class);" . PHP_EOL. $this->getTabs(2);
26
                $usedClasses .= "use App\Observers\\". $model . "Observer;" . PHP_EOL;
27
                $usedClasses .= "use App\Models\\". $model . ";" . PHP_EOL;
28
            }
29
30
            $observerProviderTemplate = $this->buildTemplate('common/LaragenObserverServiceProvider', [
31
                '{{modelObservers}}'     => $modelsCode,
32
                '{{usedClasses}}'     => $usedClasses,
33
            ]);
34
35
            $fullFilePath = $this->getPath("app/Providers/")."LaragenObserverServiceProvider.php";
36
            file_put_contents($fullFilePath, $observerProviderTemplate);
37
            $generatedFiles[] = $fullFilePath;
38
            $this::$initializeFlag++;
39
40
        }
41
42
        $controllerTemplate = $this->buildTemplate('backend/observers/observer', [
43
            '{{modelName}}'           => $this->module->getModelName(),
44
            '{{modelNameLowercase}}' => $this->module->getModelNameLowercase(),
45
        ]);
46
        
47
        $fullFilePath = $this->getPath("app/Observers/").$this->module->getModelName()."Observer".".php";
48
        file_put_contents($fullFilePath, $controllerTemplate);
49
        $generatedFiles[] = $fullFilePath;
50
        return $generatedFiles;
51
    }
52
53
}
54