Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

RouterPathTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 14 2
A getRouteArguments() 0 12 3
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service\UriGenerator\Transformer;
11
12
use Aura\Router\Exception\RouteNotFound;
13
use Aura\Router\RouterContainer;
14
use Psr\Http\Message\UriInterface;
15
use Slick\Http\Uri;
16
use Slick\Mvc\Service\UriGenerator\LocationTransformerInterface;
17
18
/**
19
 * RouterPathTransformer
20
 *
21
 * @package Slick\Mvc\Service\UriGenerator\Transformer
22
 * @author  Filipe Silva <[email protected]>
23
 */
24
class RouterPathTransformer extends AbstractLocationTransformer implements
25
    LocationTransformerInterface
26
{
27
28
    /**
29
     * @var RouterContainer
30
     */
31
    private $router;
32
33
    /**
34
     * Router Path Transformer
35
     *
36
     * @param RouterContainer $router
37
     */
38
    public function __construct(RouterContainer $router)
39
    {
40
        $this->router = $router;
41
    }
42
43
    /**
44
     * Tries to transform the provided location data into a server URI
45
     *
46
     * @param string $location Location name, path or identifier
47
     * @param array $options Filter options
48
     *
49
     * @return UriInterface|null
50
     */
51
    public function transform($location, array $options = [])
52
    {
53
        try {
54
            $path = $this->router
55
                ->getGenerator()
56
                ->generate($location, $this->getRouteArguments($options));
57
        } catch (RouteNotFound $caught) {
58
            return null;
59
        }
60
61
        $uri = (new Uri())->withPath($path);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->router->getGenera...uteArguments($options)) on line 54 can also be of type false; however, Slick\Http\Uri::withPath() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
62
        $uri = $this->updateOptions($uri);
63
        return $uri;
64
    }
65
66
    /**
67
     * Get the route arguments from provided options
68
     *
69
     * For options that match the default options those will
70
     * override the default value.
71
     *
72
     * @param array $options
73
     *
74
     * @return array
75
     */
76
    private function getRouteArguments(array $options = [])
77
    {
78
        $routeArguments = [];
79
        foreach ($options as $key => $value) {
80
            if (array_key_exists($key, $this->options)) {
81
                $this->options[$key] = $value;
82
                continue;
83
            }
84
            $routeArguments[$key] = $value;
85
        }
86
        return $routeArguments;
87
    }
88
}