Completed
Push — master ( 7e4951...f1eb70 )
by Dmitry
12:23 queued 07:38
created

QuantityFormatterFactory::forConsumption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\logic\bill;
4
5
use hipanel\modules\finance\forms\BillForm;
6
use hipanel\modules\finance\models\Bill;
7
use hipanel\modules\finance\models\Charge;
8
use hipanel\modules\server\models\Consumption;
9
use hiqdev\php\units\Quantity;
10
use hiqdev\php\units\yii2\formatters\IntlFormatter;
11
12
/**
13
 * Class QuantityFormatterFactory
14
 */
15
final class QuantityFormatterFactory implements QuantityFormatterFactoryInterface
16
{
17
    /**
18
     * @var array maps bill type to a QuantityFormatter
19
     * // TODO: use DI to configure
20
     */
21
    private $types = [
22
        'support_time' => SupportTimeQuantity::class,
23
        'server_traf_max' => DefaultQuantityFormatter::class,
24
        'server_traf95_max' => DefaultQuantityFormatter::class,
25
        'backup_du' => DefaultQuantityFormatter::class,
26
        'server_du' => DefaultQuantityFormatter::class,
27
        'hw_purchase' => DefaultQuantityFormatter::class,
28
        'ip_num' => IPNumQuantity::class,
29
        'monthly' => MonthlyQuantity::class,
30
        'drenewal' => DomainRenewalQuantity::class,
31
    ];
32
    /**
33
     * @var IntlFormatter
34
     */
35
    private $intlFormatter;
36
37
    public function __construct(IntlFormatter $intlFormatter)
38
    {
39
        $this->intlFormatter = $intlFormatter;
40
    }
41
42
    /** {@inheritdoc} */
43
    public function create($model): ?QuantityFormatterInterface
44
    {
45
        if ($model instanceof Bill || $model instanceof BillForm) {
46
            return $this->forBill($model);
47
        }
48
49
        if ($model instanceof Charge) {
50
            return $this->forCharge($model);
51
        }
52
53
        throw new \InvalidArgumentException('Create is not supported for the passed model');
54
    }
55
56
    /**
57
     * @param Bill|BillForm $bill
58
     * @return QuantityFormatterInterface|null
59
     */
60
    public function forBill($bill): ?QuantityFormatterInterface
61
    {
62
        return $this->createByType($bill->type, Quantity::create($bill->unit, $bill->quantity), $bill);
63
    }
64
65
    public function forCharge(Charge $charge): ?QuantityFormatterInterface
66
    {
67
        return $this->createByType($charge->type, Quantity::create($charge->unit, $charge->quantity), $charge);
68
    }
69
70
    public function forConsumption(Consumption $consumption)
71
    {
72
        return $this->createByType($consumption->type, Quantity::create($consumption->unit, $consumption->quantity), $consumption);
73
    }
74
75
    /** {@inheritdoc} */
76
    public function createByType(string $type, Quantity $quantity, $context = null): ?QuantityFormatterInterface
77
    {
78
        $type = $this->fixType($type);
79
        if ($className = $this->types[$type] ?? null) {
80
            /** @var QuantityFormatterInterface $formatter */
81
            $formatter = new $className($quantity, $this->intlFormatter);
82
83
            if ($formatter instanceof ContextAwareQuantityFormatter) {
84
                $formatter->setContext($context);
85
            }
86
87
            return $formatter;
88
        }
89
90
        return null;
91
    }
92
93
    private function fixType($type): string
94
    {
95
        if (strpos($type, ',') !== false) {
96
            $types = explode(',', $type);
97
            return end($types);
98
        }
99
100
        return $type;
101
    }
102
}
103