Completed
Push — master ( 669d4a...cba942 )
by Andrii
05:17 queued 03:45
created

Backuping::isEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\models;
12
13
use hipanel\models\Obj;
14
use hipanel\models\Ref;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
class Backuping extends \hipanel\base\Model
19
{
20
    use \hipanel\base\ModelTrait;
21
22
    const STATE_DELETED = 'deleted';
23
    const STATE_DISABLED = 'disabled';
24
25
    /** {@inheritdoc} */
26
    public function rules()
27
    {
28
        return [
29
            [['id', 'service_id', 'server_id', 'account_id', 'client_id', 'seller_id'], 'integer'],
30
            [['client', 'seller'], 'safe'],
31
            [['day', 'hour', 'path', 'include', 'exclude'], 'safe'],
32
            [['method', 'method_label', 'server', 'account', 'name', 'object', 'service'], 'safe'],
33
            [['backup_last'], 'date'],
34
            [['backup_count', 'total_du', 'total_du_gb'], 'integer'],
35
            [['type', 'type_label', 'state', 'state_label'], 'safe'],
36
37
            [['id', 'type'], 'safe', 'on' => ['update']],
38
            [['id'], 'integer', 'on' => ['delete', 'disable', 'enable', 'undelete']],
39
40
            // Update settings
41
            [['id', 'service_id'], 'integer', 'on' => 'update'],
42
            [['skip_lock'], 'boolean', 'on' => 'update'],
43
            [['day', 'hour'], 'integer', 'on' => 'update'],
44
            [['day', 'hour'], 'default', 'value' => 0, 'on' => 'update'],
45
            [['path', 'include', 'exclude', 'type', 'method'], 'string', 'on' => 'update'],
46
        ];
47
    }
48
49
    /** {@inheritdoc} */
50
    public function attributeLabels()
51
    {
52
        return $this->mergeAttributeLabels([
53
            'day' => Yii::t('hipanel', 'Date'),
54
            'hour' => Yii::t('hipanel', 'Time'),
55
            'backup_last' => Yii::t('hipanel:hosting', 'Last backup'),
56
            'backup_count' => Yii::t('hipanel', 'Count'),
57
            'total_du' => Yii::t('hipanel:hosting', 'Disk usage'),
58
            'total_du_gb' => Yii::t('hipanel:hosting', 'Disk'),
59
            'method_label' => Yii::t('hipanel:hosting', 'Method'),
60
            'method_id' => Yii::t('hipanel:hosting', 'Method'),
61
            'account_id' => Yii::t('hipanel', 'Account'),
62
            'server_id' => Yii::t('hipanel', 'Server'),
63
            'state_label' => Yii::t('hipanel', 'State'),
64
            'state' => Yii::t('hipanel', 'State'),
65
            'type' => Yii::t('hipanel:hosting', 'Periodicity'),
66
            'object' => Yii::t('hipanel:hosting', 'Object'),
67
            'skip_lock' => Yii::t('hipanel:hosting', 'Skip Lock'),
68
        ]);
69
    }
70
71 View Code Duplication
    public function getObj()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        return Yii::createObject([
74
            'class' => Obj::class,
75
            'id' => $this->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\hosting\models\Backuping>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
76
            'name' => $this->name,
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<hipanel\modules\hosting\models\Backuping>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
            'class_name' => $this->object,
0 ignored issues
show
Documentation introduced by
The property object does not exist on object<hipanel\modules\hosting\models\Backuping>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
        ]);
79
    }
80
81
    public function canBeDeleted() : bool
82
    {
83
        return $this->can('account.delete') && !$this->isDeleted();
84
    }
85
86
    public function canBeDisabled() : bool
87
    {
88
        return $this->can('account.update') && $this->isEnabled();
89
    }
90
91
    public function canBeRestored() : bool
92
    {
93
        return $this->can('account.update') && $this->isDeleted();
94
    }
95
96
    public function canBeEnabled() : bool
97
    {
98
        return $this->can('account.update') && !$this->isEnabled() && !$this->isDeleted();
99
    }
100
101
    public function isDeleted() : bool
102
    {
103
        return $this->state === self::STATE_DELETED;
0 ignored issues
show
Documentation introduced by
The property state does not exist on object<hipanel\modules\hosting\models\Backuping>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
    }
105
106
    public function isDisabled() : bool
107
    {
108
        return $this->state === self::STATE_DISABLED;
0 ignored issues
show
Documentation introduced by
The property state does not exist on object<hipanel\modules\hosting\models\Backuping>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
    }
110
111
    public function isEnabled() : bool
112
    {
113
        return !$this->isDeleted() && !$this->isDisabled();
114
    }
115
116
    public static function getTypeOptions(): array
117
    {
118
        return Yii::$app->get('cache')->getOrSet([__METHOD__], function () {
119
            return ArrayHelper::map(Ref::find()->where([
120
                'gtype' => 'type,backuping', 'select' => 'full',
121
            ])->all(), 'name', function ($model) {
122
                return Yii::t('hipanel.hosting.backuping.periodicity', $model->name);
123
            });
124
        }, 86400 * 24); // 24 days
125
    }
126
127
    protected function can(string $permission) : bool
128
    {
129
        return Yii::$app->user->can($permission);
130
    }
131
}
132