Completed
Push — master ( c2b776...5d5d0e )
by Chin
03:26
created

generateNameForLocaleFromOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace ChinLeung\LaravelMultilingualRoutes;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Routing\RouteCollection;
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\Arr;
9
10
class MultilingualRegistrar
11
{
12
    /**
13
     * Constructor of the class.
14
     *
15
     * @param  \Illuminate\Routing\Router  $router
16
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct(Router $router)
19
    {
20
        $this->router = $router;
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    /**
24
     * Register the routes.
25
     *
26
     * @param  string  $key
27
     * @param  mixed  $handle
28
     * @param  array  $locales
29
     * @param  array  $options
30
     * @return \Illuminate\Routing\RouteCollection
31
     */
32
    public function register(string $key, $handle, array $locales, array $options) : RouteCollection
33
    {
34
        $collection = new RouteCollection;
35
36
        $method = $this->getRequestMethodFromOptions($options);
37
38
        foreach ($locales as $locale) {
39
            $collection->add(
40
                $this
41
                    ->registerRoute($method, $key, $handle, $locale)
42
                    ->name($this->generateNameForLocaleFromOptions($locale, $key, $options))
43
                    ->prefix($this->generatePrefixForLocale($locale))
44
            );
45
        }
46
47
        return $collection;
48
    }
49
50
    /**
51
     * Register a single route.
52
     *
53
     * @param  string  $method
54
     * @param  string  $key
55
     * @param  mixed  $handle
56
     * @param  string  $locale
57
     * @return \Illuminate\Routing\Route
58
     */
59
    protected function registerRoute(string $method, string $key, $handle, string $locale) : Route
60
    {
61
        return $this->router->{strtolower($method)}(
62
            trans("routes.$key", [], $locale),
63
            $handle
64
        );
65
    }
66
67
    /**
68
     * Retrieve the request method from the options.
69
     *
70
     * @param  array  $options
71
     * @return string
72
     */
73
    protected function getRequestMethodFromOptions(array $options) : string
74
    {
75
        return $options['method'] ?? 'get';
76
    }
77
78
    /**
79
     * Generate the name of the route based on the options.
80
     *
81
     * @param  string  $locale
82
     * @param  string  $key
83
     * @param  array  $options
84
     * @return string
85
     */
86
    protected function generateNameForLocaleFromOptions(string $locale, string $key, array $options) : string
87
    {
88
        if ($name = Arr::get($options, "names.$locale")) {
89
            return "$locale.$name";
90
        }
91
92
        return sprintf(
93
            '%s.%s',
94
            $locale,
95
            Arr::get($options, 'name', $key)
96
        );
97
    }
98
99
    /**
100
     * Generate the prefix of the route based on the options.
101
     *
102
     * @param  string  $locale
103
     * @return string|null
104
     */
105
    protected function generatePrefixForLocale(string $locale) : ?string
106
    {
107
        if ($locale != config('app.fallback_locale')) {
108
            return $locale;
109
        }
110
111
        return config('laravel-multilingual-routes.prefix_fallback')
112
            ? $locale
113
            : null;
114
    }
115
}
116