Completed
Push — master ( 8ce3f9...e1543f )
by Adolfo
14s queued 11s
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 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