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