Completed
Push — master ( 9c6e80...133129 )
by Gabriel
04:00
created

setPropertyFromResponse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 10
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
 */
13
class CreateOrUpdateTransactionFromResponse
14
{
15
    /**
16
     * @param $response
17
     * @param $model
18
     * @param $type
19
     * @return \ByTIC\Payments\Models\Transactions\TransactionTrait|\Nip\Records\AbstractModels\Record
20
     */
21
    public static function handle($response, $model, $type)
22
    {
23
        $transaction = PaymentsModels::transactions()->findOrCreateForPurchase($model);
24
        static::updateTransaction($response, $transaction);
25
        return $transaction;
26
    }
27
28
29
    /**
30
     * @param AbstractResponse|ServerCompletePurchaseResponse $response
31
     * @param $transaction
32
     */
33
    protected static function updateTransaction(AbstractResponse $response, $transaction)
34
    {
35
        static::setPropertyFromResponse($response, $transaction, 'getCode', 'code');
36
        static::setPropertyFromResponse($response, $transaction, 'getTransactionReference', 'reference');
37
        static::setPropertyFromResponse($response, $transaction, 'getCardMasked', 'card');
38
39
        $transaction->update();
40
    }
41
42
    /**
43
     * @param $response
44
     * @param $transaction
45
     * @param $method
46
     * @param $property
47
     */
48
    protected static function setPropertyFromResponse($response, $transaction, $method, $property)
49
    {
50
        if (!method_exists($response, $method)) {
51
            return;
52
        }
53
        $value = $response->{$method}();
54
        if ($value === null || $value === '') {
55
            return;
56
        }
57
        $transaction->{$property} = $value;
58
    }
59
}