Passed
Push — master ( c0f464...9c6e80 )
by Gabriel
11:21
created

TokensTrait::initRelationsCommon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 0
dl 0
loc 4
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 getNew
14
 */
15
trait TokensTrait
16
{
17
18
    public function findOrCreateForMethod($method, TokenInterface $token)
19
    {
20
        $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

20
        /** @scrutinizer ignore-call */ 
21
        $findToken = $this->findOneByParams(
Loading history...
21
            [
22
                'where' => [
23
                    ['id_method =?', $method],
24
                    ['token_id = ?', $token->getId()],
25
                ]
26
            ]);
27
28
        if ($findToken instanceof Record) {
29
            return $findToken;
30
        }
31
        $token = $this->getNew();
32
        $token->populateFromPaymentMethod($method);
33
        $token->populateFromToken($token);
0 ignored issues
show
Bug introduced by
$token of type ByTIC\Payments\Models\Tokens\TokenTrait is incompatible with the type ByTIC\Omnipay\Common\Models\TokenInterface expected by parameter $token of ByTIC\Payments\Models\To...it::populateFromToken(). ( Ignorable by Annotation )

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

33
        $token->populateFromToken(/** @scrutinizer ignore-type */ $token);
Loading history...
34
        $token->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

34
        $token->/** @scrutinizer ignore-call */ 
35
                insert();
Loading history...
35
        return $token;
36
    }
37
38
    protected function initRelations()
39
    {
40
        parent::initRelations();
41
    }
42
43
    protected function initRelationsCommon()
44
    {
45
        $this->initRelationsPurchase();
46
        $this->initRelationsPaymentMethod();
47
    }
48
49
    protected function initRelationsPurchase()
50
    {
51
        $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

51
        $this->/** @scrutinizer ignore-call */ 
52
               belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
Loading history...
52
    }
53
54
    protected function initRelationsPaymentMethod()
55
    {
56
        $this->belongsTo('PaymentMethod', ['class' => get_class(PaymentsModels::methods())]);
57
    }
58
59
    /**
60
     * @param array $params
61
     */
62
    protected function injectParams(&$params = [])
63
    {
64
        $params['order'][] = ['created', 'desc'];
65
66
        parent::injectParams($params);
67
    }
68
69
70
    /**
71
     * @return mixed|\Nip\Config\Config
72
     * @throws \Exception
73
     */
74
    protected function generateTable()
75
    {
76
        return config('payments.tables.tokens', Tokens::TABLE);
77
    }
78
}
79