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

RatesConfigurationRegistry::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 8
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\Configuration;
13
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
14
use RunOpenCode\ExchangeRate\Utils\ConfigurationFilterUtil;
15
16
/**
17
 * Class RatesConfigurationRegistry
18
 *
19
 * Default implementation of rates configuration registry.
20
 *
21
 * @package RunOpenCode\ExchangeRate\Registry
22
 */
23
final class RatesConfigurationRegistry implements RatesConfigurationRegistryInterface
24
{
25
    /**
26
     * @var Configuration[]
27
     */
28
    private $configurations;
29
30
    public function __construct(array $configurations = array())
31
    {
32
        $this->configurations = array();
33
34
        foreach ($configurations as $configuration) {
35
            $this->add($configuration);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function add(Configuration $configuration)
43
    {
44
        $this->configurations[] = $configuration;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function all(array $filter = array())
51
    {
52
        if (count($filter) === 0) {
53
            return $this->configurations;
54
        }
55
56
        return $this->filter($this->configurations, $filter);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getIterator()
63
    {
64
        return new \ArrayIterator($this->configurations);
65
    }
66
67
    /**
68
     * Get configurations that matches given filter criteria.
69
     *
70
     * @param Configuration[] $configurations Configurations to filter.
71
     * @param array $criteria Filter criteria.
72
     * @return Configuration[] Matched configurations.
73
     */
74
    private function filter($configurations, array $criteria)
75
    {
76
        $result = array();
77
78
        /**
79
         * @var Configuration $configuration
80
         */
81
        foreach ($configurations as $configuration) {
82
83
            if (ConfigurationFilterUtil::matches($configuration, $criteria)) {
84
                $result[] = $configuration;
85
            }
86
        }
87
88
        return $result;
89
    }
90
}
91