CurrencyCollection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 109
ccs 27
cts 27
cp 1
rs 10
wmc 11
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A load() 0 12 4
A add() 0 7 1
A addOne() 0 6 1
A findOrFails() 0 10 2
A loadMany() 0 8 2
1
<?php namespace Arcanedev\Currencies\Entities;
2
3
use Arcanedev\Currencies\Contracts\Entities\Currency as CurrencyContract;
4
use Arcanedev\Currencies\Exceptions\CurrencyNotFoundException;
5
use Arcanedev\Support\Collection;
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
     * Get a currency from the collection by iso code.
21
     *
22
     * @param  string      $iso
23
     * @param  mixed|null  $default
24
     *
25
     * @return \Arcanedev\Currencies\Contracts\Entities\Currency
26
     */
27 108
    public function get($iso, $default = null)
28
    {
29 108
        return parent::get($iso, $default);
30
    }
31
32
    /**
33
     * Load the currencies.
34
     *
35
     * @param  array  $currencies
36
     * @param  bool   $includeNonIso
37
     *
38
     * @return self
39
     */
40 612
    public function load(array $currencies, $includeNonIso = false)
41
    {
42 612
        $this->reset();
43
44 612
        foreach ($currencies as $group => $items) {
45 612
            if ($group !== 'iso' && ! $includeNonIso) continue;
46
47 612
            $this->loadMany($items, ($group === 'iso'));
48 459
        }
49
50 612
        return $this;
51
    }
52
53
    /**
54
     * Add a currency to the collection.
55
     *
56
     * @param  string  $key
57
     * @param  array   $attributes
58
     *
59
     * @return self
60
     */
61 612
    public function add($key, array $attributes)
62
    {
63 612
        $key        = strtoupper($key);
64 612
        $attributes = compact('key') + $attributes;
65
66 612
        return $this->addOne(Currency::make($key, $attributes));
67
    }
68
69
    /**
70
     * Add a Currency object to the collection.
71
     *
72
     * @param  \Arcanedev\Currencies\Contracts\Entities\Currency  $currency
73
     *
74
     * @return self
75
     */
76 612
    public function addOne(CurrencyContract $currency)
77
    {
78 612
        $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...
79
80 612
        return $this;
81
    }
82
83
    /**
84
     * Check if the currency exists.
85
     *
86
     * @param  string  $iso
87
     *
88
     * @return \Arcanedev\Currencies\Contracts\Entities\Currency
89
     *
90
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
91
     */
92 96
    public function findOrFails($iso)
93
    {
94 96
        $iso = strtoupper($iso);
95
96 96
        if ( ! $this->has($iso)) {
97 24
            throw new CurrencyNotFoundException("The Currency with the ISO code [$iso] not found");
98
        }
99
100 72
        return $this->get($iso);
101
    }
102
103
    /* ------------------------------------------------------------------------------------------------
104
     |  Other Functions
105
     | ------------------------------------------------------------------------------------------------
106
     */
107
    /**
108
     * Add many currencies.
109
     *
110
     * @param  array  $items
111
     * @param  bool   $isIso
112
     */
113 612
    protected function loadMany($items, $isIso = false)
114
    {
115 612
        foreach ($items as $key => $attributes) {
116 612
            $attributes = ['is_iso' => $isIso] + $attributes;
117
118 612
            $this->add($key, $attributes);
119 459
        }
120 612
    }
121
}
122