BillHydrator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 11
eloc 40
c 3
b 0
f 1
dl 0
loc 66
ccs 0
cts 49
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B hydrate() 0 38 7
A extract() 0 16 4
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\bill;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\bill\Bill;
15
use hiqdev\php\billing\bill\BillState;
16
use hiqdev\php\billing\charge\ChargeInterface;
17
use hiqdev\php\billing\customer\Customer;
18
use hiqdev\php\billing\plan\Plan;
19
use hiqdev\php\billing\target\Target;
20
use hiqdev\php\billing\type\Type;
21
use hiqdev\php\units\Quantity;
22
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
23
use Money\Money;
24
25
/**
26
 * Bill Hydrator.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class BillHydrator extends GeneratedHydrator
31
{
32
    /**
33
     * {@inheritdoc}
34
     * @param object|Bill $object
35
     */
36
    public function hydrate(array $row, $object)
37
    {
38
        $row['type']        = $this->hydrator->create($row['type'],     Type::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

38
        /** @scrutinizer ignore-call */ 
39
        $row['type']        = $this->hydrator->create($row['type'],     Type::class);
Loading history...
39
        $row['time']        = $this->hydrator->create($row['time'],     DateTimeImmutable::class);
40
        $row['sum']         = $this->hydrator->create($row['sum'],      Money::class);
41
        $row['quantity']    = $this->hydrator->create($row['quantity'], Quantity::class);
42
        $row['customer']    = $this->hydrator->create($row['customer'], Customer::class);
43
        if (isset($row['target'])) {
44
            $row['target']  = $this->hydrator->create($row['target'],   Target::class);
45
        }
46
        if (isset($row['plan'])) {
47
            $row['plan']    = $this->hydrator->create($row['plan'],     Plan::class);
48
        }
49
        if (isset($row['state'])) {
50
            $row['state']  = $this->hydrator->create($row['state'],   BillState::class);
51
        }
52
53
        $raw_charges = $row['charges'];
54
        unset($row['charges']);
55
56
        /** @var Bill $bill */
57
        $bill = parent::hydrate($row, $object);
58
59
        if (\is_array($raw_charges)) {
60
            $charges = [];
61
            foreach ($raw_charges as $key => $charge) {
62
                if ($charge instanceof ChargeInterface) {
63
                    $charge->setBill($bill);
64
                    $charges[$key] = $charge;
65
                } else {
66
                    $charge['bill'] = $bill;
67
                    $charges[$key] = $this->hydrator->hydrate($charge, ChargeInterface::class);
0 ignored issues
show
Bug introduced by
hiqdev\php\billing\charge\ChargeInterface::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

67
                    $charges[$key] = $this->hydrator->hydrate($charge, /** @scrutinizer ignore-type */ ChargeInterface::class);
Loading history...
68
                }
69
            }
70
            $bill->setCharges($charges);
71
        }
72
73
        return $bill;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     * @param object|Bill $object
79
     */
80
    public function extract($object)
81
    {
82
        return array_filter([
83
            'id'            => $object->getId(),
84
            'type'          => $this->hydrator->extract($object->getType()),
85
            'time'          => $this->hydrator->extract($object->getTime()),
86
            'sum'           => $this->hydrator->extract($object->getSum()),
87
            'quantity'      => $this->hydrator->extract($object->getQuantity()),
88
            'customer'      => $this->hydrator->extract($object->getCustomer()),
89
            'target'        => $object->getTarget() ? $this->hydrator->extract($object->getTarget()) : null,
90
            'plan'          => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null,
91
            'charges'       => $this->hydrator->extractAll($object->getCharges()),
0 ignored issues
show
Bug introduced by
The method extractAll() does not exist on Zend\Hydrator\HydratorInterface. Did you maybe mean extract()? ( Ignorable by Annotation )

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

91
            'charges'       => $this->hydrator->/** @scrutinizer ignore-call */ extractAll($object->getCharges()),

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
            'state'         => $object->getState() ? $this->hydrator->extract($object->getState()) : null,
93
        ], static function ($value): bool {
94
            return $value !== null;
95
        }, ARRAY_FILTER_USE_BOTH);
96
    }
97
}
98