HasSubscriptions::subscriptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Subscriptions\Traits;
6
7
use Carbon\Carbon;
8
use Rinvex\Subscriptions\Models\Plan;
9
use Rinvex\Subscriptions\Services\Period;
10
use Illuminate\Database\Eloquent\Collection;
11
use Rinvex\Subscriptions\Models\PlanSubscription;
12
use Illuminate\Database\Eloquent\Relations\MorphMany;
13
14
trait HasSubscriptions
15
{
16
    /**
17
     * Define a polymorphic one-to-many relationship.
18
     *
19
     * @param string $related
20
     * @param string $name
21
     * @param string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
22
     * @param string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     * @param string $localKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $localKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
24
     *
25
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
26
     */
27
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
28
29
    /**
30
     * The subscriber may have many subscriptions.
31
     *
32
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
33
     */
34
    public function subscriptions(): MorphMany
35
    {
36
        return $this->morphMany(config('rinvex.subscriptions.models.plan_subscription'), 'subscriber', 'subscriber_type', 'subscriber_id');
37
    }
38
39
    /**
40
     * A model may have many active subscriptions.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Collection
43
     */
44
    public function activeSubscriptions(): Collection
45
    {
46
        return $this->subscriptions->reject->inactive();
0 ignored issues
show
Bug introduced by
The property subscriptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
    }
48
49
    /**
50
     * Get a subscription by slug.
51
     *
52
     * @param string $subscriptionSlug
53
     *
54
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
55
     */
56
    public function subscription(string $subscriptionSlug): ?PlanSubscription
57
    {
58
        return $this->subscriptions()->where('slug', $subscriptionSlug)->first();
59
    }
60
61
    /**
62
     * Get subscribed plans.
63
     *
64
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
65
     */
66
    public function subscribedPlans(): ?PlanSubscription
67
    {
68
        $planIds = $this->subscriptions->reject->inactive()->pluck('plan_id')->unique();
69
70
        return app('rinvex.subscriptions.plan')->whereIn('id', $planIds)->get();
71
    }
72
73
    /**
74
     * Check if the subscriber subscribed to the given plan.
75
     *
76
     * @param int $planId
77
     *
78
     * @return bool
79
     */
80
    public function subscribedTo($planId): bool
81
    {
82
        $subscription = $this->subscriptions()->where('plan_id', $planId)->first();
83
84
        return $subscription && $subscription->active();
85
    }
86
87
    /**
88
     * Subscribe subscriber to a new plan.
89
     *
90
     * @param string                            $subscription
91
     * @param \Rinvex\Subscriptions\Models\Plan $plan
92
     * @param \Carbon\Carbon|null               $startDate
93
     *
94
     * @return \Rinvex\Subscriptions\Models\PlanSubscription
95
     */
96
    public function newSubscription($subscription, Plan $plan, Carbon $startDate = null): PlanSubscription
97
    {
98
        $trial = new Period($plan->trial_interval, $plan->trial_period, $startDate ?? now());
99
        $period = new Period($plan->invoice_interval, $plan->invoice_period, $trial->getEndDate());
100
101
        return $this->subscriptions()->create([
102
            'name' => $subscription,
103
            'plan_id' => $plan->getKey(),
104
            'trial_ends_at' => $trial->getEndDate(),
105
            'starts_at' => $period->getStartDate(),
106
            'ends_at' => $period->getEndDate(),
107
        ]);
108
    }
109
}
110