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\logic; |
12
|
|
|
|
13
|
|
|
use hipanel\models\Ref; |
14
|
|
|
use hipanel\modules\finance\forms\CertificateTariffForm; |
15
|
|
|
use hipanel\modules\finance\forms\DomainTariffForm; |
16
|
|
|
use hipanel\modules\finance\models\Tariff; |
17
|
|
|
use hiqdev\hiart\ConnectionInterface; |
18
|
|
|
use hiqdev\hiart\ResponseErrorException; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\web\NotFoundHttpException; |
21
|
|
|
use yii\web\UnprocessableEntityHttpException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class CertificateTariffManager |
25
|
|
|
* |
26
|
|
|
* @author Dmytro Naumenko <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CertificateTariffManager extends AbstractTariffManager |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DomainTariffForm |
32
|
|
|
*/ |
33
|
|
|
public $form; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected $type = Tariff::TYPE_CERT; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ConnectionInterface |
42
|
|
|
*/ |
43
|
|
|
private $connection; |
44
|
|
|
|
45
|
|
|
public function __construct(ConnectionInterface $connection, $config = []) |
46
|
|
|
{ |
47
|
|
|
$this->connection = $connection; |
48
|
|
|
|
49
|
|
|
parent::__construct($config); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function init() |
53
|
|
|
{ |
54
|
|
|
parent::init(); |
55
|
|
|
|
56
|
|
|
if (!Yii::getAlias('@certificate', true)) { |
57
|
|
|
throw new NotFoundHttpException('Certificate module is missing'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function insert() |
62
|
|
|
{ |
63
|
|
|
$data = $this->form->toArray(); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$result = Tariff::perform('create', $data); |
67
|
|
|
} catch (ResponseErrorException $e) { |
68
|
|
|
throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->form->id = $result['id']; |
72
|
|
|
|
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function update() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$data = $this->form->toArray(); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$result = Tariff::perform('update', $data); |
|
|
|
|
82
|
|
|
} catch (ResponseErrorException $e) { |
83
|
|
|
throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getFormOptions() |
90
|
|
|
{ |
91
|
|
|
return array_merge([ |
92
|
|
|
'class' => CertificateTariffForm::class, |
93
|
|
|
'certificateTypes' => Ref::getList('type,certificate', 'hipanel:certificate', [ |
94
|
|
|
'select' => 'id_label', |
95
|
|
|
'mapOptions' => ['from' => 'id'] |
96
|
|
|
]), |
97
|
|
|
], parent::getFormOptions()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.