Completed
Push — master ( a45d97...ed433d )
by Dmitry
08:33 queued 05:59
created

DomainTariffForm::setResources()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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