HasSerializedCurrenciesTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 81
ccs 0
cts 28
cp 0
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initCurrencies() 0 6 2
A getCurrenciesModels() 0 7 2
A setCurrenciesArray() 0 3 1
A initCurrenciesArray() 0 3 1
A getCurrenciesArray() 0 7 2
A initCurrency() 0 3 1
A supportsCurrency() 0 5 1
A serializeCurrencies() 0 3 1
1
<?php
2
3
namespace ByTIC\Money\Models\Currencies\Traits;
4
5
use ByTIC\Money\Models\Currencies\CurrencyTrait;
6
7
/**
8
 * Class HasSerializedCurrenciesTrait.
9
 *
10
 * @property string $currencies
11
 */
12
trait HasSerializedCurrenciesTrait
13
{
14
    /**
15
     * @var null|CurrencyTrait[]
16
     */
17
    protected $currenciesModels = null;
18
    /**
19
     * @var null
20
     */
21
    protected $currenciesArray = null;
22
23
    /**
24
     * @param CurrencyTrait $currency
25
     *
26
     * @return bool
27
     */
28
    public function supportsCurrency($currency)
29
    {
30
        $currencies = $this->getCurrenciesModels();
31
32
        return isset($currencies[$currency->code]);
33
    }
34
35
    /**
36
     * @return null|CurrencyTrait[]
37
     */
38
    public function getCurrenciesModels()
39
    {
40
        if ($this->currenciesModels == null) {
41
            $this->initCurrencies();
42
        }
43
44
        return $this->currenciesModels;
45
    }
46
47
    public function initCurrencies()
48
    {
49
        $currenciesCodes = $this->getCurrenciesArray();
50
        $this->currenciesModels = [];
51
        foreach ($currenciesCodes as $code) {
52
            $this->currenciesModels[$code] = $this->initCurrency($code);
53
        }
54
    }
55
56
    /**
57
     * @return null|array
58
     */
59
    public function getCurrenciesArray()
60
    {
61
        if ($this->currenciesArray === null) {
62
            $this->initCurrenciesArray();
63
        }
64
65
        return $this->currenciesArray;
66
    }
67
68
    /**
69
     * @param $c
70
     */
71
    public function setCurrenciesArray($c)
72
    {
73
        $this->currenciesArray = $c;
74
    }
75
76
    public function initCurrenciesArray()
77
    {
78
        $this->currenciesArray = unserialize($this->currencies);
79
    }
80
81
    public function serializeCurrencies()
82
    {
83
        $this->currencies = serialize($this->currenciesArray);
84
    }
85
86
    /**
87
     * @param string $code
88
     * @return CurrencyTrait
89
     */
90
    public function initCurrency($code)
91
    {
92
        return currencyManager()->getByCode($code);
0 ignored issues
show
Bug Best Practice introduced by
The expression return currencyManager()->getByCode($code) returns the type Nip\Records\AbstractMode...ections\Collection|true which is incompatible with the documented return type ByTIC\Money\Models\Currencies\CurrencyTrait.
Loading history...
93
    }
94
}
95