Completed
Push — master ( 030ae6...89eb04 )
by Dmitry
04:29
created

DomainTariffForm::getBaseServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
        return $this;
40
    }
41
42
    public function load($data)
43
    {
44
        $this->setAttributes($data[$this->formName()]);
45
        $this->setResources($data[(new DomainResource())->formName()]);
46
47
        return true;
48
    }
49
50
    /**
51
     * @return DomainService[]
52
     */
53
    public function getServices()
54
    {
55
        return $this->createServices($this->tariff->resources);
56
    }
57
58
    /**
59
     * @return DomainService[]
60
     */
61
    public function getBaseServices()
62
    {
63
        return $this->createServices($this->baseTariff->resources);
64
    }
65
66
    /**
67
     * @param $resources
68
     * @return DomainService[]
69
     *
70
     */
71
    protected function createServices($resources)
72
    {
73
        $result = [];
74
        $resource = reset($resources);
75
76
        foreach ($resource->getServiceTypes() as $type => $name) {
77
            $service = new DomainService([
78
                'name' => $name,
79
                'type' => $type,
80
            ]);
81
82
            foreach ($resources as $resource) {
83
                if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) {
84
                    $result[$type] = $service;
85
                    break;
86
                }
87
            }
88
        }
89
90
        return $result;
91
    }
92
93
    public function setResources($resources)
94
    {
95
        $result = [];
96
        foreach ($resources as $resource) {
97
            if ($resource instanceof DomainResource) {
98
                $result[] = $resource;
99
                continue;
100
            }
101
102
            $model = new DomainResource(['scenario' => 'create']);
103
104
            if ($model->load($resource, '') && $model->validate()) {
105
                $result[] = $model;
106
            } else {
107
                throw new UnprocessableEntityHttpException('Failed to load resource model');
108
            }
109
        }
110
111
        $this->_resources = $result;
112
113
        return $this;
114
    }
115
116 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...
117
    {
118
        $id = $this->zones[$zone];
119
120
        $result = [];
121
122
        foreach ($this->tariff->resources as $resource) {
123
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
124
                $result[$resource->type] = $resource;
125
            }
126
        }
127
128
        // sorts $result by order of $resource->getAvailableTypes()
129
        $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 122. 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...
130
131
        return $result;
132
    }
133
134 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...
135
    {
136
        $id = $this->zones[$zone];
137
138
        $result = [];
139
140
        foreach ($this->baseTariff->resources as $resource) {
141
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
142
                $result[$resource->type] = $resource;
143
            }
144
        }
145
146
        // sorts $result by order of $resource->getAvailableTypes()
147
        $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 140. 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...
148
149
        return $result;
150
    }
151
152
    public function getZones()
153
    {
154
        return $this->zones;
155
    }
156
}
157