Completed
Push — master ( cb4835...b23375 )
by Andrii
03:01
created

PriceHydrator::hydrate()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 15
cp 0.9333
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 64
nop 2
crap 7.0145
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\price;
12
13
use hiqdev\php\billing\price\PriceFactoryInterface;
14
use hiqdev\php\billing\target\Target;
15
use hiqdev\php\billing\type\Type;
16
use hiqdev\php\units\Quantity;
17
use hiqdev\php\units\Unit;
18
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
19
use Money\Currency;
20
use Money\Money;
21
use yii\helpers\Json;
22
use Zend\Hydrator\HydratorInterface;
23
24
/**
25
 * Class PriceHydrator.
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class PriceHydrator extends GeneratedHydrator
30
{
31
    protected $priceFactory;
32
33 2
    public function __construct(
34
        HydratorInterface $hydrator,
35
        PriceFactoryInterface $priceFactory
36
    ) {
37 2
        parent::__construct($hydrator);
38 2
        $this->priceFactory = $priceFactory;
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     * @param object|Plan $object
44
     */
45 2
    public function hydrate(array $row, $object)
46
    {
47 2
        $row['target'] = $this->hydrator->hydrate($row['target'], Target::class);
48 2
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
49 2
        if (isset($row['prepaid']['unit'])) {
50 2
            $row['unit'] = Unit::create($row['prepaid']['unit']);
51
        }
52 2
        if (isset($row['prepaid']['quantity'])) {
53 2
            $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
54
        }
55 2
        if (isset($row['price']['currency'])) {
56 2
            $row['currency'] = new Currency(strtoupper($row['price']['currency']));
57
        }
58 2
        if (isset($row['price']['amount'])) {
59 2
            $row['price'] = new Money($row['price']['amount'], $row['currency']);
60
        }
61 2
        if (isset($row['data'])) {
62
            $data = Json::decode($row['data']);
63
        }
64 2
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
65
66 2
        return parent::hydrate($row, $object);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     * @param object|Plan $object
72
     */
73 View Code Duplication
    public function extract($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        $result = array_filter([
76
            'id'            => $object->getId(),
77
            'name'          => $object->getName(),
78
            'parent_id'     => $object->parent->getid(),
79
            'seller_id'     => $object->seller->getid(),
80
        ]);
81
82
        return $result;
83
    }
84
85
    /**
86
     * @param string $className
87
     * @throws \ReflectionException
88
     * @return object
89
     */
90 1
    public function createEmptyInstance(string $className, array $data = [])
91
    {
92 1
        $className = $this->priceFactory->findClassForTypes([$data['type']['name']]);
93
94 1
        return parent::createEmptyInstance($className, $data);
95
    }
96
}
97