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

TransactionsTrait::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\Transactions;
4
5
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
6
use ByTIC\Payments\Models\Purchases\Purchase;
7
use ByTIC\Payments\Utility\PaymentsModels;
8
use Nip\MailModule\Models\EmailsTable\EmailTrait;
9
use Nip\Records\AbstractModels\Record;
10
use Nip\Records\EventManager\Events\Event;
11
12
/**
13
 * Trait TransactionsTrait
14
 * @package ByTIC\Payments\Models\Transactions
15
 *
16
 * @method TransactionTrait getNew
17
 */
18
trait TransactionsTrait
19
{
20
21
    public function bootTransactionsTrait()
22
    {
23
        static::creating(function (Event $event) {
24
25
            /** @var EmailTrait|\Nip\Records\Record $record */
26
            $record = $event->getRecord();
27
28
            $record->setIf('metadata', '{}', function () use ($record) {
29
                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

29
                return count(/** @scrutinizer ignore-type */ $record->metadata) < 1;
Loading history...
30
            });
31
        });
32
    }
33
34
    /**
35
     * @param Purchase|IsPurchasableModelTrait $purchase
36
     */
37
    public function findOrCreateForPurchase($purchase)
38
    {
39
        $transaction = $this->findForPurchase($purchase);
40
        if ($transaction instanceof Record) {
41
            return $transaction;
42
        }
43
        return $this->createForPurchase($purchase);
44
    }
45
46
    /**
47
     * @param Purchase|IsPurchasableModelTrait $purchase
48
     * @return \Nip\Records\AbstractModels\Record|null
49
     */
50
    public function findForPurchase($purchase)
51
    {
52
        return $this->findOneByField('id_purchase', $purchase->id);
0 ignored issues
show
Bug introduced by
It seems like findOneByField() 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

52
        return $this->/** @scrutinizer ignore-call */ findOneByField('id_purchase', $purchase->id);
Loading history...
53
    }
54
55
    /**
56
     * @param Purchase|IsPurchasableModelTrait $purchase
57
     */
58
    protected function createForPurchase($purchase)
59
    {
60
        $transaction = $this->getNew();
61
        $transaction->populateFromPayment($purchase);
62
        $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

62
        $transaction->/** @scrutinizer ignore-call */ 
63
                      insert();
Loading history...
63
        return $transaction;
64
    }
65
66
    protected function initRelations()
67
    {
68
        parent::initRelations();
69
    }
70
71
    protected function initRelationsCommon()
72
    {
73
        $this->initRelationsPurchase();
74
        $this->initRelationsPaymentMethod();
75
        $this->initRelationsToken();
76
    }
77
78
    protected function initRelationsPurchase()
79
    {
80
        $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

80
        $this->/** @scrutinizer ignore-call */ 
81
               belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]);
Loading history...
81
    }
82
83
    protected function initRelationsPaymentMethod()
84
    {
85
        $this->belongsTo('PaymentMethod', ['class' => get_class(PaymentsModels::methods())]);
86
    }
87
88
    protected function initRelationsToken()
89
    {
90
        $this->belongsTo('PaymentToken', ['class' => get_class(PaymentsModels::tokens())]);
91
    }
92
93
    /**
94
     * @param array $params
95
     */
96
    protected function injectParams(&$params = [])
97
    {
98
        $params['order'][] = ['created', 'desc'];
99
100
        parent::injectParams($params);
101
    }
102
103
    /**
104
     * @return mixed|\Nip\Config\Config
105
     * @throws \Exception
106
     */
107
    protected function generateTable()
108
    {
109
        return config('payments.tables.transactions', \ByTIC\Payments\Models\Transactions\Transactions::TABLE);
110
    }
111
}
112