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, 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\yii\DataMapper\hydrator\GeneratedHydratorTrait; |
17
|
|
|
use hiqdev\yii\DataMapper\hydrator\RootHydratorAwareTrait; |
18
|
|
|
use hiqdev\php\units\Unit; |
19
|
|
|
use hiqdev\php\units\Quantity; |
20
|
|
|
use hiqdev\yii2\collection\Model; |
21
|
|
|
use Money\Currency; |
22
|
|
|
use Money\Money; |
23
|
|
|
use Money\Number; |
24
|
|
|
use yii\helpers\Json; |
25
|
|
|
use Zend\Hydrator\HydratorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class PriceHydrator. |
29
|
|
|
* |
30
|
|
|
* @author Andrii Vasyliev <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class PriceHydrator implements HydratorInterface |
33
|
|
|
{ |
34
|
|
|
use RootHydratorAwareTrait { |
35
|
|
|
__construct as rootHydratorAwareConstructor; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
use GeneratedHydratorTrait { |
39
|
|
|
hydrate as generatedHydrate; |
40
|
|
|
createEmptyInstance AS generatedCreateEmptyInstance; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected $priceFactory; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
HydratorInterface $hydrator, |
47
|
|
|
PriceFactoryInterface $priceFactory |
48
|
|
|
) { |
49
|
|
|
$this->rootHydratorAwareConstructor($hydrator); |
50
|
|
|
$this->priceFactory = $priceFactory; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* @param object|Plan $object |
56
|
|
|
*/ |
57
|
|
|
public function hydrate(array $row, $object) |
58
|
|
|
{ |
59
|
|
|
$row['target'] = $this->hydrator->hydrate($row['target'], Target::class); |
60
|
|
|
$row['type'] = $this->hydrator->hydrate($row['type'], Type::class); |
61
|
|
|
$row['unit'] = Unit::create($row['prepaid']['unit']); |
62
|
|
|
$row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
63
|
|
|
$row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
64
|
|
|
$row['price'] = new Money($row['price']['amount'], $row['currency']); |
65
|
|
|
$data = Json::decode($row['data']); |
66
|
|
|
$row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
67
|
|
|
|
68
|
|
|
return $this->generatedHydrate($row, $object); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* @param object|Plan $object |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function extract($object) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$result = array_filter([ |
78
|
|
|
'id' => $object->getId(), |
79
|
|
|
'name' => $object->getName(), |
80
|
|
|
'parent_id' => $object->parent->getid(), |
81
|
|
|
'seller_id' => $object->seller->getid(), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $className |
89
|
|
|
* @return object |
90
|
|
|
* @throws \ReflectionException |
91
|
|
|
*/ |
92
|
|
|
public function createEmptyInstance(string $className, array $data) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$className = $this->priceFactory->findClassForTypes([$data['type']]); |
95
|
|
|
|
96
|
|
|
return $this->generatedCreateEmptyInstance($className, $data); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.