Completed
Push — master ( a50995...90eb30 )
by Dmitry
14:39
created

Hdomain::isAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Hosting Plugin for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-hosting
7
 * @package   hipanel-module-hosting
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\hosting\models;
13
14
use hipanel\modules\client\validators\LoginValidator as ClientLoginValidator;
15
use hipanel\modules\hosting\validators\LoginValidator as AccountLoginValidator;
16
use hipanel\validators\DomainValidator;
17
use Yii;
18
use yii\web\JsExpression;
19
20
class Hdomain extends \hipanel\base\Model
21
{
22
    use \hipanel\base\ModelTrait;
23
24
    const STATE_OK = 'ok';
25
    const STATE_BLOCKED = 'blocked';
26
    const STATE_DELETED = 'deleted';
27
    const STATE_DISABLED = 'disabled';
28
    const STATE_TEMPORARY = 'temporary';
29
30
    public function init()
31
    {
32
        $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...
33
            $this->setAttributes([
34
                'ip' => $this->getAttribute('vhost')['ip'],
35
                'backend_ip' => $this->getAttribute('vhost')['backend']['ip'],
36
                'proxy_enabled' => $this->getIsProxied(),
37
            ]);
38
        });
39
    }
40
41
    /**
42
     * @var array Stores array of additional info for vhost of hdomain
43
     */
44
    public $vhost;
45
46
    /**
47
     * @var array Stores array of aliases of hdomain
48
     */
49
    public $aliases;
50
51
    /** @inheritdoc */
52
    public function rules()
53
    {
54
        return [
55
            [
56
                [
57
                    'id',
58
                    'server_id',
59
                    'client_id',
60
                    'seller_id',
61
                    'account_id',
62
                    'hdomain_id',
63
                    'state_id',
64
                    'type_id',
65
                    'vhost_id',
66
                    'device_id',
67
                    'dns_hdomain_id',
68
                ],
69
                'integer'
70
            ],
71
            [
72
                [
73
                    'server',
74
                    'device',
75
                    'state',
76
                    'type',
77
                    'backuping_type',
78
                    'state_label',
79
                    'alias_type',
80
                    'proxy_enabled',
81
                    'dns_hdomain_domain',
82
                ],
83
                'safe'
84
            ],
85
            [['client', 'seller'], ClientLoginValidator::class],
86
            [['account'], AccountLoginValidator::class],
87
            [['with_www'], 'boolean', 'on' => ['create-alias']],
88
            [['dns_on', 'with_www', 'proxy_enable'], 'boolean', 'on' => ['create']],
89
            [['domain', 'alias'], DomainValidator::class],
90
            [['ip', 'backend_ip'], 'ip'],
91
            [['ip'], 'required', 'on' => ['create']],
92
            [['domain', 'id'], 'safe', 'on' => ['enable-paid-feature-autorenewal', 'disable-paid-feature-autorenewal']],
93
            [
94
                [
95
                    'server',
96
                    'account',
97
                    'domain',
98
                    'path',
99
                    'ip',
100
                ],
101
                'required',
102
                'on' => ['create']
103
            ],
104
            [
105
                ['subdomain'],
106
                'match',
107
                'pattern' => '/^(\*|[a-z0-9][a-z0-9-]*)$/i',
108
                'message' => \Yii::t('app', '{attribute} does not look like a domain part'),
109
                'on' => ['create-alias']
110
            ],
111
            [
112
                [
113
                    'server',
114
                    'account',
115
                    'vhost_id',
116
                ],
117
                'required',
118
                'on' => ['create-alias']
119
            ],
120
            [
121
                [
122
                    'domain',
123
                ],
124
                'required',
125
                'when' => function ($model) {
126
                    return $model->alias_type === 'new';
127
                },
128
                'whenClient' => new JsExpression("function (attribute, value) {
129
                    return false;
130
                }"),
131
                'on' => ['create-alias']
132
            ],
133
            [
134
                ['id'],
135
                'required',
136
                'on' => ['manage-proxy', 'delete']
137
            ],
138
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
139
            [['comment'], 'safe', 'on' => ['disable-block']],
140
            [['id', 'dns_on'], 'safe', 'on' => ['set-dns-on']],
141
        ];
142
    }
143
144
    public function getIsProxied()
145
    {
146
        return isset($this->getAttribute('vhost')['backend']);
147
    }
148
149
    /** @inheritdoc */
150
    public function attributeLabels()
151
    {
152
        return $this->mergeAttributeLabels([
153
            'domain' => Yii::t('app', 'Domain Name'),
154
            'backend_ip' => Yii::t('app', 'Backend IP'),
155
            'with_www' => Yii::t('app', 'Create www alias'),
156
            'proxy_enable' => Yii::t('app', 'Enable proxy (NEED MANUAL)'),
157
            'backuping_type' => Yii::t('app', 'Backup periodicity'),
158
            'dns_on' => Yii::t('hipanel/hosting', 'DNS'),
159
            'vhost_id' => Yii::t('app', 'Alias for'),
160
            'comment' => Yii::t('app', 'Comment'),
161
            'proxy_enabled' => Yii::t('hipanel/hosting', 'Proxy enabled'),
162
            'path' => Yii::t('hipanel/hosting', 'Path'),
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
    public function scenarioCommands()
182
    {
183
        $result = [];
184
185
        if (in_array($this->scenario, ['create', 'update'], true)) {
186
            $result['create'] = ['vhosts', ucfirst($this->scenario)]; // Create must be sent to vhost module
187
        }
188
        $result['create-alias'] = 'create';
189
        $result['set-dns-on'] = 'update';
190
        return $result;
191
    }
192
}
193