Relation::getRelated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Spinen\Halo\Support\Relations;
4
5
use Illuminate\Support\Traits\ForwardsCalls;
6
use Illuminate\Support\Traits\Macroable;
7
use Spinen\Halo\Exceptions\InvalidRelationshipException;
8
use Spinen\Halo\Support\Builder;
9
use Spinen\Halo\Support\Model;
10
11
/**
12
 * Class Relation
13
 */
14
abstract class Relation
15
{
16
    use ForwardsCalls, Macroable {
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Spinen\Halo\Support\Relations\Relation.
Loading history...
17
        __call as macroCall;
18
    }
19
20
    /**
21
     * The related model instance.
22
     */
23
    protected Model $related;
24
25
    /**
26
     * Create a new relation instance.
27
     *
28
     *
29
     * @return void
30
     *
31
     * @throws InvalidRelationshipException
32
     */
33 25
    public function __construct(protected Builder $builder, protected Model $parent)
34
    {
35 25
        $this->related = $builder->getModel();
36
    }
37
38
    /**
39
     * Handle dynamic method calls to the relationship.
40
     */
41 3
    public function __call(string $method, array $parameters)
42
    {
43 3
        if (static::hasMacro($method)) {
44 1
            return $this->macroCall($method, $parameters);
0 ignored issues
show
Bug introduced by
The method macroCall() does not exist on Spinen\Halo\Support\Relations\Relation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            return $this->/** @scrutinizer ignore-call */ macroCall($method, $parameters);
Loading history...
45
        }
46
47 2
        $result = $this->forwardCallTo($this->getBuilder(), $method, $parameters);
48
49 2
        if ($result === $this->getBuilder()) {
50 1
            return $this;
51
        }
52
53 1
        return $result;
54
    }
55
56
    /**
57
     * Get the Builder instance
58
     */
59 11
    public function getBuilder(): Builder
60
    {
61 11
        return $this->builder;
62
    }
63
64
    /**
65
     * Get the parent Model instance
66
     */
67 4
    public function getParent(): Model
68
    {
69 4
        return $this->parent;
70
    }
71
72
    /**
73
     * Get the related Model instance
74
     */
75 1
    public function getRelated(): Model
76
    {
77 1
        return $this->related;
78
    }
79
80
    /**
81
     * Get the results of the relationship.
82
     */
83
    abstract public function getResults();
84
}
85