Completed
Pull Request — master (#2)
by Klochok
02:48
created

PriceHydrator::hydrate()   F

Complexity

Conditions 13
Paths 384

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 13.169

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 18
cts 20
cp 0.9
rs 3.7737
c 0
b 0
f 0
cc 13
eloc 19
nc 384
nop 2
crap 13.169

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\php\billing\formula\FormulaInterface;
14
use hiqdev\php\billing\plan\Plan;
15
use hiqdev\php\billing\price\PriceFactoryInterface;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\billing\type\Type;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\php\units\Unit;
20
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
21
use Money\Currency;
22
use Money\Money;
23
use yii\helpers\Json;
24
use Zend\Hydrator\HydratorInterface;
25
26
/**
27
 * Class PriceHydrator.
28
 *
29
 * @author Andrii Vasyliev <[email protected]>
30
 */
31
class PriceHydrator extends GeneratedHydrator
32
{
33
    /**
34
     * @var PriceFactoryInterface|PriceFactory
35
     */
36
    protected $priceFactory;
37
38 3
    public function __construct(
39
        HydratorInterface $hydrator,
40
        PriceFactoryInterface $priceFactory
41
    ) {
42 3
        parent::__construct($hydrator);
43 3
        $this->priceFactory = $priceFactory;
44 3
    }
45
46
    /**
47
     * {@inheritdoc}
48
     * @param object|Plan $object
49
     */
50 4
    public function hydrate(array $row, $object)
51
    {
52 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

52
        $row['target'] = $this->hydrator->hydrate($row['target'] ?? [], /** @scrutinizer ignore-type */ Target::class);
Loading history...
53 4
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
54 4
        if (isset($row['prepaid']['unit'])) {
55 4
            $row['unit'] = Unit::create($row['prepaid']['unit']);
56
        }
57 4
        if (isset($row['unit']) && isset($row['prepaid']['quantity'])) {
58 2
            $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
59
        }
60 4
        if (isset($row['price']['currency'])) {
61 4
            $row['currency'] = new Currency(strtoupper($row['price']['currency']));
62
        }
63 4
        if (isset($row['currency']) && isset($row['price']['amount'])) {
64 2
            $row['price'] = new Money($row['price']['amount'], $row['currency']);
65
        }
66 4
        if (!empty($row['plan'])) {
67
            $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

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

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

90
            '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...
91
            'target'        => $this->hydrator->extract($object->getTarget()),
0 ignored issues
show
Bug introduced by
The method getTarget() does not exist on hiqdev\php\billing\plan\Plan. ( Ignorable by Annotation )

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

91
            '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...
92
            'plan'          => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null,
0 ignored issues
show
Bug introduced by
The method getPlan() does not exist on hiqdev\php\billing\plan\Plan. Did you maybe mean getParent()? ( Ignorable by Annotation )

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

92
            '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...
93
        ]);
94
    }
95
96
    /**
97
     * @param string $className
98
     * @param array $data
99
     * @throws \ReflectionException
100
     * @return object
101
     */
102 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

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