Completed
Push — master ( 8ce3f9...e1543f )
by Adolfo
14s queued 11s
created

Plan::scopeIsDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
8
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract;
9
use Sagitarius29\LaravelSubscriptions\Contracts\PlanFeatureContract;
10
11
abstract class Plan extends Model implements PlanContract
12
{
13
    protected $table = 'plans';
14
15
    protected $fillable = [
16
        'name', 'description', 'free_days', 'sort_order', 'is_active', 'is_default', 'group',
17
    ];
18
19
    public function scopeByGroup(Builder $q, GroupContract $group = null)
20
    {
21
        return $q->where('group', $group);
22
    }
23
24
    public function scopeIsDefault(Builder $q)
25
    {
26
        return $q->where('is_default', 1);
27
    }
28
29
    public function features()
30
    {
31
        return $this->hasMany(config('subscriptions.entities.plan_feature'));
32
    }
33
34
    public function intervals()
35
    {
36
        return $this->hasMany(config('subscriptions.entities.plan_interval'), 'plan_id')
37
            ->orderBy('price');
38
    }
39
40
    public function subscriptions()
41
    {
42
        return $this->hasMany(config('subscriptions.entities.plan_subscription'));
43
    }
44
45
    public function addFeature(PlanFeatureContract $feature)
46
    {
47
        $this->features()->save($feature);
0 ignored issues
show
Compatibility introduced by
$feature of type object<Sagitarius29\Lara...ts\PlanFeatureContract> is not a sub-type of object<Illuminate\Database\Eloquent\Model>. It seems like you assume a concrete implementation of the interface Sagitarius29\LaravelSubs...cts\PlanFeatureContract to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
    }
49
50
    public function isDefault(): bool
51
    {
52
        return $this->is_default;
53
    }
54
55
    public function isActive(): bool
56
    {
57
        return $this->is_active;
58
    }
59
60
    public function isFree(): bool
61
    {
62
        return $this->intervals()->count() == 0 || $this->intervals()->first()->price == 0;
63
    }
64
65
    public function isNotFree(): bool
66
    {
67
        return $this->intervals()->count() > 0 && $this->intervals()->first()->price > 0;
68
    }
69
70
    public function hasManyIntervals(): bool
71
    {
72
        return $this->intervals()->count() > 1;
73
    }
74
75
    public static function create(
76
        string $name,
77
        string $description,
78
        int $free_days,
79
        int $sort_order,
80
        bool $is_active = false,
81
        bool $is_default = false,
82
        GroupContract $group = null
83
    ): Model {
84
        $attributes = [
85
            'name'          => $name,
86
            'description'   => $description,
87
            'free_days'     => $free_days,
88
            'sort_order'    => $sort_order,
89
            'is_active'     => $is_active,
90
            'is_default'    => $is_default,
91
            'group'         => $group,
92
        ];
93
        $calledClass = get_called_class();
94
95
        if (! self::defaultExists($group)) {
96
            $attributes['is_default'] = true;
97
        }
98
99
        $plan = new $calledClass($attributes);
100
        $plan->save();
101
102
        return $plan;
103
    }
104
105
    public function setFree()
106
    {
107
        $this->intervals()->delete();
108
    }
109
110
    public function setDefault()
111
    {
112
        $myGroup = $this->myGroup();
113
        $calledClass = get_called_class();
114
115
        $currentDefaults = $calledClass::query()
116
            ->byGroup($myGroup)
117
            ->isDefault()
118
            ->get();
119
120
        $currentDefaults->each(function ($plan) {
121
            $plan->is_default = false;
122
            $plan->save();
123
        });
124
125
        $this->is_default = true;
126
        $this->save();
127
    }
128
129
    public function myGroup(): ?GroupContract
130
    {
131
        return empty($this->group) ? null : new \Sagitarius29\LaravelSubscriptions\Entities\Group($this->group);
132
    }
133
134
    public function changeToGroup(GroupContract $group): void
135
    {
136
        $this->group = $group;
137
138
        if (! self::defaultExists($group)) {
139
            $this->is_default = true;
140
        }
141
142
        $this->save();
143
    }
144
145
    private static function defaultExists(GroupContract $group = null): bool
146
    {
147
        $calledClass = get_called_class();
148
149
        return $calledClass::query()
150
            ->byGroup($group)
151
            ->isDefault()
152
            ->exists();
153
    }
154
}
155