Completed
Branch feature/pre-split (8b986a)
by Anton
06:31
created

SingularRelation::getRelated()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 9
nop 0
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Entities\Relations;
8
9
use Spiral\Database\Exceptions\QueryException;
10
use Spiral\ORM\Exceptions\SelectorException;
11
use Spiral\ORM\ORMInterface;
12
use Spiral\ORM\Record;
13
use Spiral\ORM\RecordInterface;
14
15
/**
16
 * Provides ability to create cached instances of related data.
17
 */
18
abstract class SingularRelation extends AbstractRelation
19
{
20
    /**
21
     * Create placeholder model when relation is empty.
22
     */
23
    const CREATE_PLACEHOLDER = false;
24
25
    /**
26
     * @var RecordInterface
27
     */
28
    protected $instance;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function hasRelated(): bool
34
    {
35
        if (!$this->isLoaded()) {
36
            //Lazy loading our relation data
37
            $this->loadData();
38
        }
39
40
        return !empty($this->instance);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * Returns associated parent or NULL if none associated.
47
     */
48
    public function getRelated()
49
    {
50
        if ($this->instance instanceof RecordInterface) {
51
            return $this->instance;
52
        }
53
54
        if (!$this->isLoaded()) {
55
            //Lazy loading our relation data
56
            $this->loadData();
57
        }
58
59
        if (!empty($this->instance)) {
60
            return $this->instance;
61
        }
62
63
        if (empty($this->data)) {
64
            if (static::CREATE_PLACEHOLDER) {
65
                //Stub instance
66
                return $this->instance = $this->orm->make(
67
                    $this->class,
68
                    [],
69
                    ORMInterface::STATE_NEW
70
                );
71
            }
72
73
            return null;
74
        }
75
76
        //Create instance based on loaded data
77
        return $this->instance = $this->orm->make(
78
            $this->class,
79
            $this->data,
80
            ORMInterface::STATE_LOADED,
81
            true
82
        );
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @throws SelectorException
89
     * @throws QueryException
90
     */
91
    protected function loadData()
92
    {
93
        $this->loaded = true;
94
95
        $innerKey = $this->key(Record::INNER_KEY);
96
        if (empty($this->parent->getField($innerKey))) {
97
            //Unable to load
98
            return;
99
        }
100
101
        $this->data = $this->orm->selector($this->class)->where(
102
            $this->key(Record::OUTER_KEY),
103
            $this->parent->getField($innerKey)
0 ignored issues
show
Unused Code introduced by
The call to RecordSelector::where() has too many arguments starting with $this->parent->getField($innerKey).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
        )->fetchData();
105
106
        if (!empty($this->data[0])) {
107
            //Use first result
108
            $this->data = $this->data[0];
109
        }
110
    }
111
}