Passed
Push — main ( a28ddf...3e6196 )
by Michael
04:09
created

HasCoupons::isCouponRedeemed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Couponables;
6
7
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8
use Illuminate\Support\Str;
9
use MichaelRubel\Couponables\Models\Contracts\CouponContract;
10
use MichaelRubel\Couponables\Models\Contracts\CouponPivotContract;
11
use MichaelRubel\Couponables\Services\Contracts\CouponServiceContract;
12
use MichaelRubel\EnhancedContainer\Call;
13
14
trait HasCoupons
15
{
16
    /**
17
     * Polymorphic relation to the coupons.
18
     *
19
     * @return MorphToMany
20
     */
21 13
    public function coupons(): MorphToMany
22
    {
23 13
        return $this->morphToMany(app(CouponContract::class), Str::singular(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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

23
        return $this->/** @scrutinizer ignore-call */ morphToMany(app(CouponContract::class), Str::singular(
Loading history...
24 13
            config('couponables.pivot_table', 'couponables')
25
        ))->withPivot(
26 13
            call(CouponPivotContract::class)->getRedeemedAtColumn()
27
        );
28
    }
29
30
    /**
31
     * Use the coupon.
32
     *
33
     * @param string $code
34
     *
35
     * @return CouponContract
36
     */
37 15
    public function redeemCoupon(string $code): CouponContract
38
    {
39 15
        $service = call(CouponServiceContract::class);
40 15
        $proxy   = call($service->verifyCoupon($code, $this));
41
42 12
        $coupon  = $proxy->getInternal(Call::INSTANCE);
43
44 12
        return $service->applyCoupon($coupon, $this);
45
    }
46
47
    /**
48
     * Check if the coupon is already redeemed by the model at least once.
49
     *
50
     * @param string $code
51
     *
52
     * @return bool
53
     */
54 3
    public function isCouponRedeemed(string $code): bool
55
    {
56 3
        $column = call(CouponContract::class)
57
            ->getCodeColumn();
58
59 3
        return $this->coupons()
60
            ->where($column, $code)
61
            ->exists();
62
    }
63
64
    /**
65
     * Check if coupon with this code is already used.
66
     *
67
     * @param string $code
68
     *
69
     * @return bool
70
     */
71 3
    public function isCouponAlreadyUsed(string $code): bool
72
    {
73 3
        return $this->isCouponRedeemed($code);
74
    }
75
}
76