SplashRoute::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 14
nc 3
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
use Mouf\Mvc\Splash\Utils\SplashException;
6
7
/**
8
 * A callback used to access a page.
9
 *
10
 * @author David
11
 */
12
class SplashRoute implements SplashRouteInterface
13
{
14
    private $url;
15
16
    /**
17
     * List of HTTP methods allowed for this callback.
18
     * If empty, all methods are allowed.
19
     *
20
     * @var array<string>
21
     */
22
    private $httpMethods;
23
24
    private $controllerInstanceName;
25
26
    private $methodName;
27
28
    private $title;
29
30
    private $fullComment;
31
32
    /**
33
     * An ordered list of parameters.
34
     * The first parameter to be passed to the method will be fetched from values set $parameters[0], etc...
35
     *
36
     *
37
     * @var array<SplashParameterFetcherInterface>
38
     */
39
    private $parameters;
40
41
    /**
42
     * A list of all filters to apply to the route.
43
     *
44
     * @var array An array of filters.
45
     */
46
    private $filters;
47
48
    /**
49
     * The list of parameters matched during the route.
50
     * This is filled at runtime, by the SplashUrlNode class.
51
     *
52
     * @var array<string, string>
53
     */
54
    private $filledParameters = array();
55
    // Question: abstraire SplashRoute et rajouter un getCallbackHandler???
56
57
    /**
58
     * The file that contains the controller class.
59
     * Used to invalidate the cache.
60
     *
61
     * @var string
62
     */
63
    private $fileName;
64
65
    /**
66
     * @var int
67
     */
68
    private $fileModificationTime;
69
70
    public function __construct(string $url, string $controllerInstanceName, string $methodName, string $title = null, string $fullComment = null, array $httpMethods = array(), array $parameters = array(), array $filters = array(), string $fileName = null)
71
    {
72
        $this->url = $url;
73
        $this->httpMethods = $httpMethods;
74
        $this->controllerInstanceName = $controllerInstanceName;
75
        $this->methodName = $methodName;
76
        $this->title = $title;
77
        $this->fullComment = $fullComment;
78
        $this->parameters = $parameters;
79
        $this->filters = $filters;
80
81
        if ($fileName !== null) {
82
            $this->fileName = $fileName;
83
            $this->fileModificationTime = filemtime($fileName);
84
            if ($this->fileModificationTime === false) {
85
                throw new SplashException(sprintf('Could not find file modification time for "%s"', $this->fileName));
86
            }
87
        }
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getUrl() : string
94
    {
95
        return $this->url;
96
    }
97
98
    /**
99
     * List of HTTP methods allowed for this callback.
100
     * If empty, all methods are allowed.
101
     *
102
     * @return string[]
103
     */
104
    public function getHttpMethods() : array
105
    {
106
        return $this->httpMethods;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getControllerInstanceName() : string
113
    {
114
        return $this->controllerInstanceName;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getMethodName() : string
121
    {
122
        return $this->methodName;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getTitle()
129
    {
130
        return $this->title;
131
    }
132
133
    /**
134
     * @return string|null
135
     */
136
    public function getFullComment()
137
    {
138
        return $this->fullComment;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getParameters() : array
145
    {
146
        return $this->parameters;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getFilters() : array
153
    {
154
        return $this->filters;
155
    }
156
157
    /**
158
     * @return string[]
159
     */
160
    public function getFilledParameters() : array
161
    {
162
        return $this->filledParameters;
163
    }
164
165
    public function setFilledParameters(array $parameters)
166
    {
167
        $this->filledParameters = $parameters;
168
    }
169
170
    /**
171
     * Checks if the data stored in this route is fresh or not (it comes from the cache).
172
     *
173
     * @return bool
174
     */
175
    public function isCacheValid() : bool
176
    {
177
        if ($this->fileName === null) {
178
            return true;
179
        }
180
181
        return $this->fileModificationTime === filemtime($this->fileName);
182
    }
183
}
184