Completed
Push — master ( c83831...4ba780 )
by Andrii
02:57
created

PlanHydrator::hydrate()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 2
nop 2
crap 20
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\plan;
12
13
use hiqdev\php\billing\customer\Customer;
14
use hiqdev\php\billing\plan\Plan;
15
use hiqdev\php\billing\price\PriceInterface;
16
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
17
18
/**
19
 * Class PlanHydrator.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class PlanHydrator extends GeneratedHydrator
24
{
25
    /**
26
     * {@inheritdoc}
27
     * @param object|Plan $object
28
     * @throws \Exception
29
     */
30
    public function hydrate(array $data, $object)
31
    {
32
        $data['seller'] = $this->hydrator->hydrate($data['seller'], Customer::class);
33
        $raw_prices = $data['prices'];
34
        unset($data['prices']);
35
36
        /** @var Plan $plan */
37
        $plan = parent::hydrate($data, $object);
38
39
        if (is_array($raw_prices)) {
40
            $prices = [];
41
            foreach ($raw_prices as $key => $price) {
42
                if ($price instanceof PriceInterface) {
43
                    $price->setPlan($plan);
0 ignored issues
show
Bug introduced by
The method setPlan() does not seem to exist on object<hiqdev\php\billing\price\PriceInterface>.

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...
44
                    $prices[$key] = $price;
45
                } else {
46
                    $price['plan'] = $plan;
47
                    $prices[$key] = $this->hydrator->hydrate($price, PriceInterface::class);
48
                }
49
            }
50
            $plan->setPrices($prices);
51
        }
52
53
        return $plan;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     * @param Plan $object
59
     */
60
    public function extract($object)
61
    {
62
        $result = array_filter([
63
            'id'            => $object->getId(),
64
            'name'          => $object->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<hiqdev\php\billing\plan\Plan>.

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...
65
            'seller'        => $object->getSeller() ? $this->hydrator->extract($object->getSeller()) : null,
66
            'parent'        => $object->getParent() ? $this->hydrator->extract($object->getParent()) : null,
0 ignored issues
show
Bug introduced by
The method getParent() does not seem to exist on object<hiqdev\php\billing\plan\Plan>.

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...
67
        ]);
68
69
        return $result;
70
    }
71
}
72