Completed
Push — master ( 248421...1a6a9f )
by Klochok
16s queued 11s
created

Hdomain::getAliasTypeOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
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-2019, 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
    const ALIAS_TYPE_NEW = 'new';
30
    const ALIAS_TYPE_SUBDOMAIN = 'subdomain';
31
32
    public function init()
33
    {
34
        $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...
35
            $this->setAttributes([
36
                'ip' => $this->getAttribute('vhost')['ip'],
37
                'backend_ip' => $this->getAttribute('vhost')['backend']['ip'],
38
                'proxy_enabled' => $this->getIsProxied(),
39
            ]);
40
        });
41
    }
42
43
    /**
44
     * @var array Stores array of additional info for vhost of hdomain
45
     */
46
    public $vhost;
47
48
    /**
49
     * @var array Stores array of aliases of hdomain
50
     */
51
    public $aliases;
52
53
    /** {@inheritdoc} */
54
    public function rules()
55
    {
56
        return [
57
            [
58
                [
59
                    'id',
60
                    'server_id',
61
                    'client_id',
62
                    'seller_id',
63
                    'account_id',
64
                    'hdomain_id',
65
                    'state_id',
66
                    'type_id',
67
                    'vhost_id',
68
                    'device_id',
69
                    'dns_hdomain_id',
70
                ],
71
                'integer',
72
            ],
73
            [
74
                [
75
                    'server',
76
                    'device',
77
                    'state',
78
                    'type',
79
                    'backuping_type',
80
                    'state_label',
81
                    'alias_type',
82
                    'proxy_enabled',
83
                    'dns_hdomain_domain',
84
                ],
85
                'safe',
86
            ],
87
            [['client', 'seller'], ClientLoginValidator::class],
88
            [['account'], AccountLoginValidator::class],
89
            [['with_www'], 'boolean', 'on' => ['create-alias']],
90
            [['dns_on', 'with_www', 'proxy_enable'], 'boolean', 'on' => ['create']],
91
            [['domain', 'alias'], DomainValidator::class],
92
            [['ip', 'backend_ip'], 'ip'],
93
            [['ip'], 'required', 'on' => ['create']],
94
            [['domain', 'id'], 'safe', 'on' => ['enable-paid-feature-autorenewal', 'disable-paid-feature-autorenewal']],
95
            [['alias_type'], 'in', 'range' => [self::ALIAS_TYPE_NEW, self::ALIAS_TYPE_SUBDOMAIN], 'on' => 'create-alias'],
96
            [['alias_type'], 'default', 'value' => self::ALIAS_TYPE_SUBDOMAIN, 'on' => 'create-alias'],
97
            [
98
                [
99
                    'server',
100
                    'account',
101
                    'domain',
102
                    'path',
103
                    'ip',
104
                ],
105
                'required',
106
                'on' => ['create'],
107
            ],
108
            [
109
                ['subdomain'],
110
                'match',
111
                'pattern' => '/^(\*|[a-z0-9][a-z0-9-]*)$/i',
112
                'message' => Yii::t('hipanel', '{attribute} does not look like a domain part'),
113
                'on' => ['create-alias'],
114
            ],
115
            [
116
                [
117
                    'server',
118
                    'account',
119
                    'vhost_id',
120
                    'alias_type',
121
                ],
122
                'required',
123
                'on' => ['create-alias'],
124
            ],
125
            [
126
                [
127
                    'domain',
128
                ],
129
                'required',
130
                'when' => function ($model) {
131
                    return $model->alias_type === 'new';
132
                },
133
                'whenClient' => new JsExpression('function (attribute, value) {
134
                    return $(":input[name*=alias_type]:checked").val() == "new";
135
                }'),
136
                'on' => ['create-alias'],
137
            ],
138
            [
139
                [
140
                    'dns_hdomain_id',
141
                ],
142
                'required',
143
                'when' => function ($model) {
144
                    return $model->alias_type === 'subdomain';
145
                },
146
                'whenClient' => new JsExpression('function (attribute, value) {
147
                    return $(":input[name*=alias_type]:checked").val() == "subdomain";
148
                }'),
149
                'on' => ['create-alias'],
150
            ],
151
            [
152
                ['id'],
153
                'required',
154
                'on' => ['manage-proxy', 'enable-backuping', 'disable-backuping', 'delete'],
155
            ],
156
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
157
            [['comment'], 'safe', 'on' => ['disable-block']],
158
            [['id', 'dns_on'], 'safe', 'on' => ['set-dns-on']],
159
            [['backuping_exists'], 'boolean'],
160
            [['backuping_type'], 'required', 'on' => ['enable-backuping', 'disable-backuping']],
161
            [['id', 'premium_autorenewal'], 'required', 'on' => ['set-paid-feature-autorenewal']],
162
        ];
163
    }
164
165
    public function getIsProxied()
166
    {
167
        return isset($this->getAttribute('vhost')['backend']);
168
    }
169
170
    /** {@inheritdoc} */
171
    public function attributeLabels()
172
    {
173
        return $this->mergeAttributeLabels([
174
            'backend_ip'         => Yii::t('hipanel:hosting', 'Backend IP'),
175
            'with_www'           => Yii::t('hipanel:hosting', 'Create www alias'),
176
            'proxy_enable'       => Yii::t('hipanel:hosting', 'Enable proxy'),
177
            'backuping_type'     => Yii::t('hipanel:hosting', 'Backup periodicity'),
178
            'vhost_id'           => Yii::t('hipanel:hosting', 'Alias for'),
179
            'proxy_enabled'      => Yii::t('hipanel:hosting', 'Proxy enabled'),
180
            'path'               => Yii::t('hipanel:hosting', 'Path'),
181
            'alias_type'         => Yii::t('hipanel:hosting', 'Alias type'),
182
            'dns_hdomain_id'     => Yii::t('hipanel:hosting', 'Domain'),
183
            'dns_hdomain_domain' => Yii::t('hipanel:hosting', 'Domain'),
184
            'dns_on'             => Yii::t('hipanel', 'DNS'),
185
            'comment'            => Yii::t('hipanel', 'Comment'),
186
        ]);
187
    }
188
189
    public function getIsBlocked()
190
    {
191
        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...
192
    }
193
194
    public function getDnsId()
195
    {
196
        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...
197
    }
198
199
    public function isAlias()
200
    {
201
        return isset($this->vhost_id);
202
    }
203
204
    public function getAliasTypeOptions()
205
    {
206
        return [
207
            self::ALIAS_TYPE_SUBDOMAIN => Yii::t('hipanel:hosting', 'Subdomain of existing domain'),
208
            self::ALIAS_TYPE_NEW => Yii::t('hipanel:hosting', 'New domain'),
209
        ];
210
    }
211
212
    /** {@inheritdoc} */
213
    public function scenarioActions()
214
    {
215
        return [
216
            'create'            => [Vhost::tableName(), 'create'], // Create must be sent to Vhost module
217
            'create-alias'      => 'create',
218
            'set-dns-on'        => 'update',
219
            'enable-backuping'  => 'update-backuping',
220
            'disable-backuping' => 'update-backuping',
221
        ];
222
    }
223
}
224