Passed
Push — master ( 43186b...d45fb7 )
by Gabriel
14:32
created

updateTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Actions\GatewayNotifications;
4
5
use ByTIC\Omnipay\Librapay\Message\ServerCompletePurchaseResponse;
6
use ByTIC\Payments\Utility\PaymentsModels;
7
use Omnipay\Common\Message\AbstractResponse;
8
9
/**
10
 * Class CreateOrUpdateTransactionFromResponse
11
 * @package ByTIC\Payments\Actions\GatewayNotifications
12
 * @internal
13
 */
14
class CreateOrUpdateTransactionFromResponse
15
{
16
    /**
17
     * @param $response
18
     * @param $model
19
     * @param $type
20
     * @return \ByTIC\Payments\Models\Transactions\TransactionTrait|\Nip\Records\AbstractModels\Record
21
     */
22
    public static function handle(NotificationData $notification)
23
    {
24
        $notification->transaction = PaymentsModels::transactions()->findOrCreateForPurchase($notification->purchase);
25
26
        static::updateFromResponse($notification->response, $notification->transaction);
0 ignored issues
show
Bug introduced by
It seems like $notification->response can also be of type ByTIC\Payments\Gateways\...sModelProcessedResponse; however, parameter $response of ByTIC\Payments\Actions\G...e::updateFromResponse() does only seem to accept Omnipay\Common\Message\AbstractResponse, maybe add an additional type check? ( Ignorable by Annotation )

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

26
        static::updateFromResponse(/** @scrutinizer ignore-type */ $notification->response, $notification->transaction);
Loading history...
27
        $notification->transaction->status = $notification->purchase->getStatus();
28
29
        $notification->transaction->update();
30
31
        return $notification->transaction;
32
    }
33
34
35
    /**
36
     * @param AbstractResponse|ServerCompletePurchaseResponse $response
37
     * @param $transaction
38
     */
39
    protected static function updateFromResponse(AbstractResponse $response, $transaction)
40
    {
41
        static::setPropertyFromResponse($response, $transaction, 'getCode', 'code');
42
        static::setPropertyFromResponse($response, $transaction, 'getTransactionReference', 'reference');
43
        static::setPropertyFromResponse($response, $transaction, 'getCardMasked', 'card');
44
    }
45
46
    /**
47
     * @param $response
48
     * @param $transaction
49
     * @param $method
50
     * @param $property
51
     */
52
    protected static function setPropertyFromResponse($response, $transaction, $method, $property)
53
    {
54
        if (!method_exists($response, $method)) {
55
            return;
56
        }
57
        $value = $response->{$method}();
58
        if ($value === null || $value === '') {
59
            return;
60
        }
61
        $transaction->{$property} = $value;
62
    }
63
}