Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

QuantityFormatterFactory::forBill()   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 2
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 hiqdev\php\units\Quantity;
9
use hiqdev\php\units\yii2\formatters\IntlFormatter;
10
11
/**
12
 * Class QuantityFormatterFactory
13
 */
14
final class QuantityFormatterFactory implements QuantityFormatterFactoryInterface
15
{
16
    /**
17
     * @var array maps bill type to a QuantityFormatter
18
     * // TODO: use DI to configure
19
     */
20
    protected $types = [
21
        'support_time' => SupportTimeQuantity::class,
22
        'server_traf_max' => DefaultQuantityFormatter::class,
23
        'server_traf95_max' => DefaultQuantityFormatter::class,
24
        'backup_du' => DefaultQuantityFormatter::class,
25
        'server_du' => DefaultQuantityFormatter::class,
26
        'hw_purchase' => DefaultQuantityFormatter::class,
27
        'ip_num' => IPNumQuantity::class,
28
        'monthly' => MonthlyQuantity::class,
29
        'drenewal' => DomainRenewalQuantity::class,
30
    ];
31
    /**
32
     * @var IntlFormatter
33
     */
34
    private $intlFormatter;
35
36
    public function __construct(IntlFormatter $intlFormatter)
37
    {
38
        $this->intlFormatter = $intlFormatter;
39
    }
40
41
    /** {@inheritdoc} */
42
    public function create($model): ?QuantityFormatterInterface
43
    {
44
        if ($model instanceof Bill || $model instanceof BillForm) {
45
            return $this->forBill($model);
46
        }
47
48
        if ($model instanceof Charge) {
49
            return $this->forCharge($model);
50
        }
51
52
        throw new \InvalidArgumentException();
53
    }
54
55
    /**
56
     * @param Bill|BillForm $bill
57
     * @return QuantityFormatterInterface|null
58
     */
59
    public function forBill($bill): ?QuantityFormatterInterface
60
    {
61
        return $this->createByType($bill->type, Quantity::create($bill->unit, $bill->quantity), $bill);
62
    }
63
64
    public function forCharge(Charge $charge): ?QuantityFormatterInterface
65
    {
66
        return $this->createByType($charge->type, Quantity::create($charge->unit, $charge->quantity), $charge);
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Charge>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
    }
68
69
    /** {@inheritdoc} */
70
    public function createByType(string $type, Quantity $quantity, $context = null): ?QuantityFormatterInterface
71
    {
72
        $type = $this->fixType($type);
73
        if ($className = $this->types[$type] ?? null) {
74
            /** @var QuantityFormatterInterface $formatter */
75
            $formatter = new $className($quantity, $this->intlFormatter);
76
77
            if ($formatter instanceof ContextAwareQuantityFormatter) {
78
                $formatter->setContext($context);
79
            }
80
81
            return $formatter;
82
        }
83
84
        return null;
85
    }
86
87
    private function fixType($type): string
88
    {
89
        if (strpos($type, ',') !== false) {
90
            $types = explode(',', $type);
91
            return end($types);
92
        }
93
94
        return $type;
95
    }
96
}
97