Completed
Push — master ( 834dc7...c72fcf )
by Dmitry
14:24
created

CertificateTariffForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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