1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/money-interop project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Money\Service; |
10
|
|
|
|
11
|
|
|
use Auryn\Injector; |
12
|
|
|
use Daikon\Boot\Service\Provisioner\ProvisionerInterface; |
13
|
|
|
use Daikon\Boot\Service\ServiceDefinitionInterface; |
14
|
|
|
use Daikon\Config\ConfigProviderInterface; |
15
|
|
|
use Daikon\Money\ValueObject\Money; |
16
|
|
|
use Money\Converter; |
17
|
|
|
use Money\Currencies; |
18
|
|
|
use Money\Currencies\AggregateCurrencies; |
19
|
|
|
use Money\Exchange; |
20
|
|
|
use Money\Exchange\FixedExchange; |
21
|
|
|
use Money\Exchange\ReversedCurrenciesExchange; |
22
|
|
|
use Money\Formatter\AggregateMoneyFormatter; |
23
|
|
|
use Money\Parser\AggregateMoneyParser; |
24
|
|
|
|
25
|
|
|
final class MoneyServiceProvisioner implements ProvisionerInterface |
26
|
|
|
{ |
27
|
|
|
public function provision( |
28
|
|
|
Injector $injector, |
29
|
|
|
ConfigProviderInterface $configProvider, |
30
|
|
|
ServiceDefinitionInterface $serviceDefinition |
31
|
|
|
): void { |
32
|
|
|
$settings = $serviceDefinition->getSettings(); |
33
|
|
|
|
34
|
|
|
$factory = function (Injector $injector) use ($settings): object { |
35
|
|
|
$currencies = $this->buildCurrencies($injector, $settings['currencies'] ?? []); |
36
|
|
|
$parsers = $this->buildParsers($injector, $currencies, $settings['parsers'] ?? []); |
37
|
|
|
$formatters = $this->buildFormatters($injector, $currencies, $settings['formatters'] ?? []); |
38
|
|
|
$exchanges = $this->buildExchanges($injector, $settings['exchanges'] ?? []); |
39
|
|
|
return new MoneyService( |
40
|
|
|
$parsers, |
41
|
|
|
new Converter($currencies, $exchanges), |
42
|
|
|
$formatters, |
43
|
|
|
$settings['type'] ?? Money::class |
44
|
|
|
); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$injector |
48
|
|
|
->share(MoneyService::class) |
49
|
|
|
->delegate(MoneyService::class, $factory); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function buildCurrencies(Injector $injector, array $currencyConfigs): AggregateCurrencies |
53
|
|
|
{ |
54
|
|
|
$currencies = []; |
55
|
|
|
foreach ($currencyConfigs as $currencyConfig) { |
56
|
|
|
$currencies[] = $injector->make($currencyConfig['class'], $currencyConfig['settings'] ?? []); |
57
|
|
|
} |
58
|
|
|
return new AggregateCurrencies($currencies); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function buildParsers( |
62
|
|
|
Injector $injector, |
63
|
|
|
Currencies $currencies, |
64
|
|
|
array $parserConfigs = [] |
65
|
|
|
): AggregateMoneyParser { |
66
|
|
|
$parsers = []; |
67
|
|
|
foreach ($parserConfigs as $parserConfig) { |
68
|
|
|
$parsers[] = $injector->make($parserConfig['class'], [':currencies' => $currencies]); |
69
|
|
|
} |
70
|
|
|
return new AggregateMoneyParser($parsers); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function buildFormatters( |
74
|
|
|
Injector $injector, |
75
|
|
|
Currencies $currencies, |
|
|
|
|
76
|
|
|
array $formatterConfigs = [] |
77
|
|
|
): AggregateMoneyFormatter { |
78
|
|
|
$formatters = []; |
79
|
|
|
foreach ($formatterConfigs as $formatterConfig) { |
80
|
|
|
$currencies = $injector->make($formatterConfig['currencies']); |
81
|
|
|
//@todo support array of currencies |
82
|
|
|
foreach ($currencies as $currency) { |
83
|
|
|
$formatters[(string)$currency] = $injector->make( |
84
|
|
|
$formatterConfig['class'], |
85
|
|
|
[':currencies' => $currencies] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return new AggregateMoneyFormatter($formatters); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function buildExchanges(Injector $injector, array $exchangeConfigs = []): Exchange |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return new ReversedCurrenciesExchange(new FixedExchange($exchangeConfigs['fixed_rate'] ?? [])); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.