File size: 3,263 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

declare(strict_types=1);

namespace Billing\Infrastructure;

use Application;
use Billing\Infrastructure\Payments\PaymentGatewayFactoryInterface;
use Billing\Domain\Repositories\OrderRepositoryInterface;
use Billing\Domain\Repositories\PlanRepositoryInterface;
use Billing\Domain\Repositories\PlanSnapshotRepositoryInterface;
use Billing\Domain\Repositories\SubscriptionRepositoryInterface;
use Billing\Infrastructure\Currency\Exchange;
use Billing\Infrastructure\Currency\ExchangeInterface;
use Billing\Infrastructure\Currency\RateProviderCollection;
use Billing\Infrastructure\Currency\RateProviderCollectionInterface;
use Billing\Infrastructure\Currency\RateProviderInterface;
use Billing\Infrastructure\Currency\RateProviders\CurrencyApi\CurrencyApi;
use Billing\Infrastructure\Currency\RateProviders\NullRateProvider;
use Billing\Infrastructure\Payments\Gateways\PayPal\PayPal;
use Billing\Infrastructure\Payments\Gateways\Stripe\Stripe;
use Billing\Infrastructure\Payments\PaymentGatewayFactory;
use Billing\Infrastructure\Repositories\DoctrineOrm\OrderRepository;
use Billing\Infrastructure\Repositories\DoctrineOrm\PlanRepository;
use Billing\Infrastructure\Repositories\DoctrineOrm\PlanSnapshotRepository;
use Billing\Infrastructure\Repositories\DoctrineOrm\SubscriptionRepository;
use Override;
use Psr\Container\ContainerInterface;
use Shared\Infrastructure\BootstrapperInterface;

class BillingModuleBootstrapper implements BootstrapperInterface
{
    public function __construct(
        private Application $app,
        private PaymentGatewayFactory $factory,
        private ContainerInterface $container
    ) {
    }

    #[Override]
    public function bootstrap(): void
    {
        // Register repository implementations
        $this->app
            ->set(
                PlanRepositoryInterface::class,
                PlanRepository::class
            )
            ->set(
                PlanSnapshotRepositoryInterface::class,
                PlanSnapshotRepository::class
            )
            ->set(
                SubscriptionRepositoryInterface::class,
                SubscriptionRepository::class
            )
            ->set(
                OrderRepositoryInterface::class,
                OrderRepository::class
            );

        // Register payment gateway implementations
        $this->app->set(
            PaymentGatewayFactoryInterface::class,
            $this->factory
        );

        $this->registerBuiltInPaymentGateways();
        $this->registerCurrencyLayer();
    }

    private function registerBuiltInPaymentGateways(): void
    {
        $this->factory
            ->register(Stripe::LOOKUP_KEY, Stripe::class)
            ->register(PayPal::LOOKUP_KEY, PayPal::class);
    }

    private function registerCurrencyLayer(): void
    {
        $this->app->set(
            ExchangeInterface::class,
            Exchange::class
        );

        $collection = new RateProviderCollection($this->container);
        $collection
            ->add(NullRateProvider::LOOKUP_KEY, NullRateProvider::class)
            ->add(CurrencyApi::LOOKUP_KEY, CurrencyApi::class);

        $this->app->set(
            RateProviderCollectionInterface::class,
            $collection
        );
    }
}