MolliePaymentGateway   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getGatewayName() 0 4 1
A __construct() 0 6 1
B chargeAmountForPayable() 0 27 1
A fetchUpdateFor() 0 15 2
1
<?php
2
3
namespace SanderVanHooft\PayableRedirect;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Mollie_API_Client;
7
use SanderVanHooft\PayableRedirect\PaymentGateway;
8
use SanderVanHooft\PayableRedirect\Payment;
9
10
class MolliePaymentGateway implements PaymentGateway
11
{
12
    private $gateway;
13
    private $response;
14
15
    public function __construct()
16
    {
17
        $this->response = false;
18
        $this->gateway = new Mollie_API_Client;
19
        $this->gateway->setApiKey(config('payable.mollie.key'));
20
    }
21
22
    /**
23
     * @param  Int
24
     * @param  Illuminate\Database\Eloquent\Model
25
     * @return App\Payment
26
     */
27
    public function chargeAmountForPayable(
28
        Int $amount,
29
        Model $payable,
30
        String $description,
31
        array $params = []
32
    ): Payment {
33
34
        $this->response = $this->gateway->payments->create(array(
35
            "amount" => number_format($amount / 100, 2),
36
            "description" => $description,
37
            "redirectUrl" => $params['return_url'],
38
            "webhookUrl"  => $params['webhook_url'],
39
        ));
40
41
        // create payment record for payable model
42
        $payment = $payable->payments()->create([
43
            'amount' => $amount,
44
            'status' => $this->response->status,
45
            'description' => $description,
46
            'redirect_url' => $this->response->links->paymentUrl,
47
            'return_url' => $this->response->links->redirectUrl,
48
            'gateway_name' => $this->getGatewayName(),
49
            'gateway_payment_reference' => $this->response->id,
50
        ]);
51
        
52
        return $payment;
53
    }
54
55
    /**
56
     * @return String
57
     */
58
    public function getGatewayName() : String
59
    {
60
        return self::class;
61
    }
62
63
    /**
64
     * @param  Payment
65
     * @return Payment
66
     */
67
    public function fetchUpdateFor(Payment $payment) : Payment
68
    {
69
        $this->response = $this->gateway->payments->get($payment->gateway_payment_reference);
0 ignored issues
show
Documentation introduced by
The property gateway_payment_reference does not exist on object<SanderVanHooft\PayableRedirect\Payment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
        
71
        $status = $this->response->status;
72
        $update = ['status' => $status];
73
74
        if ($status !== 'open') {
75
            $update['redirect_url'] = null;
76
        }
77
78
        $payment->update($update);
79
80
        return $payment;
81
    }
82
}
83