Completed
Push — master ( 353cb3...85206f )
by recca
04:39
created

LaravelPayumServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 122
ccs 59
cts 60
cp 0.9833
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 2
B register() 0 32 1
A provides() 0 9 1
A handleRoutes() 0 15 2
A handlePublishes() 0 16 1
1
<?php
2
3
namespace Recca0120\LaravelPayum;
4
5
use Payum\Core\Payum;
6
use Illuminate\Support\Arr;
7
use Payum\Core\PayumBuilder;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\ServiceProvider;
10
use Recca0120\LaravelPayum\Service\PayumService;
11
use Illuminate\Contracts\View\Factory as ViewFactory;
12
use Recca0120\LaravelPayum\Action\GetHttpRequestAction;
13
use Recca0120\LaravelPayum\Action\RenderTemplateAction;
14
use Recca0120\LaravelPayum\Action\ObtainCreditCardAction;
15
use Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter;
16
use Recca0120\LaravelPayum\Extension\UpdatePaymentStatusExtension;
17
18
class LaravelPayumServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * This namespace is applied to your controller routes.
22
     *
23
     * In addition, it is set as the URL generator's root namespace.
24
     *
25
     * @var string
26
     */
27
    protected $namespace = 'Recca0120\LaravelPayum\Http\Controllers';
28
29
    /**
30
     * boot.
31
     *
32
     * @param \Illuminate\Routing\Router $router
33
     * @param \Illuminate\Contracts\View\Factory $viewFactory
34
     */
35 1
    public function boot(Router $router, ViewFactory $viewFactory)
36
    {
37 1
        $viewFactory->addNamespace('payum', __DIR__.'/../resources/views');
38 1
        $this->handleRoutes($router, $this->app['config']['payum']);
39 1
        if ($this->app->runningInConsole() === true) {
40 1
            $this->handlePublishes();
41 1
        }
42 1
    }
43
44
    /**
45
     * Register the service provider.
46
     */
47 1
    public function register()
48
    {
49 1
        $this->mergeConfigFrom(__DIR__.'/../config/payum.php', 'payum');
50
51
        $this->app->singleton(PayumBuilderWrapper::class, function ($app) {
52 1
            return new PayumBuilderWrapper(new PayumBuilder, $app['config']['payum']);
53 1
        });
54
55
        $this->app->singleton(PayumBuilder::class, function ($app) {
56 1
            return $app->make(PayumBuilderWrapper::class)
57 1
                ->setTokenFactory($app['url'])
58 1
                ->setStorage($app['files'])
59 1
                ->setCoreGatewayFactoryConfig([
60 1
                    'payum.action.get_http_request' => $app->make(GetHttpRequestAction::class),
61 1
                    'payum.action.obtain_credit_card' => $app->make(ObtainCreditCardAction::class),
62 1
                    'payum.action.render_template' => $app->make(RenderTemplateAction::class),
63 1
                    'payum.converter.reply_to_http_response' => $app->make(ReplyToSymfonyResponseConverter::class),
64 1
                    'payum.extension.update_payment_status' => $app->make(UpdatePaymentStatusExtension::class),
65 1
                ])
66 1
                ->setHttpRequestVerifier()
67 1
                ->setCoreGatewayFactory()
68 1
                ->setGenericTokenFactoryPaths()
69 1
                ->setGatewayConfig()
70 1
                ->getBuilder();
71 1
        });
72
73
        $this->app->singleton(Payum::class, function ($app) {
74 1
            return $app->make(PayumBuilder::class)->getPayum();
75 1
        });
76
77 1
        $this->app->singleton(PayumService::class, PayumService::class);
78 1
    }
79
80
    /**
81
     * provides.
82
     *
83
     * @return array
84
     */
85 1
    public function provides()
86
    {
87
        return [
88 1
            PayumBuilderWrapper::class,
89 1
            PayumBuilder::class,
90 1
            Payum::class,
91 1
            PayumService::class,
92 1
        ];
93
    }
94
95
    /**
96
     * register routes.
97
     *
98
     * @param \Illuminate\Routing\Router $router
99
     * @param array $config
100
     * @return $this
101
     */
102 1
    protected function handleRoutes(Router $router, $config = [])
103
    {
104 1
        if ($this->app->routesAreCached() === false) {
105 1
            $router->group(array_merge([
106 1
                'prefix' => 'payment',
107 1
                'as' => 'payment.',
108 1
                'namespace' => $this->namespace,
109 1
                'middleware' => ['web'],
110 1
            ], Arr::get($config, 'route', [])), function (Router $router) {
111
                require __DIR__.'/Http/routes.php';
112 1
            });
113 1
        }
114
115 1
        return $this;
116
    }
117
118
    /**
119
     * handle publishes.
120
     *
121
     * @return $this
122
     */
123 1
    protected function handlePublishes()
124
    {
125 1
        $this->publishes([
126 1
            __DIR__.'/../config/payum.php' => $this->app->configPath().'/payum.php',
127 1
        ], 'config');
128
129 1
        $this->publishes([
130 1
            __DIR__.'/../resources/views' => $this->app->basePath().'/resources/views/vendor/payum/',
131 1
        ], 'views');
132
133 1
        $this->publishes([
134 1
            __DIR__.'/../database/migrations' => $this->app->databasePath().'/migrations/',
135 1
        ], 'public');
136
137 1
        return $this;
138
    }
139
}
140