Completed
Push — master ( 77de06...45cc8d )
by
unknown
01:29
created

AllowedInclude   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 3
cbo 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A relationship() 0 14 3
A count() 0 6 1
A include() 0 4 1
A getName() 0 4 1
A isForInclude() 0 4 1
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Spatie\QueryBuilder\Includes\IncludedCount;
8
use Spatie\QueryBuilder\Includes\IncludeInterface;
9
use Spatie\QueryBuilder\Includes\IncludedRelationship;
10
11
class AllowedInclude
12
{
13
    /** @var string */
14
    protected $name;
15
16
    /** @var IncludeInterface */
17
    protected $includeClass;
18
19
    /** @var string|null */
20
    protected $internalName;
21
22
    public function __construct(string $name, IncludeInterface $includeClass, ?string $internalName = null)
23
    {
24
        $this->name = Str::camel($name);
25
        $this->includeClass = $includeClass;
26
        $this->internalName = $internalName ?? $this->name;
27
    }
28
29
    public static function relationship(string $name, ?string $internalName = null): Collection
30
    {
31
        $internalName = Str::camel($internalName ?? $name);
32
33
        return IncludedRelationship::getIndividualRelationshipPathsFromInclude($internalName)
34
            ->flatMap(function (string $relationship) use ($name, $internalName): Collection {
35
                return collect([
36
                    new self($relationship, new IncludedRelationship(), $relationship === $internalName ? $internalName : null),
37
                ])
38
                    ->when(! Str::contains($relationship, '.'), function (Collection $includes) use ($internalName, $relationship) {
39
                        return $includes->merge(self::count("{$relationship}Count", $relationship === $internalName ? "{$internalName}Count" : null));
40
                    });
41
            });
42
    }
43
44
    public static function count(string $name, ?string $internalName = null): Collection
45
    {
46
        return collect([
47
            new static($name, new IncludedCount(), $internalName),
48
        ]);
49
    }
50
51
    public function include(QueryBuilder $query): void
52
    {
53
        ($this->includeClass)($query, $this->internalName);
54
    }
55
56
    public function getName(): string
57
    {
58
        return $this->name;
59
    }
60
61
    public function isForInclude(string $includeName): bool
62
    {
63
        return $this->name === $includeName;
64
    }
65
}
66