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

Group   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCode() 0 4 1
A plans() 0 4 1
A addPlan() 0 4 1
A addPlans() 0 6 2
A hasPlans() 0 4 1
A getDefaultPlan() 0 4 1
A __toString() 0 4 1
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