Completed
Push — master ( cb4835...b23375 )
by Andrii
03:01
created

PriceHydrator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 16.18 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 72.41%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 11
loc 68
ccs 21
cts 29
cp 0.7241
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C hydrate() 0 23 7
A extract() 11 11 1
A createEmptyInstance() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\price\PriceFactoryInterface;
14
use hiqdev\php\billing\target\Target;
15
use hiqdev\php\billing\type\Type;
16
use hiqdev\php\units\Quantity;
17
use hiqdev\php\units\Unit;
18
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
19
use Money\Currency;
20
use Money\Money;
21
use yii\helpers\Json;
22
use Zend\Hydrator\HydratorInterface;
23
24
/**
25
 * Class PriceHydrator.
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class PriceHydrator extends GeneratedHydrator
30
{
31
    protected $priceFactory;
32
33 2
    public function __construct(
34
        HydratorInterface $hydrator,
35
        PriceFactoryInterface $priceFactory
36
    ) {
37 2
        parent::__construct($hydrator);
38 2
        $this->priceFactory = $priceFactory;
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     * @param object|Plan $object
44
     */
45 2
    public function hydrate(array $row, $object)
46
    {
47 2
        $row['target'] = $this->hydrator->hydrate($row['target'], Target::class);
48 2
        $row['type'] = $this->hydrator->hydrate($row['type'], Type::class);
49 2
        if (isset($row['prepaid']['unit'])) {
50 2
            $row['unit'] = Unit::create($row['prepaid']['unit']);
51
        }
52 2
        if (isset($row['prepaid']['quantity'])) {
53 2
            $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
54
        }
55 2
        if (isset($row['price']['currency'])) {
56 2
            $row['currency'] = new Currency(strtoupper($row['price']['currency']));
57
        }
58 2
        if (isset($row['price']['amount'])) {
59 2
            $row['price'] = new Money($row['price']['amount'], $row['currency']);
60
        }
61 2
        if (isset($row['data'])) {
62
            $data = Json::decode($row['data']);
63
        }
64 2
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
65
66 2
        return parent::hydrate($row, $object);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     * @param object|Plan $object
72
     */
73 View Code Duplication
    public function extract($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $result = array_filter([
76
            'id'            => $object->getId(),
77
            'name'          => $object->getName(),
78
            'parent_id'     => $object->parent->getid(),
79
            'seller_id'     => $object->seller->getid(),
80
        ]);
81
82
        return $result;
83
    }
84
85
    /**
86
     * @param string $className
87
     * @throws \ReflectionException
88
     * @return object
89
     */
90 1
    public function createEmptyInstance(string $className, array $data = [])
91
    {
92 1
        $className = $this->priceFactory->findClassForTypes([$data['type']['name']]);
93
94 1
        return parent::createEmptyInstance($className, $data);
95
    }
96
}
97