Passed
Push — master ( 660c80...17f60a )
by Arthur
08:30
created

BootstrapRegistrarService   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 134
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 27

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSeeders() 0 3 1
A getConfigs() 0 3 1
A __construct() 0 3 1
C scanDirectories() 0 54 14
A getMigrations() 0 3 1
A cache() 0 5 1
A getCommands() 0 3 1
A getCachePath() 0 3 1
A getFactories() 0 3 1
A loadBootstrapFromCache() 0 9 2
A hasPhpExtension() 0 3 2
A getRoutes() 0 3 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 02:13.
7
 */
8
9
namespace Foundation\Services;
10
11
use Illuminate\Console\Command;
12
13
class BootstrapRegistrarService
14
{
15
    protected $files;
16
17
    protected $moduleEntityDirectories = [
18
        'commands' => 'Console',
19
        'routes' => 'Routes',
20
        'configs' => 'Config',
21
        'factories' => 'Database/factories',
22
        'migrations' => 'Database/Migrations',
23
        'seeders' => 'Database/Seeders'
24
    ];
25
26
    protected $cacheFile = 'bootstrap.php';
27
28
    protected $bootstrap;
29
30
    public function __construct()
31
    {
32
        $this->files = new \Illuminate\Filesystem\Filesystem();
33
    }
34
35
    public function cache()
36
    {
37
        $boostrap = $this->scanDirectories();
38
        $this->bootstrap = $boostrap;
39
        $this->files->put($this->getCachePath(), serialize($boostrap));
40
    }
41
42
    public function scanDirectories(): array
43
    {
44
        $bootstrap = [];
45
        $modules = \Module::all();
46
47
48
        foreach ($modules as $module) {
49
            foreach ($this->moduleEntityDirectories as $key => $directory) {
50
                $directory = ucfirst($directory);
51
                $directoryPath = $module->getPath() . '/' . $directory;
52
                $namespace = 'Modules' . '\\' . $module->getName();
53
                if (file_exists($directoryPath)) {
54
                    $files = scandir($directoryPath);
55
                    foreach ($files as $fileName) {
56
                        if ($this->hasPhpExtension($fileName)) {
57
                            $className = basename($fileName, '.php');
58
                            $class = $namespace . '\\' . str_replace('/', '\\', $directory) . '\\' . $className;
59
60
                            try {
61
                                switch ($key) {
62
                                    case 'commands':
63
                                        if (new $class() instanceof Command) {
64
                                            $bootstrap[$key][] = $class;
65
                                        }
66
                                        break;
67
                                    case 'routes':
68
                                        $bootstrap[$key][] = [$directoryPath . '/' . $fileName, $namespace];
69
                                        break;
70
                                    case 'configs':
71
                                        $bootstrap[$key][] = [$directoryPath . '/' . $fileName, strtolower($module->getName())];
72
                                        break;
73
                                    case 'factories':
74
                                        $bootstrap[$key][] = $directoryPath;
75
                                        break;
76
                                    case 'migrations':
77
                                        $bootstrap[$key][] = $directoryPath;
78
                                        break;
79
                                    case 'seeders':
80
                                        $bootstrap[$key][] = $class;
81
                                        break;
82
                                    default:
83
                                        break;
84
                                }
85
86
87
                            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
                            }
89
                        }
90
                    }
91
                }
92
            }
93
        }
94
95
        return $bootstrap;
96
    }
97
98
    private function hasPhpExtension(string $fileName): bool
99
    {
100
        return strlen($fileName) > 4 && '.php' === ($fileName[-4] . $fileName[-3] . $fileName[-2] . $fileName[-1]);
101
    }
102
103
    private function loadBootstrapFromCache()
104
    {
105
        if (!isset($this->bootstrap)) {
106
            $commandCachePath = $this->getCachePath();
107
            $this->cache();
108
            $this->boostrap = unserialize($this->files->get($commandCachePath));
0 ignored issues
show
Bug Best Practice introduced by
The property boostrap does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
        }
110
111
        return $this->bootstrap;
112
    }
113
114
    public function getCommands(): array
115
    {
116
        return $this->loadBootstrapFromCache()['commands'] ?? [];
117
    }
118
119
    public function getRoutes(): array
120
    {
121
        return $this->loadBootstrapFromCache()['routes'] ?? [];
122
    }
123
124
    public function getConfigs(): array
125
    {
126
        return $this->loadBootstrapFromCache()['configs'] ?? [];
127
    }
128
129
    public function getFactories(): array
130
    {
131
        return $this->loadBootstrapFromCache()['factories'] ?? [];
132
    }
133
134
    public function getMigrations(): array
135
    {
136
        return $this->loadBootstrapFromCache()['migrations'] ?? [];
137
    }
138
139
    public function getSeeders(): array
140
    {
141
        return $this->loadBootstrapFromCache()['seeders'] ?? [];
142
    }
143
144
    public function getCachePath(): string
145
    {
146
        return app()->bootstrapPath() . '/cache/' . $this->cacheFile;
1 ignored issue
show
introduced by
The method bootstrapPath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

146
        return app()->/** @scrutinizer ignore-call */ bootstrapPath() . '/cache/' . $this->cacheFile;
Loading history...
147
    }
148
}
149