1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\price; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Default price factory. |
15
|
|
|
* |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class PriceFactory implements PriceFactoryInterface |
19
|
|
|
{ |
20
|
|
|
protected $creators = [ |
21
|
|
|
EnumPrice::class => 'createEnumPrice', |
22
|
|
|
RatePrice::class => 'createRatePrice', |
23
|
|
|
SinglePrice::class => 'createSinglePrice', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
protected $types = [ |
27
|
|
|
'enum' => EnumPrice::class, |
28
|
|
|
'single' => SinglePrice::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string default price class, when given will be used for not found types |
33
|
|
|
*/ |
34
|
|
|
protected $defaultClass = null; |
35
|
16 |
|
|
36
|
|
|
public function __construct(array $types = [], $defaultClass = null) |
37
|
16 |
|
{ |
38
|
16 |
|
$this->types = $types; |
39
|
16 |
|
$this->defaultClass = $defaultClass; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
4 |
|
* Creates price object. |
45
|
|
|
*/ |
46
|
4 |
|
public function create(PriceCreationDto $dto): PriceInterface |
47
|
4 |
|
{ |
48
|
4 |
|
$class = $this->findClassForTypes([ |
49
|
|
|
get_class($dto), |
50
|
4 |
|
$dto->type->getName(), |
51
|
|
|
]); |
52
|
4 |
|
$method = $this->findMethodForClass($class); |
53
|
|
|
|
54
|
|
|
return $this->{$method}($dto); |
55
|
4 |
|
} |
56
|
|
|
|
57
|
4 |
|
public function findClassForTypes(array $types) |
58
|
4 |
|
{ |
59
|
4 |
|
foreach ($types as $type) { |
60
|
|
|
if (isset($this->types[$type])) { |
61
|
|
|
return $this->types[$type]; |
62
|
2 |
|
} |
63
|
2 |
|
} |
64
|
|
|
if ($this->defaultClass) { |
65
|
|
|
return $this->defaultClass; |
66
|
|
|
} |
67
|
|
|
throw new FailedCreatePriceException(sprintf('unknown types: "%s"', implode(',', $types))); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
4 |
|
public function findMethodForClass($class) |
71
|
4 |
|
{ |
72
|
|
|
if (isset($this->creators[$class])) { |
73
|
|
|
return $this->creators[$class]; |
74
|
|
|
} |
75
|
|
|
throw new FailedCreatePriceException("unknown class: $class"); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function createEnumPrice(PriceCreationDto $dto) |
79
|
|
|
{ |
80
|
|
|
return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
3 |
|
public function createRatePrice(PriceCreationDto $dto) |
84
|
|
|
{ |
85
|
|
|
return new RatePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->rate); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function createSinglePrice(PriceCreationDto $dto) |
89
|
|
|
{ |
90
|
|
|
return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|