Completed
Push — master ( 0012a2...099852 )
by Dmitry
08:38
created

DomainTariffForm::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

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 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\forms;
4
5
use hipanel\helpers\ArrayHelper;
6
use hipanel\modules\finance\models\DomainResource;
7
use hipanel\modules\finance\models\Tariff;
8
use Yii;
9
10
class DomainTariffForm extends \yii\base\Model
11
{
12
    public $id;
13
    public $name;
14
    public $parent_id;
15
16
    /** @var Tariff */
17
    public $model;
18
19
    /** @var Tariff */
20
    public $baseModel;
21
22
    protected $zones;
23
24
    protected $_resources;
25
26
    public function attributes()
27
    {
28
        return [
29
            'id',
30
            'parent_id',
31
            'name',
32
        ];
33
    }
34
35
    public function rules()
36
    {
37
        return [
38
            [['name'], 'safe'],
39
            [['parent_id', 'id'], 'integer']
40
        ];
41
    }
42
43
    public function fields()
44
    {
45
        return ArrayHelper::merge(parent::fields(), [
46
            'resources' => '_resources'
47
        ]);
48
    }
49
50
    /**
51
     * @param array $zones
52
     * @param Tariff $baseModel
53
     * @param Tariff $model
54
     * @return $this
55
     */
56
    public function fill($zones, Tariff $baseModel, Tariff $model = null)
57
    {
58
        $this->model = isset($model) ? $model : $baseModel;
59
        $this->baseModel = $baseModel;
60
        $this->zones = array_flip($zones);
61
62
        if (isset($model)) {
63
            $this->id = $this->model->id ?: null;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
64
            $this->name = $this->model->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<hipanel\modules\finance\models\Tariff>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
65
        }
66
67
        $this->parent_id = $this->baseModel->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
68
69
        return $this;
70
    }
71
72
    public function load($data)
73
    {
74
        $this->setAttributes($data[$this->formName()]);
75
        $this->setResources($data[(new DomainResource())->formName()]);
76
77
        return true;
78
    }
79
80
    public function getResources()
81
    {
82
        return $this->_resources;
83
    }
84
85
    public function setResources($resources)
86
    {
87
        $result = [];
88
        foreach ($resources as $resource) {
89
            if ($resource instanceof DomainResource) {
90
                $result[] = $resource;
91
                continue;
92
            }
93
94
            $model = new DomainResource(['scenario' => 'create']);
95
96
            if ($model->load($resource, '') && $model->validate()) {
97
                $result[] = $model;
98
            } else {
99
                throw new \yii\web\UnprocessableEntityHttpException('Failed to load resource model');
100
            }
101
        }
102
103
        $this->_resources = $result;
104
105
        return $this;
106
    }
107
108 View Code Duplication
    public function getZoneResources($zone)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $id = $this->zones[$zone];
111
112
        $result = [];
113
114
        foreach ($this->model->resources as $resource) {
115
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
116
                $result[$resource->type] = $resource;
117
            }
118
        }
119
120
        // sorts $result by order of $resource->getAvailableTypes()
121
        $result = array_merge($resource->getAvailableTypes(), $result);
0 ignored issues
show
Bug introduced by
The variable $resource seems to be defined by a foreach iteration on line 114. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
122
123
        return $result;
124
    }
125
126 View Code Duplication
    public function getZoneBaseResources($zone)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        $id = $this->zones[$zone];
129
130
        $result = [];
131
132
        foreach ($this->baseModel->resources as $resource) {
133
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
134
                $result[$resource->type] = $resource;
135
            }
136
        }
137
138
        // sorts $result by order of $resource->getAvailableTypes()
139
        $result = array_merge($resource->getAvailableTypes(), $result);
0 ignored issues
show
Bug introduced by
The variable $resource seems to be defined by a foreach iteration on line 132. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
140
141
        return $result;
142
    }
143
144
    public function getZones()
145
    {
146
        return $this->zones;
147
    }
148
149
    public function getResourceTypes()
150
    {
151
        return reset($this->baseModel->resources)->getAvailableTypes();
152
    }
153
154
    public function attributeLabels()
155
    {
156
        return [
157
            'name' => Yii::t('hipanel/finance/tariff', 'Название'),
158
        ];
159
    }
160
}
161