Completed
Pull Request — master (#132)
by
unknown
01:33
created

RequestMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A map() 0 20 4
A create() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Zippy.
5
 *
6
 * (c) Alchemy <[email protected]>
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 Alchemy\Zippy\Resource;
13
14
class RequestMapper
15
{
16
    private $locator;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param TargetLocator $locator
22
     */
23
    public function __construct(TargetLocator $locator)
24
    {
25
        $this->locator = $locator;
26
    }
27
28
    /**
29
     * Maps resources request to a ResourceCollection
30
     *
31
     * @param       $context
32
     * @param array $resources
33
     *
34
     * @return ResourceCollection
35
     */
36
    public function map($context, array $resources)
37
    {
38
        $data = array();
39
40
        foreach ($resources as $location => $resource) {
41
            if (is_int($location)) {
42
                $data[] = new Resource($resource, $this->locator->locate($context, $resource));
43
            } else {
44
                $data[] = new Resource($resource, ltrim($location, '/'));
45
            }
46
        }
47
48
        if (count($data) === 1) {
49
            $context = $data[0]->getOriginal();
50
        }
51
52
        $collection = new ResourceCollection($context, $data, false);
53
54
        return $collection;
55
    }
56
57
    /**
58
     * Creates the default RequestMapper
59
     *
60
     * @return RequestMapper
61
     */
62
    public static function create()
63
    {
64
        return new static(new TargetLocator());
65
    }
66
}
67