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

RoutingResolver::resolveOptions()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 0
cts 10
cp 0
crap 90
rs 8.0555
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