Passed
Push — master ( 96d33b...43186b )
by Gabriel
13:27
created

CreateOrUpdateTokenFromResponse::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.8666
1
<?php
2
3
namespace ByTIC\Payments\Actions\GatewayNotifications;
4
5
use ByTIC\Omnipay\Common\Models\TokenInterface;
6
use ByTIC\Payments\Utility\PaymentsModels;
7
8
/**
9
 * Class CreateOrUpdateTokenFromResponse
10
 * @package ByTIC\Payments\Actions\GatewayNotifications
11
 * @internal
12
 */
13
class CreateOrUpdateTokenFromResponse
14
{
15
    /**
16
     * @param NotificationData $notification
17
     * @return \ByTIC\Payments\Models\Transactions\TransactionTrait|\Nip\Records\AbstractModels\Record
18
     */
19
    public static function handle(NotificationData $notification)
20
    {
21
        if (!method_exists($notification->response, 'getToken')) {
22
            return null;
23
        }
24
        $token = $notification->response->getToken();
25
        if (!($token instanceof TokenInterface)) {
26
            return null;
27
        }
28
        if (empty($token->getId())) {
29
            return null;
30
        }
31
32
        $notification->token = PaymentsModels::tokens()
33
            ->findOrCreateForMethod($notification->purchase->getPaymentMethod(), $token);
34
        $notification->token->populateFromCustomer($notification->purchase->getPurchaseBillingRecord());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $notification->purchase-...PurchaseBillingRecord() targeting ByTIC\Payments\Models\Pu...PurchaseBillingRecord() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the usage of $notification->purchase-...PurchaseBillingRecord() targeting ByTIC\Payments\Models\Pu...PurchaseBillingRecord() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
        $notification->token->update();
0 ignored issues
show
Bug introduced by
The method update() does not exist on ByTIC\Payments\Models\Tokens\TokenTrait. Did you maybe mean updatedTimestamps()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $notification->token->/** @scrutinizer ignore-call */ 
36
                              update();

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...
Bug introduced by
The method update() does not exist on ByTIC\Payments\Models\Tokens\TokenTrait. Did you maybe mean updatedTimestamps()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $notification->token->/** @scrutinizer ignore-call */ 
36
                              update();

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...
36
37
        return $notification->token;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $notification->token also could return the type ByTIC\Payments\Models\Tokens\TokenTrait which is incompatible with the documented return type ByTIC\Payments\Models\Tr...s\AbstractModels\Record.
Loading history...
Bug Best Practice introduced by
The expression return $notification->token also could return the type ByTIC\Payments\Models\Tokens\TokenTrait which is incompatible with the documented return type ByTIC\Payments\Models\Tr...s\AbstractModels\Record.
Loading history...
38
    }
39
}