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

Group::addPlans()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Entities;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
7
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract;
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 getCode(): string
21
    {
22
        return $this->code;
23
    }
24
25
    public function plans(): Builder
26
    {
27
        return $this->modelPlan::query()->byGroup($this);
28
    }
29
30
    public function addPlan(PlanContract $plan): void
31
    {
32
        $plan->changeToGroup($this);
33
    }
34
35
    public function addPlans(array $plans): void
36
    {
37
        foreach ($plans as $plan) {
38
            $this->addPlan($plan);
39
        }
40
    }
41
42
    public function hasPlans(): bool
43
    {
44
        return $this->plans()->count() > 0;
45
    }
46
47
    public function getDefaultPlan(): ?PlanContract
48
    {
49
        return $this->plans()->isDefault()->first();
50
    }
51
52
    public function __toString(): string
53
    {
54
        return $this->getCode();
55
    }
56
}
57