PriceHydrator::hydrate()   D
last analyzed

Complexity

Conditions 10
Paths 384

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.1

Importance

Changes 6
Bugs 2 Features 1
Metric Value
eloc 19
c 6
b 2
f 1
dl 0
loc 30
ccs 18
cts 20
cp 0.9
rs 4.5333
cc 10
nc 384
nop 2
crap 10.1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\billing\hiapi\models\Price;
14
use hiqdev\php\billing\formula\FormulaInterface;
15
use hiqdev\php\billing\plan\Plan;
16
use hiqdev\php\billing\price\PriceFactoryInterface;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Quantity;
20
use hiqdev\php\units\Unit;
21
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
22
use Money\Currency;
23
use Money\Money;
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 extends GeneratedHydrator
33
{
34
    /**
35
     * @var PriceFactoryInterface|PriceFactory
36
     */
37
    protected $priceFactory;
38
39 3
    public function __construct(
40
        HydratorInterface $hydrator,
41
        PriceFactoryInterface $priceFactory
42
    ) {
43 3
        parent::__construct($hydrator);
44 3
        $this->priceFactory = $priceFactory;
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     * @param object|Plan $object
50
     */
51 4
    public function hydrate(array $row, $object)
52
    {
53 4
        $row['target'] = $this->hydrator->hydrate($row['target'] ?? [], Target::class);
0 ignored issues
show
Bug introduced by
hiqdev\php\billing\target\Target::class of type string is incompatible with the type object expected by parameter $object of Zend\Hydrator\HydrationInterface::hydrate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $row['target'] = $this->hydrator->hydrate($row['target'] ?? [], /** @scrutinizer ignore-type */ Target::class);
Loading history...
54 4
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
55 4
        if (isset($row['prepaid']['unit'])) {
56 4
            $row['unit'] = Unit::create($row['prepaid']['unit']);
57
        }
58 4
        if (isset($row['unit'], $row['prepaid']['quantity'])) {
59 2
            $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
60
        }
61 4
        if (isset($row['price']['currency'])) {
62 4
            $row['currency'] = new Currency(strtoupper($row['price']['currency']));
63
        }
64 4
        if (isset($row['currency'], $row['price']['amount'])) {
65 2
            $row['price'] = new Money($row['price']['amount'], $row['currency']);
66
        }
67 4
        if (!empty($row['plan'])) {
68
            $row['plan'] = $this->hydrator->create($row['plan'], Plan::class);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Zend\Hydrator\HydratorInterface. It seems like you code against a sub-type of Zend\Hydrator\HydratorInterface such as hiqdev\yii\DataMapper\hy...urableAggregateHydrator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            /** @scrutinizer ignore-call */ 
69
            $row['plan'] = $this->hydrator->create($row['plan'], Plan::class);
Loading history...
69
        }
70 4
        if (isset($row['data'])) {
71 2
            $data = is_array($row['data']) ? $row['data'] : Json::decode($row['data']);
72
        }
73 4
        if (!empty($data['formula'])) {
74
            $row['modifier'] = $this->hydrator->hydrate([$data['formula']], FormulaInterface::class);
75
        }
76
77 4
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
78 4
        $row['subprices'] = $data['subprices'] ?? null;
79
80 4
        return parent::hydrate($row, $object);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     * @param object|Price $object
86
     */
87
    public function extract($object)
88
    {
89
        return array_filter([
90
            'id'            => $object->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on hiqdev\billing\hiapi\models\Price. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            'id'            => $object->/** @scrutinizer ignore-call */ getId(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            'type'          => $this->hydrator->extract($object->getType()),
0 ignored issues
show
Bug introduced by
The method getType() does not exist on hiqdev\billing\hiapi\models\Price. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
            'type'          => $this->hydrator->extract($object->/** @scrutinizer ignore-call */ getType()),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            'target'        => $this->hydrator->extract($object->getTarget()),
0 ignored issues
show
Bug introduced by
The method getTarget() does not exist on hiqdev\billing\hiapi\models\Price. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            'target'        => $this->hydrator->extract($object->/** @scrutinizer ignore-call */ getTarget()),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
            'plan'          => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null,
0 ignored issues
show
Bug introduced by
The method getPlan() does not exist on hiqdev\billing\hiapi\models\Price. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            'plan'          => $object->/** @scrutinizer ignore-call */ getPlan() ? $this->hydrator->extract($object->getPlan()) : null,

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        ]);
95
    }
96
97
    /**
98
     * @param string $className
99
     * @param array $data
100
     * @throws \ReflectionException
101
     * @return object
102
     */
103 2
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function createEmptyInstance(/** @scrutinizer ignore-unused */ string $className, array $data = [])

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

Loading history...
104
    {
105 2
        if (isset($data['data']) && !is_array($data['data'])) {
106 1
            $additionalData = Json::decode($data['data']);
107
        }
108
109 2
        $className = $this->priceFactory->findClassForTypes([
110 2
            $additionalData['class'] ?? null,
111 2
            $data['type']['name'],
112
        ]);
113
114 2
        return parent::createEmptyInstance($className, $data);
115
    }
116
}
117