Completed
Push — develop ( cd2304...f50f20 )
by Adolfo
03:04
created

Plan::toGroup()   A

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 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
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 intervals()
23
    {
24
        return $this->hasMany(config('subscriptions.entities.plan_interval'), 'plan_id')
25
            ->orderBy('price');
26
    }
27
28
    public function subscriptions()
29
    {
30
        return $this->hasMany(config('subscriptions.entities.subscription'));
31
    }
32
33
    public function isDefault(): bool
34
    {
35
        return $this->is_default;
36
    }
37
38
    public function isActive(): bool
39
    {
40
        return $this->is_active;
41
    }
42
43
    public function isFree(): bool
44
    {
45
        return $this->intervals()->count() == 0 || $this->intervals()->first()->price == 0;
46
    }
47
48
    public function isNotFree(): bool
49
    {
50
        return $this->intervals()->count() > 0 && $this->intervals()->first()->price > 0;
51
    }
52
53
    public function hasManyIntervals(): bool
54
    {
55
        return $this->intervals()->count() > 1;
56
    }
57
58
    public static function create(
59
        string $name,
60
        string $description,
61
        int $free_days,
62
        int $sort_order,
63
        bool $is_active = false,
64
        bool $is_default = false
65
    ): Model {
66
        $attributes = [
67
            'name'          => $name,
68
            'description'   => $description,
69
            'free_days'     => $free_days,
70
            'sort_order'    => $sort_order,
71
            'is_active'     => $is_active,
72
            'is_default'    => $is_default,
73
        ];
74
75
        $calledClass = get_called_class();
76
77
        $plan = new $calledClass($attributes);
78
        $plan->save();
79
80
        return $plan;
81
    }
82
83
    public function setFree()
84
    {
85
        $this->intervals()->delete();
86
    }
87
88
    public function myGroup(): ?GroupContract
89
    {
90
        return empty($this->group) ? null : new $this->group;
91
    }
92
93
    public function toGroup(GroupContract $group): void
94
    {
95
        // TODO: Implement toGroup() method.
96
    }
97
}
98