Mail   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 88
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 36 2
A attributeLabels() 0 10 1
A canChangePassword() 0 4 2
A attributeHints() 0 12 3
A getTypes() 0 8 1
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\hosting\validators\EmailLocalPartValidator;
14
use Yii;
15
use yii\helpers\StringHelper;
16
17
class Mail extends \hipanel\base\Model
18
{
19
    use \hipanel\base\ModelTrait;
20
21
    const TYPE_FORWARD_ONLY = 'forward_only';
22
    const TYPE_BOX_WITH_FORWARDS = 'mailbox_with_forwards';
23
    const TYPE_MAILBOX = 'mailbox';
24
25
    const SPAM_ACTION_NONE = '';
26
    const SPAM_ACTION_DELETE = 'delete';
27
28
    /** {@inheritdoc} */
29
    public function rules()
30
    {
31
        return [
32
            [['id', 'client_id', 'seller_id', 'account_id', 'server_id'], 'integer'],
33
            [['mail', 'nick', 'hdomain', 'client', 'seller', 'account', 'server', 'domain'], 'safe'],
34
            [['type', 'state', 'state_label', 'password', 'spam_forward_mail'], 'safe'],
35
            [['is_alias'], 'boolean'],
36
            [['hdomain_id'], 'integer', 'on' => ['create']],
37
            [['server', 'account'], 'safe', 'on' => ['create']],
38
            [['password'], 'safe', 'on' => ['create']],
39
            [['password'], 'safe', 'on' => ['update', 'set-password'], 'when' => function ($model) {
40
                return !$model->canChangePassword();
41
            }],
42
            [['password'], function ($attribute) {
43
                if (!mb_check_encoding($this->{$attribute}, 'ASCII')) {
44
                    $this->addError(Yii::t('hipanel:hosting', 'password can contain only latin characters and digits'));
45
                }
46
            }, 'on' => ['create', 'update', 'set-password']],
47
            [['password'], 'match', 'pattern' => '/^[a-zA-Z0-9]+$/',
48
                'message' => Yii::t('hipanel:hosting', 'Password can contain only latin characters and digits.'),
49
                'on' => ['create', 'update', 'set-password'], ],
50
            [['nick'], EmailLocalPartValidator::class, 'on' => ['create']],
51
            [['forwards', 'spam_forward_mail'], 'filter', 'filter' => function ($value) {
52
                $res = StringHelper::explode($value, ',', true, true);
53
54
                return $res;
55
            }, 'skipOnArray' => true, 'on' => ['create', 'update']],
56
            [['forwards', 'spam_forward_mail'], 'each', 'rule' => ['email'], 'on' => ['create', 'update']],
57
            [['spam_action', 'autoanswer'], 'safe', 'on' => ['create', 'update']],
58
            [['du_limit'], 'integer', 'on' => ['create', 'update']],
59
            [['id'], 'required', 'on' => ['update', 'delete']],
60
            [['hdomain_id', 'server', 'account', 'nick'], 'required', 'on' => ['create']],
61
62
            [['id'], 'integer', 'on' => ['enable', 'disable']],
63
        ];
64
    }
65
66
    /** {@inheritdoc} */
67
    public function attributeLabels()
68
    {
69
        return $this->mergeAttributeLabels([
70
            'forwards'      => Yii::t('hipanel:hosting', 'Forwarding'),
71
            'du_limit'      => Yii::t('hipanel:hosting', 'Disk usage limit'),
72
            'autoanswer'    => Yii::t('hipanel:hosting', 'Auto answer'),
73
            'spam_action' => Yii::t('hipanel:hosting', 'Spam action'),
74
            'mail'          => Yii::t('hipanel', 'E-mail'),
75
        ]);
76
    }
77
78
    public function canChangePassword()
79
    {
80
        return $this->type !== static::TYPE_FORWARD_ONLY && $this->state !== 'deleted';
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\hosting\models\Mail>. 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 state does not exist on object<hipanel\modules\hosting\models\Mail>. 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...
81
    }
82
83
    public function attributeHints()
84
    {
85
        return [
86
            'forwards' => Yii::t('hipanel:hosting', 'All messages will be forwarded on the specified addresses. You can select email from the list of existing or wright down your own.'),
87
            'password' => $this->type === static::TYPE_FORWARD_ONLY
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\hosting\models\Mail>. 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...
88
                            ? Yii::t('hipanel:hosting', 'Password change is prohibited on forward-only mailboxes')
89
                            : ($this->isNewRecord
90
                                ? Yii::t('hipanel:hosting', 'Leave this field empty to create a forward-only mailbox')
91
                                : Yii::t('hipanel:hosting', 'Fill this field only if you want to change the password')
92
                            ),
93
        ];
94
    }
95
96
    public static function getTypes()
97
    {
98
        return [
99
            'mailbox' => Yii::t('hipanel:hosting', 'Mailbox'),
100
            'forward_only' => Yii::t('hipanel:hosting', 'Forward only'),
101
            'mailbox_with_forwards' => Yii::t('hipanel:hosting', 'Mailbox with forwards'),
102
        ];
103
    }
104
}
105