Passed
Push — master ( ccd18c...cdfc25 )
by Andrii
07:58
created

ResourceConsumptionTable::run()   A

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 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\server\widgets;
4
5
use hipanel\modules\finance\logic\bill\QuantityFormatterFactory;
6
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface;
7
use hipanel\modules\server\models\Consumption;
8
use hipanel\modules\server\models\Server;
9
use Yii;
10
use yii\base\Widget;
11
12
class ResourceConsumptionTable extends Widget
13
{
14
    /**
15
     * @var Server
16
     */
17
    public $model;
18
19
    /**
20
     * @var QuantityFormatterFactoryInterface|QuantityFormatterFactory
21
     */
22
    private $quantityFormatterFactory;
23
24
    public function __construct(QuantityFormatterFactoryInterface $quantityFormatterFactory, array $config = [])
25
    {
26
        parent::__construct($config);
27
        $this->quantityFormatterFactory = $quantityFormatterFactory;
28
    }
29
30
    public function run(): string
31
    {
32
        return $this->render('ResourceConsumptionTable', [
33
            'model' => $this->model,
34
        ]);
35
    }
36
37
    public function getFormatted(Consumption $model, ?string $currentQuantity): string
38
    {
39
        if ($currentQuantity === null) {
40
            return '';
41
        }
42
43
        $consumption = new class($currentQuantity, $model->getAttributes()) extends Consumption
44
        {
45
            /**
46
             * @var string
47
             */
48
            public $quantity;
49
50
            public function __construct(string $quantity, array $config = [])
51
            {
52
                parent::__construct($config);
53
54
                $this->quantity = $quantity;
55
            }
56
        };
57
58
        $formatter = $this->quantityFormatterFactory->forConsumption($consumption);
59
60
        if ($formatter === null) {
61
            return '';
62
        }
63
64
        return $formatter->format();
65
    }
66
}
67
68