Completed
Push — master ( 3ec705...3da051 )
by Orkhan
01:08
created

Payable::createPayment()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 1
nop 4
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\Traits;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Orkhanahmadov\Goldenpay\Enums\CardType;
8
use Orkhanahmadov\Goldenpay\Enums\Language;
9
use Orkhanahmadov\LaravelGoldenpay\Goldenpay;
10
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
11
12
trait Payable
13
{
14
    public function payments(): MorphMany
15
    {
16
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    public function successfulPayments(): MorphMany
20
    {
21
        return $this->morphMany(Payment::class, 'payable')->successful();
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
    }
23
24
    /**
25
     * @param int $amount
26
     * @param CardType $cardType
27
     * @param string|null $description
28
     * @param Language|null $lang
29
     *
30
     * @return Payment
31
     *
32
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
33
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
34
     */
35
    public function createPayment(
36
        int $amount,
37
        CardType $cardType,
38
        ?string $description = null,
39
        ?Language $lang = null
40
    ): Payment {
41
        /** @var Goldenpay $goldenpay */
42
        $goldenpay = Container::getInstance()->make(Goldenpay::class);
43
44
        $payment = $goldenpay->payment($amount, $cardType, $description ?: $this->description(), $lang);
45
46
        $payment->payable_type = self::class;
47
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
        $payment->save();
49
50
        return $payment;
51
    }
52
53
    /**
54
     * Define description for this model's payments.
55
     *
56
     * @return string
57
     */
58
    abstract public function description(): string;
59
}
60