Completed
Push — master ( 844c41...030ae6 )
by Dmitry
04:17
created

DomainTariffForm::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
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\Tariff;
7
use yii\web\UnprocessableEntityHttpException;
8
9
class DomainTariffForm extends AbstractTariffForm
10
{
11
    /**
12
     * @var array Domain zones
13
     * Key - zone name (com, net, ...)
14
     * Value - zone id
15
     * @see getZones
16
     */
17
    protected $zones;
18
19
    /**
20
     * @param array $zones
21
     * @param Tariff $baseTariff
22
     * @param Tariff $tariff
23
     * @return $this
24
     */
25
    public function fill($zones, Tariff $baseTariff, Tariff $tariff = null)
26
    {
27
        $this->tariff = isset($tariff) ? $tariff : $baseTariff;
28
        $this->baseTariff = $baseTariff;
29
        $this->zones = array_flip($zones);
30
31
        if (isset($tariff)) {
32
            $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...
33
            $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...
34
        }
35
36
        $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...
37
38
        return $this;
39
    }
40
41
    public function load($data)
42
    {
43
        $this->setAttributes($data[$this->formName()]);
44
        $this->setResources($data[(new DomainResource())->formName()]);
45
46
        return true;
47
    }
48
49
    public function setResources($resources)
50
    {
51
        $result = [];
52
        foreach ($resources as $resource) {
53
            if ($resource instanceof DomainResource) {
54
                $result[] = $resource;
55
                continue;
56
            }
57
58
            $model = new DomainResource(['scenario' => 'create']);
59
60
            if ($model->load($resource, '') && $model->validate()) {
61
                $result[] = $model;
62
            } else {
63
                throw new UnprocessableEntityHttpException('Failed to load resource model');
64
            }
65
        }
66
67
        $this->_resources = $result;
68
69
        return $this;
70
    }
71
72 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...
73
    {
74
        $id = $this->zones[$zone];
75
76
        $result = [];
77
78
        foreach ($this->tariff->resources as $resource) {
79
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
80
                $result[$resource->type] = $resource;
81
            }
82
        }
83
84
        // sorts $result by order of $resource->getAvailableTypes()
85
        $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 78. 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...
86
87
        return $result;
88
    }
89
90 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...
91
    {
92
        $id = $this->zones[$zone];
93
94
        $result = [];
95
96
        foreach ($this->baseTariff->resources as $resource) {
97
            if ($resource->object_id == $id && $resource->isTypeCorrect()) {
98
                $result[$resource->type] = $resource;
99
            }
100
        }
101
102
        // sorts $result by order of $resource->getAvailableTypes()
103
        $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 96. 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...
104
105
        return $result;
106
    }
107
108
    public function getZones()
109
    {
110
        return $this->zones;
111
    }
112
}
113