Completed
Pull Request — master (#21)
by Klochok
14:15
created

Ip::getAvailableTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\models;
12
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\models\Ref;
15
use hipanel\validators\DomainValidator;
16
use Yii;
17
use yii\helpers\StringHelper;
18
use yii\validators\IpValidator;
19
20
class Ip extends \hipanel\base\Model
21
{
22
    use \hipanel\base\ModelTrait {
23
        attributes as defaultAttributes;
24
    }
25
26
    /**
27
     * @var string
28
     */
29
    public static $i18nDictionary = 'hipanel:hosting';
30
31
    /**
32
     * @var Link[] Array of links to be inserted/updated.
33
     * Do not mix up with [[getLinks]]
34
     * @see getAddedLinks
35
     */
36
    private $_links = [];
37
38
    public function attributes()
39
    {
40
        $attributes = $this->defaultAttributes();
41
        unset($attributes[array_search('links', $attributes, true)]);
42
43
        return $attributes;
44
    }
45
46
    public function setOldAttribute($name, $value)
47
    {
48
        if ($name !== 'links') {
49
            parent::setOldAttribute($name, $value);
50
        }
51
    }
52
53
    public function rules()
54
    {
55
        return [
56
            [['client_id', 'seller_id'],                          'integer'],
57
            [['objects_count', 'client', 'seller'],               'safe'],
58
            [['prefix', 'family', 'tags'],                        'safe'],
59
            [['type', 'state', 'state_label', 'ptr'],             'safe'],
60
            [['expanded_ips', 'ip_normalized'],                   'safe'],
61
            [['is_single'],                                       'boolean'],
62
            [['ip'], 'ip', 'subnet' => null, 'on' => ['create']],
63
            [['ip'], 'safe', 'on' => ['update']],
64
            [['links'], 'safe', 'on' => ['create', 'update']],
65
            [['tags'], 'filter',
66
                'filter' => function ($value) {
67
                    return StringHelper::explode($value);
68
                },
69
                'skipOnArray' => true, 'on' => ['create', 'update'],
70
            ],
71
            [['id'], 'required', 'on' => ['update', 'delete', 'set-ptr']],
72
            [['id'], 'integer', 'on' => ['create', 'update', 'delete', 'expand']],
73
            [['with_existing'], 'boolean', 'on' => ['expand']],
74
            [['ptr'], DomainValidator::class, 'on' => ['set-ptr']],
75
76
            [['ip', 'type'], 'required', 'on' => ['create', 'update']],
77
            [['type'], 'string', 'on' => ['create', 'update']],
78
79
            // Set note
80
            [['note'], 'string', 'on' => ['set-note', 'create', 'update']],
81
            [['note'], 'filter', 'filter'=>'\yii\helpers\HtmlPurifier::process', 'on' => ['set-note', 'create', 'update']],
82
            [['ip'], 'safe', 'on' => 'set-note'],
83
            [['id'], 'integer', 'on' => 'set-note'],
84
        ];
85
    }
86
87
    /** {@inheritdoc} */
88
    public function attributeLabels()
89
    {
90
        return $this->mergeAttributeLabels([
91
            'links'                 => Yii::t(static::$i18nDictionary, 'Links'),
92
            'objects_count'         => Yii::t(static::$i18nDictionary, 'Count of objects'),
93
            'is_single'             => Yii::t(static::$i18nDictionary, 'Single'),
94
            'ip_normalized'         => Yii::t(static::$i18nDictionary, 'Normalized IP'),
95
            'expanded_ips'          => Yii::t(static::$i18nDictionary, 'Expanded IPs'),
96
            'ptr'                   => Yii::t(static::$i18nDictionary, 'PTR'),
97
            'note'                  => Yii::t('hipanel', 'Note'),
98
        ]);
99
    }
100
101
    /**
102
     * @return array|\hiqdev\hiart\ActiveQuery
103
     */
104
    public function getLinks()
105
    {
106
        return in_array($this->scenario, ['create', 'update'], true)
107
            ? ArrayHelper::toArray($this->_links)
108
            : $this->hasMany(Link::class, ['ip_id' => 'id']);
109
    }
110
111
    /**
112
     * @return \hiqdev\hiart\ActiveQuery
113
     */
114
    public function getRelatedLinks()
115
    {
116
        return $this->hasMany(Link::class, ['ip_id' => 'id']);
117
    }
118
119
    /**
120
     * @return Link[]
121
     */
122
    public function getAddedLinks()
123
    {
124
        return $this->_links;
125
    }
126
127
    /**
128
     * @param array $links
129
     */
130
    public function setAddedLinks(array $links)
131
    {
132
        foreach ($links as $link) {
133
            $this->addLink($link);
134
        }
135
    }
136
137
    /**
138
     * @param Link $link
139
     */
140
    public function addLink(Link $link)
141
    {
142
        $this->_links[] = $link;
143
    }
144
145
    /**
146
     * Checks, whether it is possible to set a PTR record on the IP address.
147
     *
148
     * @return bool
149
     */
150
    public function canSetPtr()
151
    {
152
        return !in_array('aux', (array) $this->tags, true)
153
            && (new IpValidator(['ranges' => ['!system', 'any']]))->validate($this->ip);
0 ignored issues
show
Documentation introduced by
The property ip does not exist on object<hipanel\modules\hosting\models\Ip>. 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...
154
    }
155
156
    public function getAvailableTypes(): array
157
    {
158
        return Ref::getList('type,ip', 'hipanel.hosting.ip.types');
159
    }
160
}
161