Completed
Pull Request — master (#5)
by Stephen
08:23
created

PaystackBladeDirective::paystackEmbededCheckout()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0
cc 2
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stephenjude\PaystackLite\Blade;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\Facades\Config;
7
8
use function GuzzleHttp\json_encode;
9
10
class PaystackBladeDirective
11
{
12
    // Build your next great package.
13
14
    public static function register()
15
    {
16
        self::paystackPopupCheckout();
17
        self::paystackEmbededCheckout();
18
    }
19
20
    private static function paystackPopupCheckout()
21
    {
22
        Blade::directive('paystack', function () {
23
24
            $public_key = "\'" . Config::get('paystack-lite.public_key') . "\'";
25
            $paystack_js = '<script src="' . Config::get('paystack-lite.paystack_inline_js') . '"></script>';
26
            $script_open = "<script>";
27
            $script_closed = "</script>";
28
29
            return "<?php
30
            echo '
31
            $paystack_js
32
33
            $script_open
34
            function payWithPaystack(amount, email, meta, callback, onclose) {
35
               var meta_data = meta ? meta : {};
36
                var options = {
37
                    key: $public_key,
38
                    email: email,
39
                    amount: amount+\'00\',
40
                    metadata: meta,
41
                    callback: function(response){
42
                        callback(response);
43
                    },
44
                    onClose:function(){
45
                        if(onclose){
46
                            onclose();
47
                        }
48
                    }
49
                };
50
51
                var handler = PaystackPop.setup(options);
52
                handler.openIframe();
53
            }
54
55
            $script_closed
56
            ';
57
            ?>";
58
        });
59
    }
60
61
62
    private static function paystackEmbededCheckout()
63
    {
64
65
66
        Blade::directive('paystackEmbeded', function ($expression) {
67
68
            /**
69
             * @param $expression  contains 4 parameters
70
             * @param $params[0] amount
71
             * @param $params[1] callback
72
             * @param $params[2] email
73
             * @param $params[3] meta
74
             *
75
             */
76
77
            eval("\$params = [$expression];");
78
79
            $amount = "\'" . $params[0] . "\'";
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
80
            $callback =  $params[1];
81
            $email =  $params[2];
82
            $email = "\'" . $email . "\'";
83
            $meta = isset($params[3]) ? $params[3] : '{}';
84
85
            $public_key = "\'" . Config::get('paystack-lite.public_key') . "\'";
86
            $paystack_js = '<script src="' . Config::get('paystack-lite.paystack_inline_js') . '"></script>';
87
            $script_open = "<script>";
88
            $script_closed = "</script>";
89
90
            return "<?php
91
92
            echo ' $paystack_js
93
94
            <div id=\"paystackEmbedContainer\"></div>
95
            $script_open
96
97
                var options = {
98
                    key: $public_key,
99
                    email: $email,
100
                    amount: $amount+\'00\',
101
                    metadata:  $meta,
102
                    container: \'paystackEmbedContainer\',
103
                    callback: function(response){
104
                        $callback(response);
105
                    }
106
                };
107
108
                var handler = PaystackPop.setup(options);
109
                handler.openIframe();
110
111
            $script_closed
112
            ';
113
            ?>";
114
        });
115
    }
116
}
117