Issues (4)

src/Service/MoneyServiceProvisioner.php (1 issue)

Severity
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\Currencies\CurrencyList;
20
use Money\Exchange;
21
use Money\Exchange\FixedExchange;
22
use Money\Exchange\ReversedCurrenciesExchange;
23
use Money\Formatter\AggregateMoneyFormatter;
24
use Money\Parser\AggregateMoneyParser;
25
26
final class MoneyServiceProvisioner implements ProvisionerInterface
27
{
28
    public function provision(
29
        Injector $injector,
30
        ConfigProviderInterface $configProvider,
31
        ServiceDefinitionInterface $serviceDefinition
32
    ): void {
33
        $serviceClass = $serviceDefinition->getServiceClass();
34
        $settings = $serviceDefinition->getSettings();
35
36
        $factory = function (Injector $injector) use ($settings, $serviceClass): object {
37
            $currencies = $this->buildCurrencies($injector, $settings['currencies'] ?? []);
38
            $parsers = $this->buildParsers($injector, $currencies, $settings['parsers'] ?? []);
39
            $formatters = $this->buildFormatters($injector, $settings['formatters'] ?? []);
40
            $exchanges = $this->buildExchanges($injector, $settings['exchanges'] ?? []);
41
            return new $serviceClass(
42
                $parsers,
43
                new Converter($currencies, $exchanges),
44
                $formatters,
45
                $settings['type'] ?? Money::class
46
            );
47
        };
48
49
        $injector
50
            ->delegate($serviceClass, $factory)
51
            ->share($serviceClass)
52
            ->alias(MoneyServiceInterface::class, $serviceClass);
53
    }
54
55
    private function buildCurrencies(Injector $injector, array $currencyConfigs): AggregateCurrencies
56
    {
57
        $currencies = [];
58
        foreach ($currencyConfigs as $currencyConfig) {
59
            $currencies[] = $injector->make($currencyConfig['class'], $currencyConfig['settings'] ?? []);
60
        }
61
        return new AggregateCurrencies($currencies);
62
    }
63
64
    private function buildParsers(
65
        Injector $injector,
66
        Currencies $currencies,
67
        array $parserConfigs = []
68
    ): AggregateMoneyParser {
69
        $parsers = [];
70
        foreach ($parserConfigs as $parserConfig) {
71
            $parsers[] = $injector->make($parserConfig['class'], [':currencies' => $currencies]);
72
        }
73
        return new AggregateMoneyParser($parsers);
74
    }
75
76
    private function buildFormatters(Injector $injector, array $formatterConfigs = []): AggregateMoneyFormatter
77
    {
78
        $formatters = [];
79
        foreach ($formatterConfigs as $formatterConfig) {
80
            if (is_array($formatterConfig['currencies'])) {
81
                $currencies = new CurrencyList($formatterConfig['currencies']);
82
            } else {
83
                $currencies = $injector->make($formatterConfig['currencies']);
84
            }
85
            foreach ($currencies as $currency) {
86
                $formatters[(string)$currency] = $injector->make(
87
                    $formatterConfig['class'],
88
                    [':currencies' => $currencies]
89
                );
90
            }
91
        }
92
        return new AggregateMoneyFormatter($formatters);
93
    }
94
95
    private function buildExchanges(Injector $injector, array $exchangeConfigs = []): Exchange
0 ignored issues
show
The parameter $injector is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
    private function buildExchanges(/** @scrutinizer ignore-unused */ Injector $injector, array $exchangeConfigs = []): Exchange

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        //@todo more exchange service support
98
        return new ReversedCurrenciesExchange(new FixedExchange($exchangeConfigs['fixed_rate'] ?? []));
99
    }
100
}
101