Completed
Push — master ( 76d596...34b53d )
by Dmitry
04:27
created

CurrencyExchangeForm::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
3
namespace hipanel\modules\finance\forms;
4
5
use hipanel\base\Model;
6
use hipanel\modules\finance\models\Bill;
7
use hiqdev\hiart\ResponseErrorException;
8
9
class CurrencyExchangeForm extends Model
10
{
11
    /**
12
     * @var int
13
     */
14
    public $client_id;
15
16
    /**
17
     * @var string source currency
18
     */
19
    public $from;
20
21
    /**
22
     * @var string target currency
23
     */
24
    public $to;
25
26
    /**
27
     * @var float original sum to be exchanged
28
     */
29
    public $sum;
30
31
    public $result;
32
33
    public function rules()
34
    {
35
        return [
36
            [['client_id', 'from', 'to', 'sum'], 'required'],
37
            [['sum'], 'double'],
38
            [['from', 'to'], 'string', 'length' => 3]
39
        ];
40
    }
41
42
    public function save($runValidation = true, $attributeNames = null)
43
    {
44
        try {
45
            $result = Bill::perform('create-exchange', $this->getAttributes());
46
        } catch (ResponseErrorException $e) {
47
            $this->addError('client_id', $e->getMessage());
48
            return false;
49
        }
50
51
        return $result['id'];
52
    }
53
}
54