Passed
Push — master ( 299e2d...869df1 )
by mahdi
02:49
created

existCustomRedirectFormView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Shetabit\Payment\Provider;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Support\Facades\Blade;
8
use Illuminate\Support\ServiceProvider;
9
use Illuminate\View\View;
10
use Shetabit\Multipay\Payment;
11
use Shetabit\Multipay\Request;
12
use Shetabit\Payment\Events\InvoicePurchasedEvent;
13
use Shetabit\Payment\Events\InvoiceVerifiedEvent;
14
15
class PaymentServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Perform post-registration booting of services.
19
     *
20
     * @return void
21
     */
22
    public function boot()
23
    {
24
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'shetabitPayment');
25
26
        /**
27
         * Configurations that needs to be done by user.
28
         */
29
        $this->publishes(
30
            [
31
                Payment::getDefaultConfigPath() => config_path('payment.php'),
32
            ],
33
            'payment-config'
34
        );
35
36
        /**
37
         * Views that needs to be modified by user.
38
         */
39
        $this->publishes(
40
            [
41
                __DIR__ . '/../../resources/views' => resource_path('views/vendor/shetabitPayment'),
42
            ],
43
            'payment-views'
44
        );
45
    }
46
47
    /**
48
     * Register any package services.
49
     *
50
     * @return void
51
     */
52
    public function register()
53
    {
54
        Request::overwrite('input', function ($key) {
0 ignored issues
show
Bug introduced by
The method overwrite() does not exist on Shetabit\Multipay\Request. ( Ignorable by Annotation )

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

54
        Request::/** @scrutinizer ignore-call */ 
55
                 overwrite('input', function ($key) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            return \request($key);
56
        });
57
58
        /**
59
         * Bind to service container.
60
         */
61
        $this->app->bind('shetabit-payment', function () {
62
            $config = config('payment') ?? [];
63
64
            return new Payment($config);
65
        });
66
67
        $this->registerEvents();
68
69
        // use blade to render redirection form
70
        Payment::setRedirectionFormViewRenderer(function ($view, $action, $inputs, $method) {
71
            if ($this->existCustomRedirectFormView()) {
72
                return $this->loadNormalRedirectForm($action, $inputs, $method);
73
            }
74
            return Blade::render(
75
                str_replace('</form>', '@csrf</form>', file_get_contents($view)),
76
                [
77
                    'action' => $action,
78
                    'inputs' => $inputs,
79
                    'method' => $method,
80
                ]
81
            );
82
        });
83
    }
84
85
    /**
86
     * Register Laravel events.
87
     *
88
     * @return void
89
     */
90
    public function registerEvents()
91
    {
92
        Payment::addPurchaseListener(function ($driver, $invoice) {
93
            event(new InvoicePurchasedEvent($driver, $invoice));
94
        });
95
96
        Payment::addVerifyListener(function ($reciept, $driver, $invoice) {
97
            event(new InvoiceVerifiedEvent($reciept, $driver, $invoice));
98
        });
99
    }
100
101
    /**
102
     * Checks whether the user has customized the view file called `redirectForm.blade.php` or not
103
     *
104
     * @return bool
105
     */
106
    private function existCustomRedirectFormView()
107
    {
108
        return file_exists(resource_path('views/vendor/shetabitPayment') . '/redirectForm.blade.php');
109
    }
110
111
    /**
112
     * @param $action
113
     * @param $inputs
114
     * @param $method
115
     * @return Application|Factory|View
116
     */
117
    private function loadNormalRedirectForm($action, $inputs, $method)
118
    {
119
        return view('shetabitPayment::redirectForm')->with(
120
            [
121
                'action' => $action,
122
                'inputs' => $inputs,
123
                'method' => $method,
124
            ]
125
        );
126
    }
127
}
128