Completed
Push — master ( 150de4...ecb3c3 )
by Dmitry
05:20
created

DomainTariffForm::getZoneParentResources()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 17
loc 17
ccs 0
cts 11
cp 0
rs 9.2
cc 4
eloc 8
nc 3
nop 1
crap 20
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
    public function load($data, $formName = null)
21
    {
22
        $this->setAttributes($data[$this->formName()]);
23
        $this->setResources($data[(new DomainResource())->formName()]);
24
25
        $this->initTariff();
26
27
        return true;
28
    }
29
30
    /**
31
     * @return DomainService[]
32
     */
33
    public function getServices()
34
    {
35
        return $this->createServices($this->tariff->resources);
36
    }
37
38
    /**
39
     * @return DomainService[]
40
     */
41
    public function getParentServices()
42
    {
43
        return $this->createServices($this->parentTariff->resources);
44
    }
45
46
    /**
47
     * @param $resources
48
     * @return DomainService[]
49
     *
50
     */
51
    protected function createServices($resources)
52
    {
53
        $result = [];
54
        $resource = reset($resources);
55
56
        foreach ($resource->getServiceTypes() as $type => $name) {
57
            $service = new DomainService([
58
                'name' => $name,
59
                'type' => $type,
60
            ]);
61
62
            foreach ($resources as $resource) {
63
                if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) {
64
                    $result[$type] = $service;
65
                    break;
66
                }
67
            }
68
        }
69
70
        return $result;
71
    }
72
73 View Code Duplication
    public function setResources($resources)
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...
74
    {
75
        $result = [];
76
        foreach ($resources as $resource) {
77
            if ($resource instanceof DomainResource) {
78
                $result[] = $resource;
79
                continue;
80
            }
81
82
            $model = new DomainResource(['scenario' => $this->scenario]);
83
84
            if ($model->load($resource, '') && $model->validate()) {
85
                $result[] = $model;
86
            } else {
87
                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...
88
            }
89
        }
90
91
        $this->_resources = $result;
92
93
        return $this;
94
    }
95
96 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...
97
    {
98
        $id = $this->zones[$zone];
99
100
        $result = [];
101
102
        foreach ($this->tariff->resources as $resource) {
103
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
104
                $result[$resource->type] = $resource;
105
            }
106
        }
107
108
        // sorts $result by order of $resource->getTypes()
109
        $result = array_merge($resource->getTypes(), $result);
0 ignored issues
show
Bug introduced by
The variable $resource seems to be defined by a foreach iteration on line 102. 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...
110
111
        return $result;
112
    }
113
114 View Code Duplication
    public function getZoneParentResources($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...
115
    {
116
        $id = $this->zones[$zone];
117
118
        $result = [];
119
120
        foreach ($this->parentTariff->resources as $resource) {
121
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
122
                $result[$resource->type] = $resource;
123
            }
124
        }
125
126
        // sorts $result by order of $resource->getTypes()
127
        $result = array_merge($resource->getTypes(), $result);
0 ignored issues
show
Bug introduced by
The variable $resource seems to be defined by a foreach iteration on line 120. 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...
128
129
        return $result;
130
    }
131
132
    public function getZones()
133
    {
134
        return $this->zones;
135
    }
136
137
    public function setZones(array $zones)
138
    {
139
        $this->zones = array_flip($zones);
140
    }
141
}
142