Completed
Push — master ( 920b36...b9da09 )
by Dmitry
04:37
created

CertificateTariffForm::setResources()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 22
rs 8.6737
cc 5
eloc 13
nc 4
nop 1
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\CertificateResource;
15
use Yii;
16
use yii\web\UnprocessableEntityHttpException;
17
18
/**
19
 * Class CertificateTariffForm
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 */
23
class CertificateTariffForm extends AbstractTariffForm
24
{
25
    protected $certificateTypes = [];
26
27
    public function __construct(array $config = [])
28
    {
29
        parent::__construct($config);
30
    }
31
32 View Code Duplication
    public function load($data, $formName = null)
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...
33
    {
34
        $this->setAttributes($data[$this->formName()]);
35
        $this->setResources($data[(new CertificateResource())->formName()]);
36
37
        $this->initTariff();
38
39
        return true;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public static function getPeriods()
46
    {
47
        return CertificateResource::getPeriods();
48
    }
49
50 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...
51
    {
52
        $result = [];
53
        foreach ($resources as $resource) {
54
            if ($resource instanceof CertificateResource) {
55
                $result[] = $resource;
56
                continue;
57
            }
58
59
            $model = new CertificateResource(['scenario' => $this->scenario]);
60
61
            if ($model->load($resource, '') && $model->validate()) {
62
                $result[] = $model;
63
            } else {
64
                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...
65
            }
66
        }
67
68
        $this->_resources = $result;
69
70
        return $this;
71
    }
72
73
    public function getCertificateTypes()
74
    {
75
        $result = [];
76
77
        foreach ($this->tariff->resources as $resource) {
78
            if (isset($this->certificateTypes[$resource->object_id])) {
79
                $result[$resource->object_id] = $this->certificateTypes[$resource->object_id];
80
            }
81
        }
82
83
        return $result;
84
    }
85
86
    protected function getCertificateTypeId($type)
87
    {
88
        return array_search($type, $this->certificateTypes, true);
89
    }
90
91
    /**
92
     * @param $type
93
     * @return CertificateResource[]
94
     * @throws IntegrityException
95
     */
96 View Code Duplication
    public function getTypeResources($type)
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->getCertificateTypeId($type);
99
100
        $result = [];
101
102
        foreach ($this->tariff->resources as $resource) {
103
            if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) {
104
                $result[$resource->type] = $resource;
105
            }
106
        }
107
108
        $types = $resource->getTypes();
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...
109
        if (count($result) !== count($types)) {
110
            throw new IntegrityException('Found ' . count($result) . ' resources for certificate "' . $type . '". Must be exactly ' . count($types));
111
        }
112
113
        // sorts $result by order of $resource->getTypes()
114
        $result = array_merge($types, $result);
115
116
        return $result;
117
    }
118
119
    /**
120
     * @param array $certificateTypes
121
     */
122
    public function setCertificateTypes($certificateTypes)
123
    {
124
        $this->certificateTypes = $certificateTypes;
125
    }
126
127
    /**
128
     * @param $certificateType
129
     * @return CertificateResource[]
130
     * @throws IntegrityException
131
     */
132 View Code Duplication
    public function getTypeParentResources($certificateType)
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...
133
    {
134
        $id = $this->getCertificateTypeId($certificateType);
135
136
        $result = [];
137
138
        foreach ($this->parentTariff->resources as $resource) {
139
            if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) {
140
                $result[$resource->type] = $resource;
141
            }
142
        }
143
144
        $types = $resource->getTypes();
0 ignored issues
show
Bug introduced by
The variable $resource seems to be defined by a foreach iteration on line 138. 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...
145
        if (count($result) !== count($types)) {
146
            throw new IntegrityException('Found ' . count($result) . ' resources for certificate "' . $certificateType . '". Must be exactly ' . count($types));
147
        }
148
149
        // sorts $result by order of $resource->getTypes()
150
        $result = array_merge($types, $result);
151
152
        return $result;
153
    }
154
}
155