Passed
Push — master ( e689f4...c0f464 )
by Gabriel
12:33
created

PurchaseSessionsTrait::decodeParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
namespace ByTIC\Payments\Models\PurchaseSessions;
4
5
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits\CompletePurchaseResponseTrait;
6
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
7
use ByTIC\Payments\Models\PurchaseSessions\Traits\Cleanup\RecordsTrait as CleanupRecordsTrait;
8
use ByTIC\Payments\Utility\PaymentsModels;
9
use Omnipay\Common\Message\ResponseInterface;
10
11
/**
12
 * Trait PurchaseSessionsTrait
13
 * @package ByTIC\Payments\Models\PurchaseSessions
14
 *
15
 * @method PurchaseSessionTrait getNew
16
 */
17
trait PurchaseSessionsTrait
18
{
19
    use CleanupRecordsTrait;
20
21
    /**
22
     * @param string $type
23
     * @param CompletePurchaseResponseTrait|ResponseInterface $response
24
     * @return PurchaseSessionTrait
25
     */
26
    public function createFromResponse($response, $type)
27
    {
28
        /** @var IsPurchasableModelTrait $payment */
29
        $payment = $response->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Omnipay\Common\Message\ResponseInterface. Did you maybe mean getCode()? ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $payment = $response->getModel();

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...
30
        $session = $this->generateFromPurchaseType($payment, $type);
31
        $session->populateFromResponse($response);
32
        $session->insert();
33
34
        return $session;
35
    }
36
37
    /**
38
     * @param string $type
39
     * @param IsPurchasableModelTrait $payment
40
     * @return PurchaseSessionTrait
41
     */
42
    public function createFromPurchase($payment, $type)
43
    {
44
        $session = $this->generateFromPurchaseType($payment, $type);
45
        $session->insert();
46
47
        return $session;
48
    }
49
50
    /**
51
     * @param $params
52
     * @return mixed
53
     */
54
    public static function decodeParams($params)
55
    {
56
        return unserialize(gzuncompress(base64_decode($params)));
57
    }
58
59
    protected function initRelations()
60
    {
61
        parent::initRelations();
62
    }
63
64
    protected function initRelationsCommon()
65
    {
66
        $this->initRelationsPurchase();
67
    }
68
69
    protected function initRelationsPurchase()
70
    {
71
        $this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
0 ignored issues
show
Bug introduced by
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

71
        $this->/** @scrutinizer ignore-call */ 
72
               belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
Loading history...
72
    }
73
74
    /**
75
     * @param array $params
76
     */
77
    protected function injectParams(&$params = [])
78
    {
79
        $params['order'][] = ['created', 'desc'];
80
81
        parent::injectParams($params);
82
    }
83
84
    /**
85
     * @param IsPurchasableModelTrait $payment
86
     * @param string $type
87
     * @return PurchaseSessionTrait
88
     */
89
    protected function generateFromPurchaseType($payment, $type)
90
    {
91
        $session = $this->generateFromPurchase($payment);
92
        $session->type = $type;
93
        return $session;
94
    }
95
96
    /**
97
     * @param IsPurchasableModelTrait $payment
98
     * @return PurchaseSessionTrait
99
     */
100
    protected function generateFromPurchase($payment)
101
    {
102
        $session = $this->getNew();
103
        $session->populateFromPayment($payment);
104
        $session->populateFromGateway($payment->getPaymentMethod()->getType()->getGateway());
0 ignored issues
show
Bug introduced by
The method getGateway() does not exist on ByTIC\Payments\Models\Methods\Types\AbstractType. It seems like you code against a sub-type of ByTIC\Payments\Models\Methods\Types\AbstractType such as ByTIC\Payments\Models\Methods\Types\CreditCards. ( Ignorable by Annotation )

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

104
        $session->populateFromGateway($payment->getPaymentMethod()->getType()->/** @scrutinizer ignore-call */ getGateway());
Loading history...
105
        $session->populateFromRequest();
106
107
        return $session;
108
    }
109
110
    /**
111
     * @return mixed|\Nip\Config\Config
112
     * @throws \Exception
113
     */
114
    protected function generateTable()
115
    {
116
        return config('payments.tables.purchases_sessions', 'purchases_sessions');
117
    }
118
}
119