Completed
Push — master ( 78c2e1...01635f )
by Andrii
19:30 queued 15:45
created

src/widgets/StateFormatter.php (5 issues)

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\server\models\Server;
14
use Yii;
15
use yii\base\InvalidParamException;
16
use yii\base\Widget;
17
use yii\helpers\Html;
18
19
class StateFormatter extends Widget
20
{
21
    /**
22
     * @var Server
23
     */
24
    public $model;
25
26
    public function init()
27
    {
28
        parent::init();
29
        if (!($this->model instanceof Server)) {
0 ignored issues
show
$this->model is always a sub-type of hipanel\modules\server\models\Server.
Loading history...
30
            throw new InvalidParamException('Model should be an instance of Server model');
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated: since 2.0.14. Use [[InvalidArgumentException]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
            throw /** @scrutinizer ignore-deprecated */ new InvalidParamException('Model should be an instance of Server model');
Loading history...
31
        }
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function run()
38
    {
39
        if ($this->model->state !== 'blocked') {
0 ignored issues
show
Bug Best Practice introduced by
The property state does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
            $value = Yii::$app->formatter->asDate($this->model->expires);
0 ignored issues
show
Bug Best Practice introduced by
The property expires does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
        } else {
42
            $value = Yii::t('hipanel:server', 'Blocked {reason}', ['reason' => Yii::t('hipanel:block-reasons', $this->model->block_reason_label)]);
0 ignored issues
show
Bug Best Practice introduced by
The property block_reason_label does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
43
        }
44
45
        $class = ['label'];
46
47
        if (strtotime('+7 days', time()) < strtotime($this->model->expires)) {
48
            $class[] = 'label-info';
49
        } elseif (strtotime('+3 days', time()) < strtotime($this->model->expires)) {
50
            $class[] = 'label-warning';
51
        } else {
52
            $class[] = 'label-danger';
53
        }
54
        $html = Html::tag('span', $value, ['class' => implode(' ', $class)]);
55
56
        return $html;
57
    }
58
}
59