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

MultilingualRoutePendingRegistration::only()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace ChinLeung\LaravelMultilingualRoutes;
4
5
use Illuminate\Routing\RouteCollection;
6
use Illuminate\Support\Arr;
7
8
class MultilingualRoutePendingRegistration
9
{
10
    /**
11
     * The handle of the routes.
12
     *
13
     * @var mixed
14
     */
15
    protected $handle;
16
17
    /**
18
     * The translation key of the routes.
19
     *
20
     * @var string
21
     */
22
    protected $key;
23
24
    /**
25
     * The list of locales for the route.
26
     *
27
     * @var array
28
     */
29
    protected $locales = [];
30
31
    /**
32
     * The options of the routes.
33
     *
34
     * @var array
35
     */
36
    protected $options = [];
37
38
    /**
39
     * The resource's registration status.
40
     *
41
     * @var bool
42
     */
43
    protected $registered = false;
44
45
    /**
46
     * The resource registrar.
47
     *
48
     * @var \Illuminate\Routing\ResourceRegistrar
49
     */
50
    protected $registrar;
51
52
    /**
53
     * Constructor of the class.
54
     *
55
     * @param  \ChinLeung\LaravelMultilingualRoutes\MultilingualRegistrar  $registrar
56
     * @param  string  $key
57
     * @param  mixed  $handle
58
     * @param  array  $locales
59
     * @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...
60
     */
61
    public function __construct(MultilingualRegistrar $registrar, string $key, $handle, array $locales = [])
62
    {
63
        $this->key = $key;
64
        $this->registrar = $registrar;
0 ignored issues
show
Documentation Bug introduced by
It seems like $registrar of type object<ChinLeung\Laravel...\MultilingualRegistrar> is incompatible with the declared type object<Illuminate\Routing\ResourceRegistrar> of property $registrar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
        $this->handle = $handle;
66
        $this->locales = $locales;
67
    }
68
69
    /**
70
     * Register the resource route.
71
     *
72
     * @return \Illuminate\Routing\RouteCollection
73
     */
74
    public function register() : RouteCollection
75
    {
76
        $this->registered = true;
77
78
        return $this->registrar->register(
79
            $this->key,
80
            $this->handle,
81
            $this->options['locales'] ?? $this->locales,
82
            $this->options
0 ignored issues
show
Unused Code introduced by
The call to ResourceRegistrar::register() has too many arguments starting with $this->options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
        );
84
    }
85
86
    /**
87
     * Add one or many locale to the exception.
88
     *
89
     * @param  string|array  $locales
90
     * @return self
91
     */
92
    public function except($locales) : self
93
    {
94
        $this->options['locales'] = array_diff(
95
            $this->locales,
96
            Arr::wrap($locales)
97
        );
98
99
        return $this;
100
    }
101
102
    /**
103
     * Set the name of the routes.
104
     *
105
     * @param  string  $name
106
     * @return self
107
     */
108
    public function name(string $name) : self
109
    {
110
        $this->options['name'] = $name;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Set the method of the routes.
117
     *
118
     * @param  string  $method
119
     * @return self
120
     */
121
    public function method(string $method) : self
122
    {
123
        $this->options['method'] = $method;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Set the name of each locale for the routes.
130
     *
131
     * @param  array  $names
132
     * @return self
133
     */
134
    public function names(array $names) : self
135
    {
136
        $this->options['names'] = $names;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set the route for a list of locales only.
143
     *
144
     * @param  string|array  $locales
145
     * @return self
146
     */
147
    public function only($locales) : self
148
    {
149
        $this->options['locales'] = array_intersect(
150
            $this->locales,
151
            Arr::wrap($locales)
152
        );
153
154
        return $this;
155
    }
156
157
    /**
158
     * Handle the object's destruction.
159
     *
160
     * @return void
161
     */
162
    public function __destruct()
163
    {
164
        if (! $this->registered) {
165
            $this->register();
166
        }
167
    }
168
}
169