Passed
Push — master ( b40bc2...29ef41 )
by Mr
07:40
created

MoneyServiceProvisioner::buildFormatters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 17
ccs 0
cts 16
cp 0
crap 12
rs 10
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,
0 ignored issues
show
Unused Code introduced by
The parameter $currencies 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

75
        /** @scrutinizer ignore-unused */ Currencies $currencies,

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...
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
0 ignored issues
show
Unused Code introduced by
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

92
    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...
93
    {
94
        return new ReversedCurrenciesExchange(new FixedExchange($exchangeConfigs['fixed_rate'] ?? []));
95
    }
96
}
97