Completed
Push — master ( 1d9f9e...861a2b )
by Frédéric
02:08
created

ResourceRegistrar   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 141
ccs 57
cts 60
cp 0.95
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getLocaleResourceAction() 0 4 1
A addResourceIndex() 0 8 1
A addResourceCreate() 0 9 1
A getLocaleUris() 0 10 2
A getTranslation() 0 6 2
A addResourceShow() 0 8 1
A addResourceEdit() 0 9 1
A register() 0 30 5
A registerMethod() 0 21 3
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Routing;
4
5
use CaribouFute\LocaleRoute\LocaleConfig;
6
use Illuminate\Routing\ResourceRegistrar as IlluminateResourceRegistrar;
7
use Illuminate\Routing\Router as IlluminateRouter;
8
use Illuminate\Support\Str;
9
use Illuminate\Translation\Translator;
10
11
class ResourceRegistrar extends IlluminateResourceRegistrar
12
{
13
    protected $localeConfig;
14
    protected $localeRouter;
15
    protected $translator;
16
17 3
    public function __construct(
18
        LocaleConfig $localeConfig,
19
        LocaleRouter $localeRouter,
20
        IlluminateRouter $router,
21
        Translator $translator
22
    ) {
23 3
        $this->localeConfig = $localeConfig;
24 3
        $this->localeRouter = $localeRouter;
25 3
        $this->translator = $translator;
26
27 3
        parent::__construct($router);
28 3
    }
29
30 3
    protected function getLocaleResourceAction($controller, $method)
31
    {
32 3
        return $controller . '@' . $method;
33
    }
34
35 1
    protected function addResourceIndex($name, $base, $controller, $options): RouteCollection
36
    {
37 1
        $uri = $this->getResourceUri($name);
38 1
        $name = $this->getResourceRouteName($name, 'index', $options);
39 1
        $action = $this->getLocaleResourceAction($controller, 'index');
40
41 1
        return $this->localeRouter->get($name, $action, $uri);
0 ignored issues
show
Documentation introduced by
$uri is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
    }
43
44 3
    protected function addResourceCreate($name, $base, $controller, $options): RouteCollection
45
    {
46 3
        $base = $this->getResourceUri($name);
47 3
        $uris = $this->getLocaleUris($base, 'create', $options);
48 3
        $name = $this->getResourceRouteName($name, 'create', $options);
49 3
        $action = $this->getLocaleResourceAction($controller, 'create');
50
51 3
        return $this->localeRouter->get($name, $action, $uris);
52
    }
53
54 3
    protected function getLocaleUris($base, $label, $options)
55
    {
56 3
        $uris = [];
57
58 3
        foreach ($this->localeConfig->locales($options) as $locale) {
59 3
            $uris[$locale] = $base . '/' . $this->getTranslation($locale, $label);
60
        }
61
62 3
        return $uris;
63
    }
64
65 3
    protected function getTranslation($locale, $label)
66
    {
67 3
        $untranslated = 'route-labels.' . $label;
68 3
        $translated = $this->translator->get($untranslated, [], $locale);
69 3
        return $translated === $untranslated ? $label : $translated;
70
    }
71
72 2
    protected function addResourceShow($name, $base, $controller, $options): RouteCollection
73
    {
74 2
        $uri = $this->getResourceUri($name) . '/{' . $base . '}';
75 2
        $name = $this->getResourceRouteName($name, 'show', $options);
76 2
        $action = $this->getLocaleResourceAction($controller, 'show');
77
78 2
        return $this->localeRouter->get($name, $action, $uri);
0 ignored issues
show
Documentation introduced by
$uri is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
    }
80
81 3
    protected function addResourceEdit($name, $base, $controller, $options): RouteCollection
82
    {
83 3
        $base = $this->getResourceUri($name) . '/{' . $base . '}';
84 3
        $uris = $this->getLocaleUris($base, 'edit', $options);
85 3
        $name = $this->getResourceRouteName($name, 'edit', $options);
86 3
        $action = $this->getLocaleResourceAction($controller, 'edit');
87
88 3
        return $this->localeRouter->get($name, $action, $uris);
89
    }
90
91
    /**
92
     * Route a resource to a controller.
93
     *
94
     * @param  string $name
95
     * @param  string $controller
96
     * @param  array $options
97
     * @return \Illuminate\Routing\RouteCollection
98
     */
99 3
    public function register($name, $controller, array $options = [])
100
    {
101 3
        if (isset($options['parameters']) && !isset($this->parameters)) {
102
            $this->parameters = $options['parameters'];
103
        }
104
105
        // If the resource name contains a slash, we will assume the developer wishes to
106
        // register these resource routes with a prefix so we will set that up out of
107
        // the box so they don't have to mess with it. Otherwise, we will continue.
108 3
        if (Str::contains($name, '/')) {
109
            $this->prefixedResource($name, $controller, $options);
110
111
            return;
112
        }
113
114
        // We need to extract the base resource from the resource name. Nested resources
115
        // are supported in the framework, but we need to know what name to use for a
116
        // place-holder on the route parameters, which should be the base resources.
117 3
        $base = $this->getResourceWildcard(last(explode('.', $name)));
118
119 3
        $defaults = $this->resourceDefaults;
120
121 3
        $collection = new RouteCollection;
122
123 3
        foreach ($this->getResourceMethods($defaults, $options) as $m) {
124 3
            $collection = $this->registerMethod($m, $name, $base, $controller, $options, $collection);
125
        }
126
127 3
        return $collection;
128
    }
129
130 3
    protected function registerMethod(
131
        string $method,
132
        $name,
133
        $base,
134
        $controller,
135
        $options,
136
        RouteCollection $collection
137
    ): RouteCollection {
138 3
        $addResourceMethod = 'addResource' . ucfirst($method);
139 3
        $routeCollection = $this->$addResourceMethod($name, $base, $controller, $options);
140
141 3
        $routeArray = is_a($routeCollection, RouteCollection::class) ?
142 3
            $routeCollection->getRoutes() :
143 3
            [$routeCollection];
144
145 3
        foreach ($routeArray as $route) {
146 3
            $collection->add($route);
147
        }
148
149 3
        return $collection;
150
    }
151
}
152