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) |
|
|
|
|
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) |
|
|
|
|
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())); |
|
|
|
|
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) |
|
|
|
|
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(); |
|
|
|
|
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) |
|
|
|
|
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(); |
|
|
|
|
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
|
|
|
|
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.