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

Plan   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 73
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A features() 0 4 1
A prices() 0 4 1
A subscriptions() 0 4 1
A isDefault() 0 4 1
A isActive() 0 4 1
A isFree() 0 4 2
A create() 0 19 1
A setFree() 0 4 1
A myGroup() 0 4 2
A toGroup() 0 4 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
7
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract;
8
9
abstract class Plan extends Model implements PlanContract
10
{
11
    protected $table = 'plans';
12
13
    protected $fillable = [
14
        'name', 'description', 'free_days', 'sort_order', 'is_active', 'is_default',
15
    ];
16
17
    public function features()
18
    {
19
        return $this->hasMany(config('subscriptions.entities.plan_feature'));
20
    }
21
22
    public function prices()
23
    {
24
        return $this->hasMany(config('subscriptions.entities.plan_price'), 'plan_id')->orderBy('amount');
25
    }
26
27
    public function subscriptions()
28
    {
29
        return $this->hasMany(config('subscriptions.entities.subscription'));
30
    }
31
32
    public function isDefault(): bool
33
    {
34
        return $this->is_default;
35
    }
36
37
    public function isActive(): bool
38
    {
39
        return $this->is_active;
40
    }
41
42
    public function isFree(): bool
43
    {
44
        return $this->prices()->count() == 0 || $this->prices()->first()->amount == 0;
45
    }
46
47
    public static function create(
48
        string $name, string $description, int $free_days, int $sort_order, bool $is_active = false, bool $is_default = false
49
    ): Model {
50
        $attributes = [
51
            'name'          => $name,
52
            'description'   => $description,
53
            'free_days'     => $free_days,
54
            'sort_order'    => $sort_order,
55
            'is_active'     => $is_active,
56
            'is_default'    => $is_default,
57
        ];
58
59
        $calledClass = get_called_class();
60
61
        $plan = new $calledClass($attributes);
62
        $plan->save();
63
64
        return $plan;
65
    }
66
67
    public function setFree()
68
    {
69
        $this->prices()->delete();
70
    }
71
72
    public function myGroup(): ?GroupContract
73
    {
74
        return empty($this->group) ? null : new $this->group;
75
    }
76
77
    public function toGroup(GroupContract $group): void
78
    {
79
        // TODO: Implement toGroup() method.
80
    }
81
}
82