RecordTrait::setCurrency()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ByTIC\Money\Models\Currencies\Traits\HasCurrencyCode;
4
5
use ByTIC\Money\Models\Currencies\CurrencyTrait;
6
use Nip\Records\Record;
7
8
/**
9
 * Class RecordTrait.
10
 *
11
 * @property string $amount
12
 * @property string $currency_code
13
 */
14
trait RecordTrait
15
{
16
    protected $_currency = null;
17
18
    /**
19
     * @return string
20
     */
21
    public function printCurrencyAmount()
22
    {
23
        return $this->getCurrency()->moneyHTMLFormat($this->getCurrencyAmount());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getCurrenc...s->getCurrencyAmount()) also could return the type Nip\Records\AbstractMode...\Collections\Collection which is incompatible with the documented return type string.
Loading history...
24
    }
25
26
    /**
27
     * @return CurrencyTrait|Record
28
     */
29
    public function getCurrency()
30
    {
31
        if ($this->_currency === null) {
32
            $this->initCurrency();
33
        }
34
35
        return $this->_currency;
36
    }
37
38
    /**
39
     * @param CurrencyTrait|Record $c
40
     */
41
    public function setCurrency(Record $c)
42
    {
43
        $this->_currency = $c;
44
        $this->setCurrencyCode($c->code);
45
    }
46
47
    public function initCurrency()
48
    {
49
        $this->_currency = currencyManager()->getByCode($this->getCurrencyCode());
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function getCurrencyAmount()
56
    {
57
        return $this->amount;
58
    }
59
60
    /**
61
     * @param $code
62
     *
63
     * @return $this
64
     */
65
    public function setCurrencyCode($code)
66
    {
67
        $c = currencyManager()->getByCode($code);
68
        if ($c) {
69
            $this->currency_code = $code;
70
            $this->_currency = $c;
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function getCurrencyCode()
80
    {
81
        return $this->currency_code;
82
    }
83
}
84