Completed
Branch feature/moar-test-optimizing (8cffd1)
by Lucas
19:31 queued 13:44
created

ExtReferenceConverter::getUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.3931
Metric Value
dl 0
loc 17
ccs 7
cts 13
cp 0.5385
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2.3931
1
<?php
2
/**
3
 * ExtReferenceConverter class file
4
 */
5
6
namespace Graviton\DocumentBundle\Service;
7
8
use Graviton\DocumentBundle\Entity\ExtReference;
9
use Symfony\Component\Routing\RouterInterface;
10
use Symfony\Component\Routing\Route;
11
12
/**
13
 * Extref converter
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class ExtReferenceConverter implements ExtReferenceConverterInterface
20
{
21
    /**
22
     * @var RouterInterface
23
     */
24
    private $router;
25
    /**
26
     * @var array
27
     */
28
    private $mapping;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param RouterInterface $router  Router
34
     * @param array           $mapping colleciton_name => service_id mapping
35
     */
36 10
    public function __construct(RouterInterface $router, array $mapping)
37
    {
38 10
        $this->router = $router;
39 10
        $this->mapping = $mapping;
40 10
    }
41
42
    /**
43
     * return the extref from URL
44
     *
45
     * @param string $url Extref URL
46
     * @return ExtReference
47
     * @throws \InvalidArgumentException
48
     */
49 6
    public function getExtReference($url)
50
    {
51 6
        $path = parse_url($url, PHP_URL_PATH);
52 6
        if ($path === false) {
53
            throw new \InvalidArgumentException(sprintf('URL %s', $url));
54
        }
55
56 6
        $id = null;
57 6
        $collection = null;
58
59 6
        foreach ($this->router->getRouteCollection()->all() as $route) {
60 6
            list($collection, $id) = $this->getDataFromRoute($route, $path);
61 6
            if ($collection !== null && $id !== null) {
62 6
                return ExtReference::create($collection, $id);
63
            }
64 1
        }
65
66
        throw new \InvalidArgumentException(sprintf('Could not read URL %s', $url));
67
    }
68
69
    /**
70
     * return the URL from extref
71
     *
72
     * @param ExtReference $extReference Extref
73
     * @return string
74
     * @throws \InvalidArgumentException
75
     */
76 4
    public function getUrl(ExtReference $extReference)
77
    {
78 4
        if (!isset($this->mapping[$extReference->getRef()])) {
79
            throw new \InvalidArgumentException(
80
                sprintf(
81
                    'Could not create URL from extref "%s"',
82
                    json_encode($extReference)
83
                )
84
            );
85
        }
86
87 4
        return $this->router->generate(
88 4
            $this->mapping[$extReference->getRef()].'.get',
89 4
            ['id' => $extReference->getId()],
90 2
            true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91 2
        );
92
    }
93
94
    /**
95
     * get collection and id from route
96
     *
97
     * @param Route  $route route to look at
98
     * @param string $value value of reference as URI
99
     *
100
     * @return array
101
     */
102 6
    private function getDataFromRoute(Route $route, $value)
103
    {
104 6
        if ($route->getRequirement('id') !== null &&
105 6
            $route->getMethods() === ['GET'] &&
106 6
            preg_match($route->compile()->getRegex(), $value, $matches)
107 3
        ) {
108 6
            $id = $matches['id'];
109
110 6
            list($routeService) = explode(':', $route->getDefault('_controller'));
111 6
            list($core, $bundle,,$name) = explode('.', $routeService);
112 6
            $serviceName = implode('.', [$core, $bundle, 'rest', $name]);
113 6
            $collection = array_search($serviceName, $this->mapping);
114
115 6
            return [$collection, $id];
116
        }
117
118 2
        return [null, null];
119
    }
120
}
121