FiatRatesCollection::check()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
4
namespace Sarnado\Converter\Collections;
5
6
7
use Sarnado\Converter\Objects\CryptoRateObject;
8
use Sarnado\Converter\Objects\FiatRateObject;
9
use Tightenco\Collect\Support\Collection;
10
11
/**
12
 * Class FiatRatesCollection
13
 * @package Sarnado\Converter\Collections
14
 */
15
class FiatRatesCollection extends Collection
16
{
17
    /**
18
     * FiatRatesCollection constructor.
19
     * @param array $data
20
     */
21 30
    public function __construct(array $data)
22
    {
23 30
        if ($this->check($data))
24
        {
25 18
            parent::__construct($data);
26
        }
27
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
28
        {
29 12
            throw new \InvalidArgumentException('Item from array must be instance of FiatRateObject');
30
        }
31 18
    }
32
33
    /**
34
     * @param array $data
35
     * @return bool
36
     */
37 30
    private function check(array $data): bool
38
    {
39 30
        if (!empty($data))
40
        {
41 18
            foreach ($data as $item)
42
            {
43 18
                if (!($item instanceof FiatRateObject))
44
                {
45 14
                    return false;
46
                }
47
            }
48
        }
49 18
        return true;
50
    }
51
52 6
    public function filterByPair(string $from, string $to): FiatRatesCollection
53
    {
54
        return $this->filter(function (FiatRateObject $rate) use ($from, $to)
55
        {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
56 3
            return $rate->getFrom() === $from && $rate->getTo() === $to;
57 6
        });
58
    }
59
}
60