RouteRegistrar   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
eloc 24
c 3
b 1
f 0
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAllRoutes() 0 7 1
A registerRoutes() 0 16 3
A getDefaultRouteNamespace() 0 6 1
A getRoutesFolder() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Contains the RouteRegistrar class.
7
 *
8
 * @copyright   Copyright (c) 2019 Attila Fulop
9
 * @author      Attila Fulop
10
 * @license     MIT
11
 * @since       2019-06-05
12
 *
13
 */
14
15
namespace Konekt\Concord\Routing;
16
17
use Illuminate\Support\Arr;
18
use Illuminate\Support\Facades\File;
19
use Illuminate\Support\Facades\Route;
20
use Konekt\Concord\Contracts\Convention;
21
use Konekt\Concord\Contracts\Module;
22
23
class RouteRegistrar
24
{
25
    /** @var Module */
26
    private $module;
27
28
    /** @var Convention */
29
    private $convention;
30
31
    public function __construct(Module $module, Convention $convention)
32
    {
33
        $this->module = $module;
34
        $this->convention = $convention;
35
    }
36
37
    public function registerAllRoutes()
38
    {
39
        $routeFiles = collect(File::glob($this->getRoutesFolder() . '/*.php'))->map(function ($file) {
0 ignored issues
show
Bug introduced by
Illuminate\Support\Facad...tesFolder() . '/*.php') of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

39
        $routeFiles = collect(/** @scrutinizer ignore-type */ File::glob($this->getRoutesFolder() . '/*.php'))->map(function ($file) {
Loading history...
40
            return File::name($file);
41
        })->all();
42
43
        $this->registerRoutes($routeFiles);
44
    }
45
46
    public function registerRoutes(array $files, array $config = [])
47
    {
48
        $path = $this->getRoutesFolder();
49
50
        if (is_dir($path)) {
51
            $routes = $files;
52
53
            foreach ($routes as $route) {
54
                Route::group(
55
                    [
56
                        'namespace' => Arr::get($config, 'namespace', $this->getDefaultRouteNamespace()),
57
                        'prefix' => Arr::get($config, 'prefix', $this->module->shortName()),
58
                        'as' => Arr::get($config, 'as', $this->module->shortName() . '.'),
59
                        'middleware' => Arr::get($config, 'middleware', ['web'])
60
                    ],
61
                    sprintf('%s/%s.php', $path, $route)
62
                );
63
            }
64
        }
65
    }
66
67
    /**
68
     * Returns the default namespace for routes/controllers within a box/module
69
     *
70
     * @return string
71
     */
72
    private function getDefaultRouteNamespace()
73
    {
74
        return sprintf(
75
            '%s\\%s',
76
            $this->module->getNamespaceRoot(),
77
            str_replace('/', '\\', $this->convention->controllersFolder())
78
        );
79
    }
80
81
    private function getRoutesFolder(): string
82
    {
83
        return $this->module->getBasePath() . '/' . $this->convention->routesFolder();
84
    }
85
}
86