|
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
|
|
|
ProgressivePrice::class => 'createProgressivePrice', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
protected $types = [ |
|
28
|
|
|
'enum' => EnumPrice::class, |
|
29
|
|
|
'single' => SinglePrice::class, |
|
30
|
|
|
'progressive' => ProgressivePrice::class, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string default price class, when given will be used for not found types |
|
35
|
16 |
|
*/ |
|
36
|
|
|
protected $defaultClass = null; |
|
37
|
16 |
|
|
|
38
|
16 |
|
public function __construct(array $types = [], $defaultClass = null) |
|
39
|
16 |
|
{ |
|
40
|
|
|
$this->types = $types; |
|
41
|
|
|
$this->defaultClass = $defaultClass; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
|
|
45
|
|
|
/** |
|
46
|
4 |
|
* Creates price object. |
|
47
|
4 |
|
*/ |
|
48
|
4 |
|
public function create(PriceCreationDto $dto): PriceInterface |
|
49
|
|
|
{ |
|
50
|
4 |
|
$class = $this->findClassForTypes([ |
|
51
|
|
|
get_class($dto), |
|
52
|
4 |
|
$dto->type->getName(), |
|
53
|
|
|
]); |
|
54
|
|
|
$method = $this->findMethodForClass($class); |
|
55
|
4 |
|
|
|
56
|
|
|
return $this->{$method}($dto); |
|
57
|
4 |
|
} |
|
58
|
4 |
|
|
|
59
|
4 |
|
public function findClassForTypes(array $types) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($types as $type) { |
|
62
|
2 |
|
if (isset($this->types[$type])) { |
|
63
|
2 |
|
return $this->types[$type]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
if ($this->defaultClass) { |
|
67
|
|
|
return $this->defaultClass; |
|
68
|
4 |
|
} |
|
69
|
|
|
throw new FailedCreatePriceException(sprintf('unknown types: "%s"', implode(',', $types))); |
|
70
|
4 |
|
} |
|
71
|
4 |
|
|
|
72
|
|
|
public function findMethodForClass($class) |
|
73
|
|
|
{ |
|
74
|
|
|
if (isset($this->creators[$class])) { |
|
75
|
|
|
return $this->creators[$class]; |
|
76
|
1 |
|
} |
|
77
|
|
|
throw new FailedCreatePriceException("unknown class: $class"); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function createEnumPrice(PriceCreationDto $dto) |
|
81
|
3 |
|
{ |
|
82
|
|
|
return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function createRatePrice(PriceCreationDto $dto) |
|
86
|
|
|
{ |
|
87
|
|
|
return new RatePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->rate); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function createSinglePrice(PriceCreationDto $dto) |
|
91
|
|
|
{ |
|
92
|
|
|
return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function createProgressivePrice(PriceCreationDto $dto): ProgressivePrice |
|
96
|
|
|
{ |
|
97
|
|
|
return new ProgressivePrice($dto->id, $dto->type, $dto->target, $dto->prepaid, $dto->thresholds, $dto->plan); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|