Completed
Push — master ( 7b09e3...7e39a8 )
by Andrii
12:47
created

MoneyQuantity::getAmount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\logic\bill;
12
13
use Money\Formatter\DecimalMoneyFormatter;
14
use Money\Money;
15
use Money\Currency;
16
use Yii;
17
18
/**
19
 * Class MoneyQuantity.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class MoneyQuantity extends DefaultQuantityFormatter
24
{
25
    public function format(): string
26
    {
27
        $formatter = Yii::$container->get(DecimalMoneyFormatter::class);
28
        $sum = $formatter->format($this->getAmount());
29
30
        return Yii::$app->formatter->asCurrency($sum, $this->getCurrency());
31
    }
32
33
    public function getAmount(): Money
34
    {
35
        $quantity = $this->getQuantity();
36
        $amount = $quantity->getQuantity();
37
38
        return new Money($amount, new Currency($this->getCurrency()));
39
    }
40
41
    public function getCurrency(): string
42
    {
43
        return strtoupper($this->getQuantity()->getUnit()->getName());
44
    }
45
}
46