Completed
Push — master ( 9c7fd0...afc45c )
by Andrii
10:58
created

PriceHydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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, 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\yii\DataMapper\hydrator\GeneratedHydratorTrait;
17
use hiqdev\yii\DataMapper\hydrator\RootHydratorAwareTrait;
18
use hiqdev\php\units\Unit;
19
use hiqdev\php\units\Quantity;
20
use hiqdev\yii2\collection\Model;
21
use Money\Currency;
22
use Money\Money;
23
use Money\Number;
24
use yii\helpers\Json;
25
use Zend\Hydrator\HydratorInterface;
26
27
/**
28
 * Class PriceHydrator.
29
 *
30
 * @author Andrii Vasyliev <[email protected]>
31
 */
32
class PriceHydrator implements HydratorInterface
33
{
34
    use RootHydratorAwareTrait {
35
        __construct as rootHydratorAwareConstructor;
36
    }
37
38
    use GeneratedHydratorTrait {
39
        hydrate as generatedHydrate;
40
        createEmptyInstance AS generatedCreateEmptyInstance;
41
    }
42
43
    protected $priceFactory;
44
45
    public function __construct(
46
        HydratorInterface $hydrator,
47
        PriceFactoryInterface $priceFactory
48
    ) {
49
        $this->rootHydratorAwareConstructor($hydrator);
50
        $this->priceFactory = $priceFactory;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     * @param object|Plan $object
56
     */
57
    public function hydrate(array $row, $object)
58
    {
59
        $row['target'] = $this->hydrator->hydrate($row['target'], Target::class);
60
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
61
        $row['unit'] = Unit::create($row['prepaid']['unit']);
62
        $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
63
        $row['currency'] = new Currency(strtoupper($row['price']['currency']));
64
        $row['price'] = new Money($row['price']['amount'], $row['currency']);
65
        $data = Json::decode($row['data']);
66
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
67
68
        return $this->generatedHydrate($row, $object);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     * @param object|Plan $object
74
     */
75 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...
76
    {
77
        $result = array_filter([
78
            'id'            => $object->getId(),
79
            'name'          => $object->getName(),
80
            'parent_id'     => $object->parent->getid(),
81
            'seller_id'     => $object->seller->getid(),
82
        ]);
83
84
        return $result;
85
    }
86
87
    /**
88
     * @param string $className
89
     * @return object
90
     * @throws \ReflectionException
91
     */
92
    public function createEmptyInstance(string $className, array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $className is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        $className = $this->priceFactory->findClassForTypes([$data['type']]);
95
96
        return $this->generatedCreateEmptyInstance($className, $data);
0 ignored issues
show
Unused Code introduced by
The call to PriceHydrator::generatedCreateEmptyInstance() has too many arguments starting with $data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
    }
98
}
99