Completed
Push — master ( 862be6...6b5941 )
by Nikola
50:19 queued 47:05
created

SourcesRegistry::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate\Registry;
11
12
use RunOpenCode\ExchangeRate\Contract\SourceInterface;
13
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface;
14
use RunOpenCode\ExchangeRate\Exception\SourceNotAvailableException;
15
use RunOpenCode\ExchangeRate\Utils\SourceFilterUtil;
16
17
/**
18
 * Class SourcesRegistry
19
 *
20
 * Default implementation of sources registry.
21
 *
22
 * @package RunOpenCode\ExchangeRate\Registry
23
 */
24
final class SourcesRegistry implements SourcesRegistryInterface
25
{
26
    /**
27
     * @var SourceInterface[]
28
     */
29
    private $sources;
30
31
    public function __construct(array $sources = array())
32
    {
33
        $this->sources = array();
34
35
        foreach ($sources as $source) {
36
            $this->add($source);
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function add(SourceInterface $source)
44
    {
45
        if ($this->has($source->getName())) {
46
            throw new \RuntimeException(sprintf('Source "%s" is already registered.', $source->getName()));
47
        }
48
49
        $this->sources[$source->getName()] = $source;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function has($name)
56
    {
57
        return array_key_exists($name, $this->sources);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws SourceNotAvailableException
64
     */
65
    public function get($name)
66
    {
67
        if ($this->has($name)) {
68
            return $this->sources[$name];
69
        }
70
71
        throw new SourceNotAvailableException(sprintf('Unknown source requested: "%s".', $name));
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function all(array $filter = array())
78
    {
79
        if (count($filter) === 0) {
80
            return $this->sources;
81
        }
82
83
        return $this->filter($this->sources, $filter);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getIterator()
90
    {
91
        return new \ArrayIterator($this->sources);
92
    }
93
94
    /**
95
     * Filter sources.
96
     *
97
     * Available filter criteria:
98
     * * name: string
99
     * * names: string[]
100
     *
101
     * @param SourceInterface[] $sources Sources to filter.
102
     * @param array $filters Filter criteria.
103
     *
104
     * @return SourceInterface[]
105
     */
106
    private function filter($sources, array $filters = array())
107
    {
108
        $result = array();
109
110
        foreach ($sources as $source) {
111
112
            if (SourceFilterUtil::matches($source, $filters)) {
113
                $result[] = $source;
114
            }
115
        }
116
117
        return $result;
118
    }
119
}
120