Passed
Push — master ( 133129...87687b )
by Gabriel
12:43
created

TokensTrait::createForMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Models\Tokens;
4
5
use ByTIC\Omnipay\Common\Models\TokenInterface;
6
use ByTIC\Payments\Utility\PaymentsModels;
7
use Nip\Records\AbstractModels\Record;
8
9
/**
10
 * Trait TokensTrait
11
 * @package ByTIC\Payments\Models\Tokens
12
 *
13
 * @method TokenTrait|Token getNew
14
 */
15
trait TokensTrait
16
{
17
18
    /**
19
     * @param $method
20
     * @param TokenInterface $token
21
     * @return TokenTrait|Record
22
     */
23
    public function findOrCreateForMethod($method, TokenInterface $token)
24
    {
25
        $findToken = $this->findOneByParams(
0 ignored issues
show
Bug introduced by
It seems like findOneByParams() 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

25
        /** @scrutinizer ignore-call */ 
26
        $findToken = $this->findOneByParams(
Loading history...
26
            [
27
                'where' => [
28
                    ['id_method =?', is_object($method) ? $method->id : $method],
29
                    ['token_id = ?', $token->getId()],
30
                ]
31
            ]);
32
33
        if ($findToken instanceof Record) {
34
            return $findToken;
35
        }
36
        $tokenRecord = $this->getNew();
37
        $tokenRecord->populateFromPaymentMethod($method);
38
        $tokenRecord->populateFromToken($token);
39
        $tokenRecord->insert();
0 ignored issues
show
Bug introduced by
It seems like insert() 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

39
        $tokenRecord->/** @scrutinizer ignore-call */ 
40
                      insert();
Loading history...
40
41
        return $this->createForMethod($method, $token);
42
    }
43
44
    /**
45
     * @param $method
46
     * @param TokenInterface $token
47
     * @return Token|TokenTrait
48
     */
49
    protected function createForMethod($method, TokenInterface $token)
50
    {
51
        $item = $this->getNew();
52
        $item->populateFromPaymentMethod($method);
53
        $item->populateFromGateway($method->getType()->getGateway());
54
        $item->populateFromToken($token);
55
        $item->insert();
56
        return $item;
57
    }
58
59
    protected function initRelations()
60
    {
61
        parent::initRelations();
62
    }
63
64
    protected function initRelationsCommon()
65
    {
66
        $this->initRelationsPurchase();
67
        $this->initRelationsPaymentMethod();
68
    }
69
70
    protected function initRelationsPurchase()
71
    {
72
        $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

72
        $this->/** @scrutinizer ignore-call */ 
73
               belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
Loading history...
73
    }
74
75
    protected function initRelationsPaymentMethod()
76
    {
77
        $this->belongsTo('PaymentMethod', ['class' => get_class(PaymentsModels::methods())]);
78
    }
79
80
    /**
81
     * @param array $params
82
     */
83
    protected function injectParams(&$params = [])
84
    {
85
        $params['order'][] = ['created', 'desc'];
86
87
        parent::injectParams($params);
88
    }
89
90
91
    /**
92
     * @return mixed|\Nip\Config\Config
93
     * @throws \Exception
94
     */
95
    protected function generateTable()
96
    {
97
        return config('payments.tables.tokens', Tokens::TABLE);
98
    }
99
}
100