Completed
Push — master ( 8fd5ec...7383a6 )
by ARCANEDEV
7s
created

CurrencyCollection::loadMany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\Currencies\Entities;
2
3
use Arcanedev\Currencies\Exceptions\CurrencyNotFoundException;
4
use Arcanedev\Support\Collection;
5
use Arcanedev\Currencies\Contracts\Entities\Currency as CurrencyContract;
6
7
/**
8
 * Class     CurrencyCollection
9
 *
10
 * @package  Arcanedev\Currencies\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class CurrencyCollection extends Collection
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * @param  array  $currencies
21
     * @param  bool   $includeNonIso
22
     *
23
     * @return self
24
     */
25 180
    public function load(array $currencies, $includeNonIso = false)
26
    {
27 180
        $this->reset();
28
29 180
        foreach ($currencies as $group => $items) {
30 180
            if ($group !== 'iso' && ! $includeNonIso) continue;
31
32 180
            $this->loadMany($items, ($group === 'iso'));
33 135
        }
34
35 180
        return $this;
36
    }
37
38
    /**
39
     * Add a currency to the collection.
40
     *
41
     * @param  string  $key
42
     * @param  array   $attributes
43
     *
44
     * @return self
45
     */
46 180
    public function add($key, array $attributes)
47
    {
48 180
        $key        = strtoupper($key);
49 180
        $attributes = compact('key') + $attributes;
50
51 180
        $this->addOne(Currency::make($key, $attributes));
52
53 180
        return $this;
54
    }
55
56
    /**
57
     * Add a Currency object to the collection.
58
     *
59
     * @param  \Arcanedev\Currencies\Contracts\Entities\Currency  $currency
60
     *
61
     * @return self
62
     */
63 180
    public function addOne(CurrencyContract $currency)
64
    {
65 180
        $this->put($currency->key, $currency);
0 ignored issues
show
Bug introduced by
Accessing key on the interface Arcanedev\Currencies\Contracts\Entities\Currency suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66
67 180
        return $this;
68
    }
69
70
    /**
71
     * Check if the currency exists.
72
     *
73
     * @param  string  $iso
74
     *
75
     * @return \Arcanedev\Currencies\Entities\Currency
76
     *
77
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
78
     */
79 24
    public function findOrFails($iso)
80
    {
81 24
        $iso = strtoupper($iso);
82
83 24
        if ( ! $this->has($iso)) {
84 12
            throw new CurrencyNotFoundException("The Currency with the ISO code [$iso] not found");
85
        }
86
87 12
        return $this->get($iso);
88
    }
89
90
    /* ------------------------------------------------------------------------------------------------
91
     |  Other Functions
92
     | ------------------------------------------------------------------------------------------------
93
     */
94
    /**
95
     * Add many currencies.
96
     *
97
     * @param  array  $items
98
     * @param  bool   $isIso
99
     */
100 180
    protected function loadMany($items, $isIso = false)
101
    {
102 180
        foreach ($items as $key => $attributes) {
103 180
            $attributes = ['is_iso' => $isIso] + $attributes;
104
105 180
            $this->add($key, $attributes);
106 135
        }
107 180
    }
108
}
109