Completed
Push — master ( a00223...b355d9 )
by Dmitry
01:57
created

PriceRepository::negotiatePriceAndUnit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
crap 12
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\plan;
12
13
use hiqdev\yii\DataMapper\components\ConnectionInterface;
14
use hiqdev\php\billing\price\PriceFactoryInterface;
15
use hiqdev\php\billing\target\Target;
16
use hiqdev\php\billing\type\Type;
17
use hiqdev\php\units\Unit;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\yii2\collection\Model;
20
use Money\Currency;
21
use Money\Money;
22
use Money\Number;
23
use yii\helpers\Json;
24
25
class PriceRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository
26
{
27
    public $queryClass = PriceQuery::class;
28
29
    /**
30
     * @var PriceFactoryInterface
31
     */
32
    protected $factory;
33
34
    public function __construct(
35
        ConnectionInterface $db,
36
        PriceFactoryInterface $factory,
37
        array $config = []
38
    ) {
39
        parent::__construct($config);
40
41
        $this->db = $db;
42
        $this->factory = $factory;
43
    }
44
45
    public function create(array $row)
46
    {
47
        $row['target'] = $this->createEntity(Target::class, $row['target']);
48
        $row['type'] = $this->createEntity(Type::class, $row['type']);
49
        $row['unit'] = Unit::create($row['prepaid']['unit']);
50
        $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
51
        $row['currency'] = new Currency(strtoupper($row['price']['currency']));
52
        $row['price'] = new Money($row['price']['amount'], $row['currency']);
53
        $data = Json::decode($row['data']);
54
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
55
56
        return parent::create($row);
57
    }
58
}
59