Completed
Push — master ( 823023...c43f27 )
by Dmitry
06:01 queued 02:13
created

SellForm::getValidationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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