Completed
Push — develop ( f50f20...f9e081 )
by Adolfo
01:10
created

Group::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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 Illuminate\Support\Str;
7
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
8
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract;
9
10
class Group implements GroupContract
11
{
12
    protected $code = null;
13
    protected $modelPlan;
14
15
    public function __construct(string $code)
16
    {
17
        $this->code = $code;
18
        $this->modelPlan = config('subscriptions.entities.plan');
19
    }
20
21
    public function getCode(): string
22
    {
23
        return $this->code;
24
    }
25
26
    public function plans(): Builder
27
    {
28
        return $this->modelPlan::query()->byGroup($this);
29
    }
30
31
    public function addPlan(PlanContract $plan): void
32
    {
33
        $plan->changeToGroup($this);
34
    }
35
36
    public function addPlans(array $plans): void
37
    {
38
        foreach ($plans as $plan) {
39
            $this->addPlan($plan);
40
        }
41
    }
42
43
    public function hasPlans(): bool
44
    {
45
        return $this->plans()->count() > 0;
46
    }
47
48
    public function getDefaultPlan(): ?PlanContract
49
    {
50
        return $this->plans()->isDefault()->first();
51
    }
52
53
    public function __toString(): string
54
    {
55
        return $this->getCode();
56
    }
57
}
58