Completed
Pull Request — master (#34)
by Albin
03:25
created

RoutingNormalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 45
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C normalizeDeclaration() 0 25 8
A normalizeString() 0 16 2
1
<?php
2
3
namespace Knp\Rad\ResourceResolver;
4
5
class RoutingNormalizer
6
{
7
    public function normalizeDeclaration($declaration)
8
    {
9
        if (is_string($declaration)) {
10
            return $this->normalizeString($declaration);
11
        }
12
13
        if (array_keys($declaration) === array_keys(array_values($declaration))) {
14
            if (false === strpos($declaration[0], ':')) {
15
                throw new \RuntimeException('The first argument for a resource configuration, when expressed with a numerically indexed array, should be a string containing the service and the method used, seperated by a colon.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 180 characters; contains 230 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
16
            } elseif (isset($declaration[1]) && !is_array($declaration[1])) {
17
                throw new \RuntimeException('The second argument for a resource configuration, when expressed with a numerically indexed array, should be an array of arguments.');
18
            }
19
20
            list($service, $method) = explode(':', $declaration[0]);
21
22
            return [
23
                'service'   => $service,
24
                'method'    => $method,
25
                'arguments' => isset($declaration[1]) ? $declaration[1] : [],
26
                'required'  => isset($declaration[2]) ? (bool) $declaration[2] : true,
27
            ];
28
        }
29
30
        return array_merge(['required' => true, 'arguments' => []], $declaration);
31
    }
32
33
    private function normalizeString($declaration)
34
    {
35
        $service = $declaration;
36
        $method  = null;
37
38
        if (strpos($declaration, ':') !== false) {
39
            list($service, $method) = explode(':', $declaration);
40
        }
41
42
        return [
43
            'service'   => $service,
44
            'method'    => $method,
45
            'arguments' => [],
46
            'required'  => true,
47
        ];
48
    }
49
}
50