Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

RoutingResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 48
ccs 0
cts 23
cp 0
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 15 1
A __construct() 0 3 1
B resolveOptions() 0 19 9
1
<?php
2
3
namespace LAG\AdminBundle\Routing\Resolver;
4
5
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
6
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
7
use LAG\AdminBundle\Exception\Exception;
8
9
class RoutingResolver implements RoutingResolverInterface
10
{
11
    /**
12
     * @var ApplicationConfiguration
13
     */
14
    private $configuration;
15
16
    public function __construct(ApplicationConfigurationStorage $storage)
17
    {
18
        $this->configuration = $storage->getConfiguration();
19
    }
20
21
    public function resolve(string $adminName, string $actionName): string
22
    {
23
        $routeName = str_replace(
24
            '{admin}',
25
            strtolower($adminName),
26
            $this->configuration->get('routing_name_pattern')
27
        );
28
29
        $routeName = str_replace(
30
            '{action}',
31
            $actionName,
32
            $routeName
33
        );
34
35
        return $routeName;
36
    }
37
38
    public function resolveOptions(array $options): ?string
39
    {
40
        if (key_exists('route', $options) && $options['route']) {
41
            return $options['route'];
42
        }
43
44
        if ((key_exists('uri', $options) && $options['uri']) || (key_exists('url', $options) && $options['url'])) {
45
            return null;
46
        }
47
48
        if (!key_exists('admin', $options)) {
49
            throw new Exception('Cannot resolve options, missing "admin" key');
50
        }
51
52
        if (!key_exists('action', $options)) {
53
            throw new Exception('Cannot resolve options, missing "action" key');
54
        }
55
56
        return $this->resolve($options['admin'], $options['action']);
57
    }
58
}
59