Completed
Push — master ( e3a066...ae53ec )
by Dmitry
09:38
created

DomainTariffForm::fill()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 8.8571
cc 5
eloc 11
nc 12
nop 3
crap 30
1
<?php
2
3
namespace hipanel\modules\finance\forms;
4
5
use hipanel\modules\finance\models\DomainResource;
6
use hipanel\modules\finance\models\DomainService;
7
use hipanel\modules\finance\models\Tariff;
8
use yii\web\UnprocessableEntityHttpException;
9
10
class DomainTariffForm extends AbstractTariffForm
11
{
12
    /**
13
     * @var array Domain zones
14
     * Key - zone name (com, net, ...)
15
     * Value - zone id
16
     * @see getZones
17
     */
18
    protected $zones;
19
20
    /**
21
     * @param array $zones
22
     * @param Tariff $baseTariff
23
     * @param Tariff $tariff
24
     * @return $this
25
     */
26
    public function fill($zones, Tariff $baseTariff, Tariff $tariff = null)
27
    {
28
        $this->tariff = isset($tariff) ? $tariff : $baseTariff;
29
        $this->baseTariff = $baseTariff;
30
        $this->zones = array_flip($zones);
31
32
        if (isset($tariff)) {
33
            $this->id = $this->tariff->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...
34
            $this->name = $this->tariff->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...
35
        }
36
37
        $this->parent_id = $this->baseTariff->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...
38
39
        foreach ($this->tariff->resources as $resource) {
40
            $resource->scenario = $this->scenario;
41
        }
42
43
        return $this;
44
    }
45
46
    public function load($data)
47
    {
48
        $this->setAttributes($data[$this->formName()]);
49
        $this->setResources($data[(new DomainResource())->formName()]);
50
51
        return true;
52
    }
53
54
    /**
55
     * @return DomainService[]
56
     */
57
    public function getServices()
58
    {
59
        return $this->createServices($this->tariff->resources);
60
    }
61
62
    /**
63
     * @return DomainService[]
64
     */
65
    public function getBaseServices()
66
    {
67
        return $this->createServices($this->baseTariff->resources);
68
    }
69
70
    /**
71
     * @param $resources
72
     * @return DomainService[]
73
     *
74
     */
75
    protected function createServices($resources)
76
    {
77
        $result = [];
78
        $resource = reset($resources);
79
80
        foreach ($resource->getServiceTypes() as $type => $name) {
81
            $service = new DomainService([
82
                'name' => $name,
83
                'type' => $type,
84
            ]);
85
86
            foreach ($resources as $resource) {
87
                if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) {
88
                    $result[$type] = $service;
89
                    break;
90
                }
91
            }
92
        }
93
94
        return $result;
95
    }
96
97
    public function setResources($resources)
98
    {
99
        $result = [];
100
        foreach ($resources as $resource) {
101
            if ($resource instanceof DomainResource) {
102
                $result[] = $resource;
103
                continue;
104
            }
105
106
            $model = new DomainResource(['scenario' => $this->scenario]);
107
108
            if ($model->load($resource, '') && $model->validate()) {
109
                $result[] = $model;
110
            } else {
111
                throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
0 ignored issues
show
Bug introduced by
$model->getFirstErrors() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
112
            }
113
        }
114
115
        $this->_resources = $result;
116
117
        return $this;
118
    }
119
120 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...
121
    {
122
        $id = $this->zones[$zone];
123
124
        $result = [];
125
126
        foreach ($this->tariff->resources as $resource) {
127
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
128
                $result[$resource->type] = $resource;
129
            }
130
        }
131
132
        // sorts $result by order of $resource->getAvailableTypes()
133
        $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 126. 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...
134
135
        return $result;
136
    }
137
138 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...
139
    {
140
        $id = $this->zones[$zone];
141
142
        $result = [];
143
144
        foreach ($this->baseTariff->resources as $resource) {
145
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
146
                $result[$resource->type] = $resource;
147
            }
148
        }
149
150
        // sorts $result by order of $resource->getAvailableTypes()
151
        $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 144. 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...
152
153
        return $result;
154
    }
155
156
    public function getZones()
157
    {
158
        return $this->zones;
159
    }
160
}
161