Completed
Push — develop ( 8deed5...67cc12 )
by Adolfo
01:12
created

Plan   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 5
dl 0
loc 178
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 27 2
A defaultExists() 0 9 1
A scopeByGroup() 0 4 1
A scopeIsDefault() 0 4 1
A scopeEnabled() 0 4 1
A scopeDisabled() 0 4 1
A subscriptions() 0 4 1
A isDefault() 0 4 1
A isEnabled() 0 4 1
A isDisabled() 0 4 1
A isFree() 0 4 2
A intervals() 0 5 1
A isNotFree() 0 4 2
A hasManyIntervals() 0 4 1
A setFree() 0 4 1
A setDefault() 0 18 1
A myGroup() 0 4 2
A changeToGroup() 0 10 2
A getName() 0 4 1
A getDescription() 0 4 1
A delete() 0 7 2
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\Entities\Group;
10
use Sagitarius29\LaravelSubscriptions\Exceptions\PlanErrorException;
11
use Sagitarius29\LaravelSubscriptions\Traits\HasFeatures;
12
13
abstract class Plan extends Model implements PlanContract
14
{
15
    use HasFeatures;
16
17
    protected $table = 'plans';
18
19
    protected $fillable = [
20
        'name', 'description', 'free_days', 'sort_order', 'is_enabled', 'is_default', 'group',
21
    ];
22
23
    /**
24
     * @param  string  $name
25
     * @param  string  $description
26
     * @param  int  $free_days
0 ignored issues
show
Bug introduced by
There is no parameter named $free_days. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
     * @param  int  $sort_order
28
     * @param  bool  $is_enabled
29
     * @param  bool  $is_default
30
     * @param  GroupContract|null  $group
31
     * @return Model|PlanContract
32
     */
33
    public static function create(
34
        string $name,
35
        string $description,
36
        int $sort_order,
37
        bool $is_enabled = false,
38
        bool $is_default = false,
39
        GroupContract $group = null
40
    ): PlanContract {
41
        $attributes = [
42
            'name' => $name,
43
            'description' => $description,
44
            'sort_order' => $sort_order,
45
            'is_enabled' => $is_enabled,
46
            'is_default' => $is_default,
47
            'group' => $group,
48
        ];
49
        $calledClass = get_called_class();
50
51
        if (! self::defaultExists($group)) {
52
            $attributes['is_default'] = true;
53
        }
54
55
        $plan = new $calledClass($attributes);
56
        $plan->save();
57
58
        return $plan;
59
    }
60
61
    private static function defaultExists(GroupContract $group = null): bool
62
    {
63
        $calledClass = get_called_class();
64
65
        return $calledClass::query()
66
            ->byGroup($group)
67
            ->isDefault()
68
            ->exists();
69
    }
70
71
    public function scopeByGroup(Builder $q, GroupContract $group = null)
72
    {
73
        return $q->where('group', $group);
74
    }
75
76
    public function scopeIsDefault(Builder $q)
77
    {
78
        return $q->where('is_default', 1);
79
    }
80
81
    public function scopeEnabled(Builder $q)
82
    {
83
        return $q->where('is_enabled', 1);
84
    }
85
86
    public function scopeDisabled(Builder $q)
87
    {
88
        return $q->where('is_enabled', 1);
89
    }
90
91
    public function subscriptions()
92
    {
93
        return $this->hasMany(config('subscriptions.entities.plan_subscription'));
94
    }
95
96
    public function isDefault(): bool
97
    {
98
        return $this->is_default;
99
    }
100
101
    public function isEnabled(): bool
102
    {
103
        return $this->is_enabled;
104
    }
105
106
    public function isDisabled(): bool
107
    {
108
        return ! $this->is_enabled;
109
    }
110
111
    public function isFree(): bool
112
    {
113
        return $this->intervals()->count() == 0 || $this->intervals()->first()->price == 0;
114
    }
115
116
    public function intervals()
117
    {
118
        return $this->hasMany(config('subscriptions.entities.plan_interval'), 'plan_id')
119
            ->orderBy('price');
120
    }
121
122
    public function isNotFree(): bool
123
    {
124
        return $this->intervals()->count() > 0 && $this->intervals()->first()->price > 0;
125
    }
126
127
    public function hasManyIntervals(): bool
128
    {
129
        return $this->intervals()->count() > 1;
130
    }
131
132
    public function setFree()
133
    {
134
        $this->intervals()->delete();
135
    }
136
137
    public function setDefault()
138
    {
139
        $myGroup = $this->myGroup();
140
        $calledClass = get_called_class();
141
142
        $currentDefaults = $calledClass::query()
143
            ->byGroup($myGroup)
144
            ->isDefault()
145
            ->get();
146
147
        $currentDefaults->each(function ($plan) {
148
            $plan->is_default = false;
149
            $plan->save();
150
        });
151
152
        $this->is_default = true;
153
        $this->save();
154
    }
155
156
    public function myGroup(): ?GroupContract
157
    {
158
        return empty($this->group) ? null : new Group($this->group);
159
    }
160
161
    public function changeToGroup(GroupContract $group): void
162
    {
163
        $this->group = $group;
164
165
        if (! self::defaultExists($group)) {
166
            $this->is_default = true;
167
        }
168
169
        $this->save();
170
    }
171
172
    public function getName()
173
    {
174
        return $this->name;
175
    }
176
177
    public function getDescription()
178
    {
179
        return $this->description;
180
    }
181
182
    public function delete()
183
    {
184
        if($this->subscriptions()->exists() > 0) {
185
            throw new PlanErrorException('You cannot delete this plan because this has subscriptions.');
186
        }
187
        return parent::delete();
188
    }
189
190
}
191