Passed
Pull Request — master (#270)
by
unknown
08:58
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 1
Metric Value
eloc 1
c 1
b 1
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Shetabit\Payment\Provider;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use Shetabit\Multipay\Payment;
8
use Shetabit\Multipay\Request;
9
use Shetabit\Payment\Events\InvoicePurchasedEvent;
10
use Shetabit\Payment\Events\InvoiceVerifiedEvent;
11
12
class PaymentServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Perform post-registration booting of services.
16
     *
17
     * @return void
18
     */
19
    public function boot()
20
    {
21
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'shetabitPayment');
22
23
        /**
24
         * Configurations that needs to be done by user.
25
         */
26
        $this->publishes(
27
            [
28
                Payment::getDefaultConfigPath() => config_path('payment.php'),
29
            ],
30
            'payment-config'
31
        );
32
33
        /**
34
         * Views that needs to be modified by user.
35
         */
36
        $this->publishes(
37
            [
38
                __DIR__ . '/../../resources/views' => resource_path('views/vendor/shetabitPayment'),
39
            ],
40
            'payment-views'
41
        );
42
    }
43
44
    /**
45
     * Register any package services.
46
     *
47
     * @return void
48
     */
49
    public function register()
50
    {
51
        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

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