Completed
Pull Request — master (#34)
by Albin
02:07
created

RoutingNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeDeclaration() 0 14 3
A normalizeString() 0 16 2
B normalizeArray() 0 18 6
1
<?php
2
3
namespace Knp\Rad\ResourceResolver;
4
5
class RoutingNormalizer
6
{
7
    /**
8
     * Normalizes string and array declarations into associative array.
9
     *
10
     * @param string|array $declaration
11
     *
12
     * @return array
13
     */
14
    public function normalizeDeclaration($declaration)
15
    {
16
        if (is_string($declaration)) {
17
            return $this->normalizeString($declaration);
18
        }
19
20
        // Normalize numerically indexed array
21
        if (array_keys($declaration) === array_keys(array_values($declaration))) {
22
            return $this->normalizeArray($declaration);
23
        }
24
25
        // Adds default value to associative array
26
        return array_merge(['required' => true, 'arguments' => []], $declaration);
27
    }
28
29
    private function normalizeString($declaration)
30
    {
31
        $service = $declaration;
32
        $method  = null;
33
34
        if (strpos($declaration, ':') !== false) {
35
            list($service, $method) = explode(':', $declaration);
36
        }
37
38
        return [
39
            'service'   => $service,
40
            'method'    => $method,
41
            'arguments' => [],
42
            'required'  => true,
43
        ];
44
    }
45
46
    private function normalizeArray($declaration)
47
    {
48
        $service = $declaration[0];
49
        $method  = null;
50
51
        if (false !== strpos($declaration[0], ':')) {
52
 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 215 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...
53
        } elseif (isset($declaration[1]) && !is_array($declaration[1])) {
54
            throw new \RuntimeException('The second argument for a resource configuration, when expressed with a numerically indexed array, should be an array of arguments.');
55
        }
56
57
        return [
58
            'service'   => $service,
59
            'method'    => $method,
60
            'arguments' => isset($declaration[1]) ? $declaration[1] : [],
61
            'required'  => isset($declaration[2]) ? (bool) $declaration[2] : true,
62
        ];
63
    }
64
}
65