Completed
Push — master ( a7a898...344131 )
by Andrii
11:40
created

Hdomain::scenarioActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 2
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-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\models;
12
13
use hipanel\modules\client\validators\LoginValidator as ClientLoginValidator;
14
use hipanel\modules\hosting\validators\LoginValidator as AccountLoginValidator;
15
use hipanel\validators\DomainValidator;
16
use Yii;
17
use yii\web\JsExpression;
18
19
class Hdomain extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    const STATE_OK = 'ok';
24
    const STATE_BLOCKED = 'blocked';
25
    const STATE_DELETED = 'deleted';
26
    const STATE_DISABLED = 'disabled';
27
    const STATE_TEMPORARY = 'temporary';
28
29
    public function init()
30
    {
31
        $this->on(self::EVENT_AFTER_FIND, function ($event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
            $this->setAttributes([
33
                'ip' => $this->getAttribute('vhost')['ip'],
34
                'backend_ip' => $this->getAttribute('vhost')['backend']['ip'],
35
                'proxy_enabled' => $this->getIsProxied(),
36
            ]);
37
        });
38
    }
39
40
    /**
41
     * @var array Stores array of additional info for vhost of hdomain
42
     */
43
    public $vhost;
44
45
    /**
46
     * @var array Stores array of aliases of hdomain
47
     */
48
    public $aliases;
49
50
    /** {@inheritdoc} */
51
    public function rules()
52
    {
53
        return [
54
            [
55
                [
56
                    'id',
57
                    'server_id',
58
                    'client_id',
59
                    'seller_id',
60
                    'account_id',
61
                    'hdomain_id',
62
                    'state_id',
63
                    'type_id',
64
                    'vhost_id',
65
                    'device_id',
66
                    'dns_hdomain_id',
67
                ],
68
                'integer',
69
            ],
70
            [
71
                [
72
                    'server',
73
                    'device',
74
                    'state',
75
                    'type',
76
                    'backuping_type',
77
                    'state_label',
78
                    'alias_type',
79
                    'proxy_enabled',
80
                    'dns_hdomain_domain',
81
                ],
82
                'safe',
83
            ],
84
            [['client', 'seller'], ClientLoginValidator::class],
85
            [['account'], AccountLoginValidator::class],
86
            [['with_www'], 'boolean', 'on' => ['create-alias']],
87
            [['dns_on', 'with_www', 'proxy_enable'], 'boolean', 'on' => ['create']],
88
            [['domain', 'alias'], DomainValidator::class],
89
            [['ip', 'backend_ip'], 'ip'],
90
            [['ip'], 'required', 'on' => ['create']],
91
            [['domain', 'id'], 'safe', 'on' => ['enable-paid-feature-autorenewal', 'disable-paid-feature-autorenewal']],
92
            [
93
                [
94
                    'server',
95
                    'account',
96
                    'domain',
97
                    'path',
98
                    'ip',
99
                ],
100
                'required',
101
                'on' => ['create'],
102
            ],
103
            [
104
                ['subdomain'],
105
                'match',
106
                'pattern' => '/^(\*|[a-z0-9][a-z0-9-]*)$/i',
107
                'message' => Yii::t('hipanel', '{attribute} does not look like a domain part'),
108
                'on' => ['create-alias'],
109
            ],
110
            [
111
                [
112
                    'server',
113
                    'account',
114
                    'vhost_id',
115
                ],
116
                'required',
117
                'on' => ['create-alias'],
118
            ],
119
            [
120
                [
121
                    'domain',
122
                ],
123
                'required',
124
                'when' => function ($model) {
125
                    return $model->alias_type === 'new';
126
                },
127
                'whenClient' => new JsExpression('function (attribute, value) {
128
                    return false;
129
                }'),
130
                'on' => ['create-alias'],
131
            ],
132
            [
133
                ['id'],
134
                'required',
135
                'on' => ['manage-proxy', 'enable-backuping', 'disable-backuping', 'delete'],
136
            ],
137
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
138
            [['comment'], 'safe', 'on' => ['disable-block']],
139
            [['id', 'dns_on'], 'safe', 'on' => ['set-dns-on']],
140
            [['backuping_exists'], 'boolean'],
141
            [['backuping_type'], 'required', 'on' => ['enable-backuping', 'disable-backuping']],
142
        ];
143
    }
144
145
    public function getIsProxied()
146
    {
147
        return isset($this->getAttribute('vhost')['backend']);
148
    }
149
150
    /** {@inheritdoc} */
151
    public function attributeLabels()
152
    {
153
        return $this->mergeAttributeLabels([
154
            'backend_ip'        => Yii::t('hipanel:hosting', 'Backend IP'),
155
            'with_www'          => Yii::t('hipanel:hosting', 'Create www alias'),
156
            'proxy_enable'      => Yii::t('hipanel:hosting', 'Enable proxy'),
157
            'backuping_type'    => Yii::t('hipanel:hosting', 'Backup periodicity'),
158
            'vhost_id'          => Yii::t('hipanel:hosting', 'Alias for'),
159
            'proxy_enabled'     => Yii::t('hipanel:hosting', 'Proxy enabled'),
160
            'path'              => Yii::t('hipanel:hosting', 'Path'),
161
            'dns_on'            => Yii::t('hipanel', 'DNS'),
162
            'comment'           => Yii::t('hipanel', 'Comment'),
163
        ]);
164
    }
165
166
    public function getIsBlocked()
167
    {
168
        return $this->state === static::STATE_BLOCKED;
0 ignored issues
show
Documentation introduced by
The property state does not exist on object<hipanel\modules\hosting\models\Hdomain>. 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...
169
    }
170
171
    public function getDnsId()
172
    {
173
        return $this->dns_hdomain_id ?: $this->id;
0 ignored issues
show
Documentation introduced by
The property dns_hdomain_id does not exist on object<hipanel\modules\hosting\models\Hdomain>. 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...
Documentation introduced by
The property id does not exist on object<hipanel\modules\hosting\models\Hdomain>. 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...
174
    }
175
176
    public function isAlias()
177
    {
178
        return isset($this->vhost_id);
179
    }
180
181
    /** {@inheritdoc} */
182
    public function scenarioActions()
183
    {
184
        return [
185
            'create'            => [Vhost::tableName(), 'create'], // Create must be sent to Vhost module
0 ignored issues
show
Bug introduced by
The method tableName() does not seem to exist on object<hipanel\modules\hosting\models\Vhost>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
            'create-alias'      => 'create',
187
            'set-dns-on'        => 'update',
188
            'enable-backuping'  => 'update-backuping',
189
            'disable-backuping' => 'update-backuping',
190
        ];
191
    }
192
}
193