Completed
Push — master ( fa9e98...bd4ae6 )
by Dmitry
01:56
created

PriceHydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
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
    public function __construct(
34
        HydratorInterface $hydrator,
35
        PriceFactoryInterface $priceFactory
36
    ) {
37
        parent::__construct($hydrator);
38
        $this->priceFactory = $priceFactory;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     * @param object|Plan $object
44
     */
45
    public function hydrate(array $row, $object)
46
    {
47
        $row['target'] = $this->hydrator->hydrate($row['target'], Target::class);
48
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
49
        if (isset($row['prepaid']['unit'])) {
50
            $row['unit'] = Unit::create($row['prepaid']['unit']);
51
        }
52
        if (isset($row['unit']) && isset($row['prepaid']['quantity'])) {
53
            $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
54
        }
55
        if (isset($row['price']['currency'])) {
56
            $row['currency'] = new Currency(strtoupper($row['price']['currency']));
57
        }
58
        if (isset($row['currency']) && isset($row['price']['amount'])) {
59
            $row['price'] = new Money($row['price']['amount'], $row['currency']);
60
        }
61
        if (isset($row['data'])) {
62
            $data = Json::decode($row['data']);
0 ignored issues
show
Documentation introduced by
$row['data'] is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
        }
64
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
65
66
        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
    public function createEmptyInstance(string $className, array $data = [])
91
    {
92
        $className = $this->priceFactory->findClassForTypes([$data['type']['name']]);
93
94
        return parent::createEmptyInstance($className, $data);
95
    }
96
}
97