SellForm   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 18
c 2
b 0
f 0
dl 0
loc 82
ccs 0
cts 36
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setModels() 0 3 1
A getModels() 0 7 2
A getValidationUrl() 0 3 1
A getDefaultDateTime() 0 3 1
A isServer() 0 3 2
A getActionUrl() 0 3 1
A run() 0 8 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 DateTime;
14
use hipanel\modules\server\forms\HubSellForm;
15
use hipanel\modules\server\helpers\ServerSort;
16
use hipanel\modules\server\models\Server;
17
use yii\bootstrap\Widget;
18
19
class SellForm extends Widget
20
{
21
    /**
22
     * @var string
23
     */
24
    public $actionUrl;
25
26
    /**
27
     * @var string
28
     */
29
    public $validationUrl;
30
31
    /**
32
     * @var Server[]|HubSellForm[]
33
     */
34
    private $models = [];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function run()
40
    {
41
        return $this->render('SellForm', [
42
            'defaultDateTime' => $this->getDefaultDateTime(),
43
            'model' => \reset($this->getModels()),
44
            'models' => $this->getModels(),
45
            'actionUrl' => $this->getActionUrl(),
46
            'validationUrl' => $this->getValidationUrl(),
47
        ]);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getModels(): array
54
    {
55
        if ($this->isServer()) {
56
            return ServerSort::byServerName()->values($this->models);
57
        }
58
59
        return $this->models;
60
    }
61
62
    /**
63
     * @param array $models
64
     */
65
    public function setModels(array $models): void
66
    {
67
        $this->models = $models;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isServer(): bool
74
    {
75
        return \count($this->models) > 0 && \reset($this->models) instanceof Server;
76
    }
77
78
    /**
79
     * @throws \Exception
80
     * @return DateTime
81
     */
82
    private function getDefaultDateTime(): DateTime
83
    {
84
        return new DateTime('first day of this month 00:00');
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    private function getActionUrl(): string
91
    {
92
        return $this->actionUrl;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    private function getValidationUrl(): string
99
    {
100
        return $this->validationUrl;
101
    }
102
}
103