Passed
Push — master ( ba21e2...5468d0 )
by Gabriel
15:15
created

TokensTrait::initRelationsTransactions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

29
        /** @scrutinizer ignore-call */ 
30
        $findToken = $this->findOneByParams(
Loading history...
30
            [
31
                'where' => [
32
                    ['id_method =?', is_object($method) ? $method->id : $method],
33
                    ['token_id = ?', $token->getId()],
34
                ]
35
            ]
36
        );
37
38
        if ($findToken instanceof Record) {
39
            return $findToken;
40
        }
41
42
        return $this->createForMethod($method, $token);
43
    }
44
45
    /**
46
     * @param $method
47
     * @param TokenInterface $token
48
     * @return Token|TokenTrait
49
     */
50
    protected function createForMethod($method, TokenInterface $token)
51
    {
52
        $item = $this->getNew();
53
        $item->populateFromPaymentMethod($method);
54
        $item->populateFromGateway($method->getType()->getGateway());
55
        $item->populateFromToken($token);
56
        $item->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

56
        $item->/** @scrutinizer ignore-call */ 
57
               insert();
Loading history...
57
        return $item;
58
    }
59
60
    protected function initRelations()
61
    {
62
        parent::initRelations();
63
        $this->initRelationsCommon();
64
    }
65
66
    protected function initRelationsCommon()
67
    {
68
        $this->initRelationsTransactions();
69
        $this->initRelationsPaymentMethod();
70
        $this->initRelationsCustomer();
71
    }
72
73
    protected function initRelationsTransactions()
74
    {
75
        $this->hasMany('Transactions', ['class' => get_class(PaymentsModels::transactions()), 'fk' => 'id_token']);
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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

75
        $this->/** @scrutinizer ignore-call */ 
76
               hasMany('Transactions', ['class' => get_class(PaymentsModels::transactions()), 'fk' => 'id_token']);
Loading history...
76
    }
77
78
    /**
79
     * @param array $params
80
     */
81
    protected function injectParams(&$params = [])
82
    {
83
        $params['order'][] = ['created', 'desc'];
84
85
        parent::injectParams($params);
86
    }
87
88
    /**
89
     * @return mixed|\Nip\Config\Config
90
     * @throws \Exception
91
     */
92
    protected function generateTable()
93
    {
94
        return config('payments.tables.tokens', Tokens::TABLE);
95
    }
96
97
    /**
98
     * @return mixed|\Nip\Config\Config
99
     * @throws \Exception
100
     */
101
    protected function generateController()
102
    {
103
        return Tokens::CONTROLLER;
104
    }
105
}
106