Completed
Push — master ( 2c4859...5ed06f )
by Adolfo
14s queued 11s
created

Group::getEnabledPlans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Entities;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract;
7
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
8
9
class Group implements GroupContract
10
{
11
    protected $code = null;
12
    protected $modelPlan;
13
14
    public function __construct(string $code)
15
    {
16
        $this->code = $code;
17
        $this->modelPlan = config('subscriptions.entities.plan');
18
    }
19
20
    public function addPlans(array $plans): void
21
    {
22
        foreach ($plans as $plan) {
23
            $this->addPlan($plan);
24
        }
25
    }
26
27
    public function addPlan(PlanContract $plan): void
28
    {
29
        $plan->changeToGroup($this);
30
    }
31
32
    public function hasPlans(): bool
33
    {
34
        return $this->plans()->count() > 0;
35
    }
36
37
    public function plans(): Builder
38
    {
39
        return $this->modelPlan::query()
40
            ->byGroup($this)
41
            ->orderBy('sort_order');
42
    }
43
44
    public function getDefaultPlan(): ?PlanContract
45
    {
46
        return $this->plans()->isDefault()->first();
47
    }
48
49
    public function __toString(): string
50
    {
51
        return $this->getCode();
52
    }
53
54
    public function getCode(): string
55
    {
56
        return $this->code;
57
    }
58
59
    public function getEnabledPlans()
60
    {
61
        return $this->modelPlan::query()
62
            ->byGroup($this)
63
            ->enabled()
64
            ->orderBy('sort_order')
65
            ->get();
66
    }
67
}
68