Passed
Push — master ( 43186b...d45fb7 )
by Gabriel
14:32
created

TransactionsTrait::getStatusItemsDirectory()   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\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
    use \ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordsTrait;
21
22
    public function bootTransactionsTrait()
23
    {
24
        static::creating(function (Event $event) {
25
26
            /** @var EmailTrait|\Nip\Records\Record $record */
27
            $record = $event->getRecord();
28
29
            $record->setIf('metadata', '{}', function () use ($record) {
30
                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

30
                return count(/** @scrutinizer ignore-type */ $record->metadata) < 1;
Loading history...
31
            });
32
        });
33
    }
34
35
    /**
36
     * @param Purchase|IsPurchasableModelTrait $purchase
37
     * @return TransactionTrait
38
     */
39
    public function findOrCreateForPurchase($purchase)
40
    {
41
        $transaction = $this->findForPurchase($purchase);
42
        if ($transaction instanceof Record) {
43
            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...
44
        }
45
        return $this->createForPurchase($purchase);
46
    }
47
48
    /**
49
     * @param Purchase|IsPurchasableModelTrait $purchase
50
     * @return TransactionTrait|null
51
     */
52
    public function findForPurchase($purchase)
53
    {
54
        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

54
        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...
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getStatusItemsRootNamespace()
61
    {
62
        return '\ByTIC\Payments\Models\Transactions\Statuses\\';
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getStatusItemsDirectory()
69
    {
70
        return __DIR__ . DIRECTORY_SEPARATOR . 'Statuses';
71
    }
72
73
    /**
74
     * @param Purchase|IsPurchasableModelTrait $purchase
75
     * @return TransactionTrait
76
     */
77
    protected function createForPurchase($purchase)
78
    {
79
        $transaction = $this->getNew();
80
        $transaction->populateFromPayment($purchase);
81
        $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

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

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

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