Passed
Push — master ( 5ace1f...ba21e2 )
by Gabriel
12:24
created

TokensTrait::injectParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
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->initRelationsPurchase();
69
        $this->initRelationsPaymentMethod();
70
    }
71
72
    protected function initRelationsPurchase()
73
    {
74
        $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

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