Completed
Push — master ( 104946...6683dc )
by Dmitry
02:26
created

PlanHydrator::createEmptyInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
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
        if (!empty($data['seller'])) {
33
            $data['seller'] = $this->hydrator->hydrate($data['seller'], Customer::class);
0 ignored issues
show
Bug introduced by
hiqdev\php\billing\customer\Customer::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

33
            $data['seller'] = $this->hydrator->hydrate($data['seller'], /** @scrutinizer ignore-type */ Customer::class);
Loading history...
34
        }
35
        $raw_prices = $data['prices'];
36
        unset($data['prices']);
37
38
        /** @var Plan $plan */
39
        $plan = parent::hydrate($data, $object);
40
41
        if (is_array($raw_prices)) {
42
            $prices = [];
43
            foreach ($raw_prices as $key => $price) {
44
                if ($price instanceof PriceInterface) {
45
                    $price->setPlan($plan);
46
                    $prices[$key] = $price;
47
                } else {
48
                    $price['plan'] = $plan;
49
                    $prices[$key] = $this->hydrator->hydrate($price, PriceInterface::class);
50
                }
51
            }
52
            $plan->setPrices($prices);
53
        }
54
55
        return $plan;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     * @param Plan $object
61
     */
62
    public function extract($object)
63
    {
64
        $result = array_filter([
65
            'id'            => $object->getId(),
66
            'name'          => $object->getName(),
67
            'seller'        => $object->getSeller() ? $this->hydrator->extract($object->getSeller()) : null,
68
            'parent'        => $object->getParent() ? $this->hydrator->extract($object->getParent()) : null,
69
            'is_grouping'   => $object instanceof GroupingPlan,
70
        ]);
71
72
        return $result;
73
    }
74
75
    /**
76
     * @param string $className
77
     * @param array $data
78
     * @throws \ReflectionException
79
     * @return object
80
     */
81
    public function createEmptyInstance(string $className, array $data = [])
82
    {
83
        if (isset($data['is_grouping']) && $data['is_grouping'] === true) {
84
            $className = GroupingPlan::class;
85
        }
86
87
        return parent::createEmptyInstance($className, $data);
88
    }
89
}
90