Payable::payments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\Traits;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphMany;
8
use Orkhanahmadov\Goldenpay\Enums\CardType;
9
use Orkhanahmadov\Goldenpay\Enums\Language;
10
use Orkhanahmadov\LaravelGoldenpay\Goldenpay;
11
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
12
13
/**
14
 * Trait Payable.
15
 *
16
 * @mixin Model
17
 */
18
trait Payable
19
{
20
    /**
21
     * Returns all related payments.
22
     *
23
     * @return MorphMany
24
     */
25
    public function payments(): MorphMany
26
    {
27
        return $this->morphMany(Payment::class, 'payable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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

27
        return $this->/** @scrutinizer ignore-call */ morphMany(Payment::class, 'payable');
Loading history...
28
    }
29
30
    /**
31
     * Returns all related successful payments.
32
     *
33
     * @return MorphMany
34
     */
35
    public function successfulPayments(): MorphMany
36
    {
37
        return $this->morphMany(Payment::class, 'payable')->whereSuccessful();
38
    }
39
40
    /**
41
     * @param CardType $cardType
42
     * @param int|null $amount
43
     * @param string|null $description
44
     * @param Language|null $lang
45
     *
46
     * @return Payment
47
     *
48
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
49
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
50
     */
51
    public function createPayment(
52
        CardType $cardType,
53
        ?int $amount = null,
54
        ?string $description = null,
55
        ?Language $lang = null
56
    ): Payment {
57
        /** @var Goldenpay $goldenpay */
58
        $goldenpay = Container::getInstance()->make(Goldenpay::class);
59
60
        $payment = $goldenpay->payment(
61
            $amount ?: $this->amount(),
62
            $cardType,
63
            $description ?: $this->description(),
64
            $lang
65
        );
66
67
        $payment->payable_type = self::class;
68
        $payment->payable_id = $this->getKey();
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

68
        /** @scrutinizer ignore-call */ 
69
        $payment->payable_id = $this->getKey();
Loading history...
69
        $payment->save();
70
71
        return $payment;
72
    }
73
74
    /**
75
     * Defines payment amount for this model's payments.
76
     *
77
     * @return int
78
     */
79
    abstract protected function amount(): int;
80
81
    /**
82
     * Defines description for this model's payments.
83
     *
84
     * @return string
85
     */
86
    abstract protected function description(): string;
87
}
88