ResourceConsumptionTable   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
c 2
b 0
f 0
dl 0
loc 56
ccs 0
cts 29
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ getFormatted() 0 31 2
getFormatted() 0 31 ?
A __construct() 0 4 1
A hp$0 ➔ __construct() 0 5 1
A run() 0 4 1
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\widgets;
12
13
use hipanel\modules\finance\logic\bill\QuantityFormatterFactory;
14
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface;
15
use hipanel\modules\server\models\Consumption;
16
use hipanel\modules\server\models\Server;
17
use yii\base\Widget;
18
19
class ResourceConsumptionTable extends Widget
20
{
21
    /**
22
     * @var Server
23
     */
24
    public $model;
25
26
    /**
27
     * @var QuantityFormatterFactoryInterface|QuantityFormatterFactory
28
     */
29
    private $quantityFormatterFactory;
30
31
    public function __construct(QuantityFormatterFactoryInterface $quantityFormatterFactory, array $config = [])
32
    {
33
        parent::__construct($config);
34
        $this->quantityFormatterFactory = $quantityFormatterFactory;
35
    }
36
37
    public function run(): string
38
    {
39
        return $this->render('ResourceConsumptionTable', [
40
            'model' => $this->model,
41
        ]);
42
    }
43
44
    public function getFormatted(Consumption $model, ?string $currentQuantity): string
45
    {
46
        if (strpos($model->type, 'monthly,') === 0) {
47
            return '';
48
        }
49
50
        if ($currentQuantity === null) {
51
            return '';
52
        }
53
54
        $consumption = new class($currentQuantity, $model->getAttributes()) extends Consumption {
55
            /**
56
             * @var string
57
             */
58
            public $quantity;
59
60
            public function __construct(string $quantity, array $config = [])
61
            {
62
                parent::__construct($config);
63
64
                $this->quantity = $quantity;
65
            }
66
        };
67
68
        $formatter = $this->quantityFormatterFactory->forConsumption($consumption);
69
70
        if ($formatter === null) {
71
            return '';
72
        }
73
74
        return $formatter->format();
75
    }
76
}
77