Completed
Push — master ( 22cb37...1cdde8 )
by Dmitry
02:19
created

QuantityDto::unitValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hiqdev\billing\hiapi\commands\order;
4
5
use hiqdev\php\units\Quantity;
6
use yii\base\Model;
7
8
class QuantityDto extends Model
9
{
10
    public $unit;
11
12
    public $quantity;
13
14
    public function rules()
15
    {
16
        return [
17
            [['unit', 'quantity'], 'required'],
18
            ['unit', 'string'],
19
            ['quantity', 'number'],
20
            ['unit', 'unitValidation']
21
        ];
22
    }
23
24
    public function unitValidation()
25
    {
26
        try {
27
            Quantity::create($this->unit, $this->quantity);
28
            return true;
29
        } catch (\Exception $exception) {
30
            $this->addError('unit', $exception->getMessage());
31
            return false;
32
        }
33
    }
34
35
    public function load($data, $formName = '')
36
    {
37
        return parent::load($data, $formName);
38
    }
39
}
40