QuantityFormatterFactory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 104
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 12 4
A forBill() 0 4 1
A forCharge() 0 4 1
A forConsumption() 0 4 1
A createByType() 0 23 5
A fixType() 0 10 2
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 hipanel\modules\finance\forms\BillForm;
14
use hipanel\modules\finance\models\Bill;
15
use hipanel\modules\finance\models\Charge;
16
use hipanel\modules\server\models\Consumption;
17
use hiqdev\php\units\Quantity;
18
use hiqdev\php\units\yii2\formatters\IntlFormatter;
19
20
/**
21
 * Class QuantityFormatterFactory.
22
 */
23
final class QuantityFormatterFactory implements QuantityFormatterFactoryInterface
24
{
25
    /**
26
     * @var array maps bill type to a QuantityFormatter
27
     * // TODO: use DI to configure
28
     */
29
    private $types = [
30
        'monthly'           => MonthlyQuantity::class,
31
32
        'monthly,rack_unit' => RackUnitQuantity::class,
33
        'ip_num'            => IPNumQuantity::class,
34
        'support_time'      => SupportTimeQuantity::class,
35
        'referral'          => MoneyQuantity::class,
36
        'server_traf_max'   => DefaultQuantityFormatter::class,
37
        'server_traf95_max' => DefaultQuantityFormatter::class,
38
        'cdn_traf_max'      => DefaultQuantityFormatter::class,
39
        'backup_du'         => DefaultQuantityFormatter::class,
40
        'server_du'         => DefaultQuantityFormatter::class,
41
        'win_license'       => DefaultQuantityFormatter::class,
42
        'hw_purchase'       => DefaultQuantityFormatter::class,
43
        'server_ssd'        => DefaultQuantityFormatter::class,
44
        'server_sata'       => DefaultQuantityFormatter::class,
45
46
        'drenewal'          => DomainRenewalQuantity::class,
47
    ];
48
    /**
49
     * @var IntlFormatter
50
     */
51
    private $intlFormatter;
52
53
    public function __construct(IntlFormatter $intlFormatter)
54
    {
55
        $this->intlFormatter = $intlFormatter;
56
    }
57
58
    /** {@inheritdoc} */
59
    public function create($model): ?QuantityFormatterInterface
60
    {
61
        if ($model instanceof Bill || $model instanceof BillForm) {
62
            return $this->forBill($model);
63
        }
64
65
        if ($model instanceof Charge) {
66
            return $this->forCharge($model);
67
        }
68
69
        throw new \InvalidArgumentException('Create is not supported for the passed model');
70
    }
71
72
    /**
73
     * @param Bill|BillForm $bill
74
     * @return QuantityFormatterInterface|null
75
     */
76
    public function forBill($bill): ?QuantityFormatterInterface
77
    {
78
        return $this->createByType($bill->type, Quantity::create($bill->unit, $bill->quantity), $bill);
79
    }
80
81
    public function forCharge(Charge $charge): ?QuantityFormatterInterface
82
    {
83
        return $this->createByType($charge->ftype ?? $charge->type, Quantity::create($charge->unit, $charge->quantity), $charge);
84
    }
85
86
    public function forConsumption(Consumption $consumption): ?QuantityFormatterInterface
87
    {
88
        return $this->createByType($consumption->type, Quantity::create($consumption->unit, $consumption->quantity), $consumption);
89
    }
90
91
    /** {@inheritdoc} */
92
    public function createByType(string $type, Quantity $quantity, $context = null): ?QuantityFormatterInterface
93
    {
94
        if (!isset($this->types[$type])) {
95
            if (strpos($type, 'monthly,') === 0) {
96
                $type = 'monthly';
97
            } else {
98
                $type = $this->fixType($type);
99
            }
100
        }
101
102
        if ($className = $this->types[$type] ?? null) {
103
            /** @var QuantityFormatterInterface $formatter */
104
            $formatter = new $className($quantity, $this->intlFormatter);
105
106
            if ($formatter instanceof ContextAwareQuantityFormatter) {
107
                $formatter->setContext($context);
108
            }
109
110
            return $formatter;
111
        }
112
113
        return null;
114
    }
115
116
    private function fixType($type): string
117
    {
118
        if (strpos($type, ',') !== false) {
119
            $types = explode(',', $type);
120
121
            return end($types);
122
        }
123
124
        return $type;
125
    }
126
}
127