Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/forms/DomainTariffForm.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2019, 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 yii\web\UnprocessableEntityHttpException;
17
18
class DomainTariffForm extends AbstractTariffForm
19
{
20
    /**
21
     * @var array Domain zones
22
     * Key - zone name (com, net, ...)
23
     * Value - zone id
24
     * @see getZones
25
     */
26
    protected $zones;
27
28 View Code Duplication
    public function load($data, $formName = null)
0 ignored issues
show
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...
29
    {
30
        $this->setAttributes($data[$this->formName()]);
31
        $this->setResources($data[(new DomainResource())->formName()]);
0 ignored issues
show
Deprecated Code introduced by
The class hipanel\modules\finance\models\DomainResource has been deprecated with message: Is implemented in plan module

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
33
        $this->initTariff();
34
35
        return true;
36
    }
37
38
    /**
39
     * @return DomainService[]
40
     */
41
    public function getServices()
42
    {
43
        return $this->createServices($this->tariff->resources);
44
    }
45
46
    /**
47
     * @return DomainService[]
48
     */
49
    public function getParentServices()
50
    {
51
        return $this->createServices($this->parentTariff->resources);
52
    }
53
54
    /**
55
     * @param $resources
56
     * @return DomainService[]
57
     */
58
    protected function createServices($resources)
59
    {
60
        $result = [];
61
        $resource = reset($resources);
62
63
        foreach ($resource->getServiceTypes() as $type => $name) {
64
            $service = new DomainService([
65
                'name' => $name,
66
                'type' => $type,
67
            ]);
68
69
            foreach ($resources as $resource) {
70
                if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) {
71
                    $result[$type] = $service;
72
                    break;
73
                }
74
            }
75
        }
76
77
        return $result;
78
    }
79
80 View Code Duplication
    public function setResources($resources)
0 ignored issues
show
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...
81
    {
82
        $result = [];
83
        foreach ($resources as $resource) {
84
            if ($resource instanceof DomainResource) {
85
                $result[] = $resource;
86
                continue;
87
            }
88
89
            $model = new DomainResource(['scenario' => $this->scenario]);
0 ignored issues
show
Deprecated Code introduced by
The class hipanel\modules\finance\models\DomainResource has been deprecated with message: Is implemented in plan module

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
90
91
            if ($model->load($resource, '') && $model->validate()) {
92
                $result[] = $model;
93
            } else {
94
                throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
0 ignored issues
show
$model->getFirstErrors() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
95
            }
96
        }
97
98
        $this->_resources = $result;
99
100
        return $this;
101
    }
102
103 View Code Duplication
    public function getZoneResources($zone)
0 ignored issues
show
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...
104
    {
105
        $id = $this->zones[$zone];
106
107
        $result = [];
108
109
        foreach ($this->tariff->resources as $resource) {
110
            if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) {
111
                $result[$resource->type] = $resource;
112
            }
113
        }
114
115
        if (empty($result)) {
116
            return [];
117
        }
118
119
        $types = $resource->getTypes();
0 ignored issues
show
The variable $resource seems to be defined by a foreach iteration on line 109. 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...
120
        if (count($result) !== count($types)) {
121
            throw new IntegrityException('Found ' . count($result) . ' resources for zone "' . $zone . '". Must be exactly ' . count($types));
122
        }
123
124
        // sorts $result by order of $resource->getTypes()
125
        $result = array_merge($types, $result);
126
127
        return $result;
128
    }
129
130 View Code Duplication
    public function getZoneParentResources($zone)
0 ignored issues
show
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...
131
    {
132
        $id = $this->zones[$zone];
133
134
        $result = [];
135
136
        foreach ($this->parentTariff->resources as $resource) {
137
            if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) {
138
                $result[$resource->type] = $resource;
139
            }
140
        }
141
142
        if (empty($result)) {
143
            return [];
144
        }
145
146
        $types = $resource->getTypes();
0 ignored issues
show
The variable $resource seems to be defined by a foreach iteration on line 136. 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...
147
        if (count($result) !== count($types)) {
148
            throw new IntegrityException('Found ' . count($result) . ' resources for zone "' . $zone . '". Must be exactly ' . count($types));
149
        }
150
151
        // sorts $result by order of $resource->getTypes()
152
        $result = array_merge($types, $result);
153
154
        return $result;
155
    }
156
157
    public function getZones()
158
    {
159
        return $this->zones;
160
    }
161
162
    public function setZones($zones)
163
    {
164
        $this->zones = array_flip($zones);
165
    }
166
}
167