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

TransactionsTrait::initRelationsToken()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Models\Transactions;
4
5
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRepository;
6
use ByTIC\Payments\Models\AbstractModels\HasToken\HasTokenRepository;
7
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
8
use ByTIC\Payments\Models\Purchases\Purchase;
9
use ByTIC\Payments\Utility\PaymentsModels;
10
use Nip\MailModule\Models\EmailsTable\EmailTrait;
11
use Nip\Records\AbstractModels\Record;
12
use Nip\Records\EventManager\Events\Event;
13
14
/**
15
 * Trait TransactionsTrait
16
 * @package ByTIC\Payments\Models\Transactions
17
 *
18
 * @method TransactionTrait getNew
19
 */
20
trait TransactionsTrait
21
{
22
    use \ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordsTrait;
23
    use HasTokenRepository;
24
    use HasPaymentMethodRepository;
25
26
    public function bootTransactionsTrait()
27
    {
28
        static::creating(function (Event $event) {
29
30
            /** @var EmailTrait|\Nip\Records\Record $record */
31
            $record = $event->getRecord();
32
33
            $record->setIf('metadata', '{}', function () use ($record) {
34
                return count($record->metadata) < 1;
0 ignored issues
show
Bug introduced by
It seems like $record->metadata can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

34
                return count(/** @scrutinizer ignore-type */ $record->metadata) < 1;
Loading history...
35
            });
36
        });
37
    }
38
39
    /**
40
     * @param Purchase|IsPurchasableModelTrait $purchase
41
     * @return TransactionTrait
42
     */
43
    public function findOrCreateForPurchase($purchase)
44
    {
45
        $transaction = $this->findForPurchase($purchase);
46
        if ($transaction instanceof Record) {
47
            return $transaction;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transaction also could return the type Nip\Records\AbstractModels\Record which is incompatible with the documented return type ByTIC\Payments\Models\Tr...ctions\TransactionTrait.
Loading history...
48
        }
49
        return $this->createForPurchase($purchase);
50
    }
51
52
    /**
53
     * @param Purchase|IsPurchasableModelTrait $purchase
54
     * @return TransactionTrait|null
55
     */
56
    public function findForPurchase($purchase)
57
    {
58
        return $this->findOneByField('id_purchase', $purchase->id);
0 ignored issues
show
Bug introduced by
The method findOneByField() does not exist on ByTIC\Payments\Models\Tr...tions\TransactionsTrait. Did you maybe mean findOneByQuery()? ( Ignorable by Annotation )

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

58
        return $this->/** @scrutinizer ignore-call */ findOneByField('id_purchase', $purchase->id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getStatusItemsRootNamespace()
65
    {
66
        return '\ByTIC\Payments\Models\Transactions\Statuses\\';
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getStatusItemsDirectory()
73
    {
74
        return __DIR__ . DIRECTORY_SEPARATOR . 'Statuses';
75
    }
76
77
    /**
78
     * @param Purchase|IsPurchasableModelTrait $purchase
79
     * @return TransactionTrait
80
     */
81
    protected function createForPurchase($purchase)
82
    {
83
        $transaction = $this->getNew();
84
        $transaction->populateFromPayment($purchase);
85
        $transaction->populateFromGateway($purchase->getPaymentMethod()->getType()->getGateway());
0 ignored issues
show
Bug introduced by
The method getGateway() does not exist on ByTIC\Payments\Models\Methods\Types\AbstractType. It seems like you code against a sub-type of ByTIC\Payments\Models\Methods\Types\AbstractType such as ByTIC\Payments\Models\Methods\Types\CreditCards. ( Ignorable by Annotation )

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

85
        $transaction->populateFromGateway($purchase->getPaymentMethod()->getType()->/** @scrutinizer ignore-call */ getGateway());
Loading history...
86
        $transaction->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

86
        $transaction->/** @scrutinizer ignore-call */ 
87
                      insert();
Loading history...
87
        return $transaction;
88
    }
89
90
    protected function initRelations()
91
    {
92
        parent::initRelations();
93
        $this->initRelationsCommon();
94
    }
95
96
    protected function initRelationsCommon()
97
    {
98
        $this->initRelationsPurchase();
99
        $this->initRelationsPaymentMethod();
100
        $this->initRelationsSubscription();
101
        $this->initRelationsToken();
102
    }
103
104
    protected function initRelationsPurchase()
105
    {
106
        $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

106
        $this->/** @scrutinizer ignore-call */ 
107
               belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
Loading history...
107
    }
108
109
    protected function initRelationsSubscription()
110
    {
111
        $this->belongsTo('Subscription', ['class' => get_class(PaymentsModels::subscriptions())]);
112
    }
113
114
    /**
115
     * @param array $params
116
     */
117
    protected function injectParams(&$params = [])
118
    {
119
        $params['order'][] = ['created', 'desc'];
120
121
        parent::injectParams($params);
122
    }
123
124
    /**
125
     * @return mixed|\Nip\Config\Config
126
     * @throws \Exception
127
     */
128
    protected function generateTable()
129
    {
130
        return config('payments.tables.transactions', \ByTIC\Payments\Models\Transactions\Transactions::TABLE);
131
    }
132
}
133