Passed
Push — master ( 19a915...d9efc8 )
by Gabriel
14:56
created

TokensTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 2
Metric Value
eloc 23
c 4
b 2
f 2
dl 0
loc 76
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initRelations() 0 3 1
A initRelationsPurchase() 0 3 1
A createForMethod() 0 8 1
A initRelationsCommon() 0 4 1
A injectParams() 0 5 1
A generateTable() 0 3 1
A findOrCreateForMethod() 0 15 3
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
        if ($findToken instanceof Record) {
38
            return $findToken;
39
        }
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();
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

55
        $item->/** @scrutinizer ignore-call */ 
56
               insert();
Loading history...
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
    /**
76
     * @param array $params
77
     */
78
    protected function injectParams(&$params = [])
79
    {
80
        $params['order'][] = ['created', 'desc'];
81
82
        parent::injectParams($params);
83
    }
84
85
86
    /**
87
     * @return mixed|\Nip\Config\Config
88
     * @throws \Exception
89
     */
90
    protected function generateTable()
91
    {
92
        return config('payments.tables.tokens', Tokens::TABLE);
93
    }
94
}
95