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

RateFilterUtil::matchesArrayCriteria()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
crap 2
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\Utils;
11
12
use RunOpenCode\ExchangeRate\Contract\RateInterface;
13
14
/**
15
 * Class RateFilterUtil
16
 *
17
 * Utility to check if rate matches filters.
18
 *
19
 * @package RunOpenCode\ExchangeRate\Utils
20
 */
21
final class RateFilterUtil
22
{
23
    use FilterUtilHelper;
24
25
    private function __construct() { }
26
27
    /**
28
     * Check if rate matches filters.
29
     *
30
     * @param RateInterface $rate Rate to filter.
31
     * @param array $criteria Filter criteria.
32 12
     * @return bool TRUE if filter criteria is matched.
33
     */
34
    public static function matches(RateInterface $rate, array $criteria)
35 12
    {
36 6
        return
37 12
            self::matchesArrayCriteria('currencyCode', $rate, $criteria)
38 6
            &&
39 12
            self::matchesArrayCriteria('sourceName', $rate, $criteria)
40 6
            &&
41 12
            self::matchesArrayCriteria('rateType', $rate, $criteria)
42 6
            &&
43 12
            self::matchesDateCriteria('onDate', $rate, $criteria)
44 6
            &&
45 12
            self::matchesDateCriteria('dateFrom', $rate, $criteria)
46 6
            &&
47
            self::matchesDateCriteria('dateTo', $rate, $criteria)
48
            ;
49
    }
50
51
    /**
52
     * Check if date criteria is matched.
53
     *
54
     * @param string $key Date criteria key.
55
     * @param RateInterface $rate Rate to check for match.
56
     * @param array $criteria Filter criterias.
57 12
     * @return bool TRUE if there is a match.
58
     */
59 12
    private static function matchesDateCriteria($key, RateInterface $rate, array $criteria)
60
    {
61 12
        $date = self::extractDateCriteria($key, $criteria);
62 12
63
        if ($date === null) {
64
            return true;
65 2
        }
66 2
67 2
        if ($key === 'dateFrom') {
68 2
            return $date <= $rate->getDate();
69
        } elseif ($key === 'dateTo') {
70
            return $date >= $rate->getDate();
71 2
        }
72
73
        return $date->format('Y-m-d') === $rate->getDate()->format('Y-m-d');
74
    }
75
}
76