Passed
Push — master ( 762407...a7439c )
by Roy
04:51 queued 03:09
created

RoutesLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 4 1
A parseNamespace() 0 7 2
A registerRoute() 0 6 2
A loadFiles() 0 6 1
A getFilesPath() 0 3 1
1
<?php
2
3
namespace LaravelModulize\Services\Loaders;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Route;
8
use LaravelModulize\Contracts\LoadsFiles;
9
use Illuminate\Contracts\Foundation\Application;
10
use LaravelModulize\Contracts\ModulizerRepositoryInterface;
11
12
class RoutesLoader extends BaseFileLoader implements LoadsFiles
13
{
14
    /**
15
     * Load the files to load and register them
16
     *
17
     * @param string $module
18
     * @return void
19
     */
20
    public function loadFiles(string $module): void
21
    {
22
        $this->getFilesToLoad($module)->each(function ($routeFile) use ($module) {
23
            $this->registerRoute(
24
                $this->parseNamespace($module, $routeFile->getBasename()),
25
                $routeFile->getRealPath()
26
            );
27
        });
28
    }
29
30
    /**
31
     * Retrieve the path where the files to load should be at
32
     *
33
     * @param string $module
34
     * @return string
35
     */
36
    public function getFilesPath(string $module): string
37
    {
38
        return $this->repo->getModulePath($module) . "/Http/Routes";
39
    }
40
41
    /**
42
     * Retrieve the namespace to be used when registering the files
43
     *
44
     * @param string $module
45
     * @return string
46
     */
47
    public function getNamespace(string $module): string
48
    {
49
        return $this->repo
50
            ->getModuleNamespace($module) . '\\Http\\Controllers';
51
    }
52
53
    /**
54
     * Parse the namespace that will be used in the Router
55
     * This enables the user to create as many route files as needed
56
     * If the baseName does not contain 'Routes' the namespace will be at the base Controller
57
     *
58
     * @param string $module
59
     * @param string $baseName
60
     * @return string
61
     */
62
    private function parseNamespace(string $module, string $baseName): string
63
    {
64
        $namespace = Str::contains($baseName, 'Routes')
65
            ? Str::start(Str::studly(Str::before($baseName, 'Routes')), '\\')
66
            : '';
67
68
        return $this->getNamespace($module) . $namespace;
69
    }
70
71
    /**
72
     * First we check if the routes have been cached, if not
73
     * Load the routes while registering the namespace.
74
     *
75
     * @param string $namespace
76
     * @param string $realPath
77
     * @return void
78
     */
79
    private function registerRoute(string $namespace, string $realPath)
80
    {
81
        if (!$this->app->routesAreCached()) {
82
            Route::middleware('api')
83
                ->namespace($namespace)
84
                ->group($realPath);
85
        }
86
    }
87
}
88