Completed
Push — master ( 39f1f0...d5595a )
by Orkhan
03:51
created

GoldenpayController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A checkPaymentResult() 0 6 1
A fireEvent() 0 16 2
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\Http\Controllers;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Http\Request;
9
use Orkhanahmadov\LaravelGoldenpay\Goldenpay;
10
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
11
12
abstract class GoldenpayController
13
{
14
    /**
15
     * @var Application
16
     */
17
    protected $application;
18
    /**
19
     * @var Goldenpay
20
     */
21
    protected $goldenpay;
22
    /**
23
     * @var Dispatcher
24
     */
25
    protected $dispatcher;
26
    /**
27
     * @var Repository
28
     */
29
    protected $config;
30
    /**
31
     * @var Payment
32
     */
33
    protected $payment;
34
35
    /**
36
     * Controller constructor.
37
     *
38
     * @param Application $application
39
     * @param Repository $config
40
     * @param Request $request
41
     * @param Goldenpay $goldenpay
42
     * @param Dispatcher $dispatcher
43
     */
44
    public function __construct(
45
        Application $application,
46
        Repository $config,
47
        Request $request,
48
        Goldenpay $goldenpay,
49
        Dispatcher $dispatcher
50
    ) {
51
        $this->application = $application;
52
        $this->config = $config;
53
        $this->goldenpay = $goldenpay;
54
        $this->dispatcher = $dispatcher;
55
56
        $this->checkPaymentResult($request);
57
    }
58
59
    /**
60
     * Checks payment result with "payment_key" query parameter.
61
     *
62
     * @param Request $request
63
     */
64
    final protected function checkPaymentResult(Request $request): void
65
    {
66
        $this->payment = $this->goldenpay->paymentResult($request->query('payment_key'));
0 ignored issues
show
Bug introduced by
It seems like $request->query('payment_key') targeting Illuminate\Http\Concerns...ractsWithInput::query() can also be of type array or null; however, Orkhanahmadov\LaravelGol...denpay::paymentResult() does only seem to accept object<Orkhanahmadov\Lar...\Models\Payment>|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
68
        $this->fireEvent($this->payment);
69
    }
70
71
    /**
72
     * Fires event based on payment status.
73
     *
74
     * @param Payment $payment
75
     */
76
    private function fireEvent(Payment $payment): void
77
    {
78
        if ($payment->status === 1) {
79
            $event = $this->application->make(
80
                $this->config->get('goldenpay.events.payment_successful'),
81
                [$payment]
82
            );
83
        } else {
84
            $event = $this->application->make(
85
                $this->config->get('goldenpay.events.payment_failed'),
86
                [$payment]
87
            );
88
        }
89
90
        $this->dispatcher->dispatch($event);
91
    }
92
}
93