Ip::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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