QuantityDto::unitValidation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\commands\order;
12
13
use hiqdev\php\units\Quantity;
14
use yii\base\Model;
15
16
class QuantityDto extends Model
17
{
18
    public $unit;
19
20
    public $quantity;
21
22
    public function rules()
23
    {
24
        return [
25
            [['unit', 'quantity'], 'required'],
26
            ['unit', 'string'],
27
            ['quantity', 'number'],
28
            ['unit', 'unitValidation'],
29
        ];
30
    }
31
32
    public function unitValidation()
33
    {
34
        try {
35
            Quantity::create($this->unit, $this->quantity);
36
37
            return true;
38
        } catch (\Exception $exception) {
39
            $this->addError('unit', $exception->getMessage());
40
41
            return false;
42
        }
43
    }
44
45
    public function load($data, $formName = '')
46
    {
47
        return parent::load($data, $formName);
48
    }
49
}
50