Completed
Push — master ( d5595a...9bb41e )
by Orkhan
08:19
created

GoldenpayController::paymentSuccessful()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Orkhanahmadov\LaravelGoldenpay\Events\PaymentSuccessfulEvent;
9
use Orkhanahmadov\LaravelGoldenpay\Goldenpay;
10
use Orkhanahmadov\LaravelGoldenpay\Http\Requests\Request;
11
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
12
13
abstract class GoldenpayController
14
{
15
    /**
16
     * @var Repository
17
     */
18
    protected $config;
19
    /**
20
     * @var Dispatcher
21
     */
22
    protected $dispatcher;
23
    /**
24
     * @var Goldenpay
25
     */
26
    protected $goldenpay;
27
    /**
28
     * @var Payment
29
     */
30
    protected $payment;
31
32
    /**
33
     * Controller constructor.
34
     *
35
     * @param Repository $config
36
     * @param Request $request
37
     * @param Dispatcher $dispatcher
38
     * @param Goldenpay $goldenpay
39
     */
40
    public function __construct(
41
        Repository $config,
42
        Request $request,
43
        Dispatcher $dispatcher,
44
        Goldenpay $goldenpay
45
    ) {
46
        $this->config = $config;
47
        $this->dispatcher = $dispatcher;
48
        $this->goldenpay = $goldenpay;
49
50
        $this->checkPaymentResult($request);
51
    }
52
53
    /**
54
     * Checks payment result with "payment_key" query parameter.
55
     *
56
     * @param Request $request
57
     */
58
    final protected function checkPaymentResult(Request $request): void
59
    {
60
        $this->payment = $this->goldenpay->result($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\LaravelGoldenpay\Goldenpay::result() 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...
61
62
        $this->fireEvent();
63
    }
64
65
    /**
66
     * Fires event based on payment status.
67
     */
68
    private function fireEvent(): void
69
    {
70
        if ($this->paymentSuccessful()) {
71
            $event = $this->config->get('goldenpay.events.payment_successful');
72
        } else {
73
            $event = $this->config->get('goldenpay.events.payment_failed');
74
        }
75
76
        $this->dispatcher->dispatch(new $event($this->payment));
77
    }
78
79
    /**
80
     * Returns state if payment was successful.
81
     *
82
     * @return bool
83
     */
84
    protected function paymentSuccessful(): bool
85
    {
86
        return $this->payment->successful;
87
    }
88
}
89