UrlGenerator::to()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Endeavors\Components\Routing;
4
5
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
6
use DateInterval;
7
use DateTimeInterface;
8
use Carbon\Carbon;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\RouteCollection;
11
use Illuminate\Contracts\Routing\UrlRoutable;
12
13
/**
14
 * Decorate the UrlGenerator for simplicity
15
 */
16
class UrlGenerator implements UrlGeneratorContract, IEnableRoutes
17
{
18
    private $originalUrlGenerator;
19
20
    public function __construct(UrlGeneratorContract $originalUrlGenerator)
21
    {
22
        $this->originalUrlGenerator = $originalUrlGenerator;
23
    }
24
25
    /**
26
     * Get the full URL for the current request.
27
     *
28
     * @return string
29
     */
30
    public function full()
31
    {
32
        return $this->originalUrlGenerator->full();
33
    }
34
35
    /**
36
     * Get the current URL for the request.
37
     *
38
     * @return string
39
     */
40
    public function current()
41
    {
42
        return $this->originalUrlGenerator->current();
43
    }
44
45
    /**
46
     * Get the URL for the previous request.
47
     *
48
     * @return string
49
     */
50
    public function previous($fallback = false)
51
    {
52
        return $this->originalUrlGenerator->previous($fallback);
53
    }
54
55
    /**
56
     * Generate a absolute URL to the given path.
57
     *
58
     * @param  string  $path
59
     * @param  mixed  $extra
60
     * @param  bool|null  $secure
61
     * @return string
62
     */
63
    public function to($path, $extra = array(), $secure = null)
64
    {
65
        return $this->originalUrlGenerator->to($path, $extra, $secure);
66
    }
67
68
    /**
69
     * Generate a secure, absolute URL to the given path.
70
     *
71
     * @param  string  $path
72
     * @param  array   $parameters
73
     * @return string
74
     */
75
    public function secure($path, $parameters = array())
76
    {
77
        return $this->originalUrlGenerator->secure($path, $parameters);
78
    }
79
80
    /**
81
     * Generate a URL to an application asset.
82
     *
83
     * @param  string  $path
84
     * @param  bool|null  $secure
85
     * @return string
86
     */
87
    public function asset($path, $secure = null)
88
    {
89
        return $this->originalUrlGenerator->asset($path, $secure);
90
    }
91
92
    /**
93
     * Generate a URL to a secure asset.
94
     *
95
     * @param  string  $path
96
     * @return string
97
     */
98
    public function secureAsset($path)
99
    {
100
        return $this->originalUrlGenerator->secureAsset($path);
101
    }
102
103
    /**
104
     * Force the schema for URLs.
105
     *
106
     * @param  string  $schema
107
     * @return void
108
     */
109
    public function forceSchema($schema)
110
    {
111
        $this->originalUrlGenerator->forceSchema($schema);
112
    }
113
114
    /**
115
     * Get the URL to a named route.
116
     *
117
     * @param  string  $name
118
     * @param  mixed   $parameters
119
     * @param  bool  $absolute
120
     * @param  \Illuminate\Routing\Route  $route
121
     * @return string
122
     *
123
     * @throws \InvalidArgumentException
124
     */
125
    public function route($name, $parameters = array(), $absolute = true, $route = null)
126
    {
127
        return $this->originalUrlGenerator->route($name, $parameters, $absolute, $route);
128
    }
129
130
    /**
131
     * Get the URL to a controller action.
132
     *
133
     * @param  string  $action
134
     * @param  mixed   $parameters
135
     * @param  bool    $absolute
136
     * @return string
137
     */
138
    public function action($action, $parameters = array(), $absolute = true)
139
    {
140
        return $this->originalUrlGenerator->action($action, $parameters, $absolute);
141
    }
142
143
    /**
144
     * Set the forced root URL.
145
     *
146
     * @param  string  $root
147
     * @return void
148
     */
149
    public function forceRootUrl($root)
150
    {
151
        $this->originalUrlGenerator->forceRootUrl($root);
152
    }
153
154
    /**
155
     * Determine if the given path is a valid URL.
156
     *
157
     * @param  string  $path
158
     * @return bool
159
     */
160
    public function isValidUrl($path)
161
    {
162
        return $this->originalUrlGenerator->isValidUrl($path);
163
    }
164
165
    /**
166
     * Get the request instance.
167
     *
168
     * @return \Symfony\Component\HttpFoundation\Request
169
     */
170
    public function getRequest()
171
    {
172
        return $this->originalUrlGenerator->getRequest();
173
    }
174
175
    /**
176
     * Set the current request instance.
177
     *
178
     * @param  \Illuminate\Http\Request  $request
179
     * @return void
180
     */
181
    public function setRequest(Request $request)
182
    {
183
        $this->originalUrlGenerator->setRequest($request);
184
    }
185
186
    public function setRoutes(RouteCollection $routes)
187
    {
188
        return $this->originalUrlGenerator->setRoutes($routes);
189
    }
190
191
    public function setSessionResolver(callable $sessionResolver)
192
    {
193
        return $this->originalUrlGenerator->setSessionResolver($sessionResolver);
194
    }
195
196
    public function setRootControllerNamespace($rootNamespace)
197
    {
198
        return $this->originalUrlGenerator->setRootControllerNamespace($rootNamespace);
199
    }
200
201
    public function getOriginalUrlGenerator()
202
    {
203
        return $this->originalUrlGenerator;
204
    }
205
206
    /**
207
     * @return bool
208
     */
209
    public function hasValidSignature(Request $request)
210
    {
211
        $validator = new SignatureValidator($request);
212
        $validator->setKeyResolver($this->keyResolver);
213
        return $validator->passes();
214
    }
215
    
216
    /**
217
     * Only check validity of signature if specified parameters exist
218
     */
219
    public function hasValidParameterSignature(Request $request, array $parameters = [])
220
    {
221
        // we'll bail here as we need at least one
222
        if (count($parameters) === 0) {
223
            return true;
224
        } 
225
226
        foreach ($parameters as $parameter) {
227
            // if the request has the parameter or
228
            // the route has the parameter we check the signature
229
            if ($request->has($parameter)) { 
230
                return $this->hasValidSignature($request); 
231
            }
232
233
            if ($this->routeHasParameter($request, $parameter)) {
234
                return $this->hasValidSignature($request);
235
            }
236
        }
237
238
        return true;
239
    }
240
241
    /**
242
     * Create a signed route URL for a named route.
243
     *
244
     * @param  string  $name
245
     * @param  array  $parameters
246
     * @param  \DateTimeInterface|int  $expiration
247
     * @return string
248
     */
249
    public function signedRoute($name, $parameters = [], $expiration = null)
250
    {
251
        $parameters = $this->formatParameters($parameters);
252
        if ($expiration) {
253
            $parameters = $parameters + ['expires' => $this->availableAt($expiration)];
254
        }
255
        ksort($parameters);
256
        $key = call_user_func($this->keyResolver);
257
        return $this->route($name, $parameters + [
258
            'signature' => hash_hmac('sha256', $this->route($name, $parameters), $key),
259
        ]);
260
    }
261
    /**
262
     * Create a temporary signed route URL for a named route.
263
     *
264
     * @param  string  $name
265
     * @param  \DateTimeInterface|int  $expiration
266
     * @param  array  $parameters
267
     * @return string
268
     */
269
    public function temporarySignedRoute($name, $expiration, $parameters = [])
270
    {
271
        return $this->signedRoute($name, $parameters, $expiration);
272
    }
273
274
    /**
275
     * Format the array of URL parameters.
276
     *
277
     * @param  mixed|array  $parameters
278
     * @return array
279
     */
280
    public function formatParameters($parameters)
281
    {
282
        $parameters = Arr::wrap($parameters);
283
        foreach ($parameters as $key => $parameter) {
284
            if ($parameter instanceof UrlRoutable) {
285
                $parameters[$key] = $parameter->getRouteKey();
286
            }
287
        }
288
        return $parameters;
289
    }
290
291
    /**
292
     * Get the "available at" UNIX timestamp.
293
     *
294
     * @param  \DateTimeInterface|\DateInterval|int  $delay
295
     * @return int
296
     */
297
    protected function availableAt($delay = 0)
298
    {
299
        $delay = $this->parseDateInterval($delay);
300
        return $delay instanceof DateTimeInterface
0 ignored issues
show
introduced by
$delay is always a sub-type of DateTimeInterface.
Loading history...
301
                            ? $delay->getTimestamp()
302
                            : Carbon::now()->addSeconds($delay)->getTimestamp();
303
    }
304
305
    /**
306
     * If the given value is an interval, convert it to a DateTime instance.
307
     *
308
     * @param  \DateTimeInterface|\DateInterval|int  $delay
309
     * @return \DateTimeInterface|int
310
     */
311
    protected function parseDateInterval($delay)
312
    {
313
        if ($delay instanceof DateInterval) {
314
            $delay = Carbon::now()->add($delay);
315
        }
316
        return $delay;
317
    }
318
    
319
    /**
320
     * Determine if the route from a request has a parameter
321
     * 
322
     * @param \Illuminate\Http\Request $request
323
     * @param string  $parameter
324
     */
325
    protected function routeHasParameter(Request $request, $parameter)
326
    {
327
        $result = false;
328
329
        $route = $request->route($parameter);
330
331
        if ($route instanceof \Illuminate\Routing\Route && $route->hasParameter($parameter)) {
332
            $result = true;
333
        } elseif (is_string($route) && strlen($route) > 0) {
334
            $result = true;
335
        }
336
337
        return $result;
338
    }
339
340
    /**
341
     * The encryption key resolver callable.
342
     *
343
     * @var callable
344
     */
345
    protected $keyResolver;
346
347
    /**
348
     * Set the encryption key resolver.
349
     *
350
     * @param  callable  $keyResolver
351
     * @return $this
352
     */
353
    public function setKeyResolver(callable $keyResolver)
354
    {
355
        $this->keyResolver = $keyResolver;
356
        return $this;
357
    }
358
}
359