File size: 1,405 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

declare(strict_types=1);

namespace Shared\Infrastructure\Bootstrappers;

use Application;
use Billing\Infrastructure\Currency\RateProviderCollectionInterface;
use Billing\Infrastructure\Currency\RateProviderInterface;
use Billing\Infrastructure\Currency\RateProviders\NullRateProvider;
use Easy\Container\Attributes\Inject;
use Override;
use Shared\Infrastructure\BootstrapperInterface;
use Throwable;

class PreferencesBootstrapper implements BootstrapperInterface
{
    public function __construct(
        private Application $app,

        private RateProviderCollectionInterface $rateProviderCollection,

        #[Inject('option.currency.provider')]
        private ?string $currencyProviderKey = null

    ) {
    }

    #[Override]
    public function bootstrap(): void
    {
        $this->defineRateProvider();
    }

    private function defineRateProvider(): void
    {
        $provider = null;
        $collection = $this->rateProviderCollection;

        try {
            $provider = $collection->get($this->currencyProviderKey);
        } catch (Throwable $th) {
            // Currency provider not found
        }

        if (!$provider) {
            /** @var RateProviderInterface */
            $provider = $collection->get(NullRateProvider::LOOKUP_KEY);
        }

        $this->app->set(
            RateProviderInterface::class,
            $provider
        );
    }
}