ArrayUriMap   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getUri() 0 13 5
1
<?php
2
3
/*
4
 * This file is part of the Lakion package.
5
 *
6
 * (c) Lakion
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Api\Map;
13
14
/**
15
 * @author Michał Marcinkowski <[email protected]>
16
 */
17
class ArrayUriMap implements UriMapInterface
18
{
19
    /**
20
     * @var array $uriMapping
21
     */
22
    private $uriMapping;
23
    /**
24
     * @var bool $allowDefaultUris
25
     */
26
    private $allowDefaultUris;
27
28
    /**
29
     * @param array $uriMapping
30
     * @param bool  $allowDefaultUris
31
     */
32
    public function __construct(array $uriMapping, $allowDefaultUris = true)
33
    {
34
        $this->uriMapping = $uriMapping;
35
        $this->allowDefaultUris = $allowDefaultUris;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getUri($resource)
42
    {
43
        if (empty($resource) || !is_string($resource)) {
44
            throw new \InvalidArgumentException('The resource has to be string and cannot be empty.');
45
        }
46
        if (isset($this->uriMapping[$resource])) {
47
            return $this->uriMapping[$resource];
48
        }
49
        if ($this->allowDefaultUris) {
50
            return $resource;
51
        }
52
        throw new \InvalidArgumentException('No mapping defined for a given resource.');
53
    }
54
}
55