Passed
Pull Request — 2.x (#517)
by Aleksei
19:15
created

HasOneLoader::withContext()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 3
nc 3
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 5
rs 9.6111
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select\Loader;
6
7
use Cycle\ORM\Parser\AbstractNode;
8
use Cycle\ORM\Parser\SingularNode;
9
use Cycle\ORM\Relation;
10
use Cycle\ORM\SchemaInterface;
11
use Cycle\ORM\Select\JoinableLoader;
12
use Cycle\ORM\Select\LoaderInterface;
13
use Cycle\ORM\Select\Traits\JoinOneTableTrait;
14
use Cycle\ORM\Select\Traits\WhereTrait;
15
use Cycle\Database\Query\SelectQuery;
16
17
/**
18
 * Dedicated to load HAS_ONE relations, by default loader will prefer to join data into query.
19
 * Loader support MORPH_KEY.
20
 *
21
 * Please note that OUTER and INNER keys defined from perspective of parent (reversed for our
22
 * purposes).
23
 *
24
 * @internal
25
 */
26
class HasOneLoader extends JoinableLoader
27
{
28
    use JoinOneTableTrait;
29
    use WhereTrait;
30
31
    /**
32
     * Default set of relation options. Child implementation might defined their of default options.
33
     */
34
    protected array $options = [
35
        'load' => false,
36
        'scope' => true,
37
        'method' => self::INLOAD,
38
        'minify' => true,
39
        'as' => null,
40
        'using' => null,
41
        'where' => null,
42
    ];
43 722
44
    public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery
45 722
    {
46
        if ($this->options['using'] !== null) {
47
            // use pre-defined query
48
            return parent::configureQuery($query, $outerKeys);
49
        }
50 722
51
        $this->configureParentQuery($query, $outerKeys);
52
53 722
        // user specified WHERE conditions
54
        $this->setWhere(
55 722
            $query,
56 722
            $this->isJoined() ? 'onWhere' : 'where',
57
            $this->options['where'] ?? $this->schema[Relation::WHERE] ?? [],
58
        );
59 722
60
        return parent::configureQuery($query);
61
    }
62 634
63
    public function withContext(LoaderInterface $parent, array $options = []): static
64 634
    {
65 634
        if (isset($options['method']) && ($options['method'] === self::INLOAD || $options['method'] === true)) {
66 634
            $options['method'] = ($this->schema[Relation::NULLABLE] ?? false) ? self::LEFT_JOIN : self::JOIN;
67 634
        }
68 634
69
        return parent::withContext($parent, $options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::withContext($parent, $options) returns the type Cycle\ORM\Select\JoinableLoader which includes types incompatible with the type-hinted return Cycle\ORM\Select\Loader\HasOneLoader.
Loading history...
70
    }
71
72
    protected function initNode(): AbstractNode
73
    {
74
        return new SingularNode(
75
            $this->columnNames(),
76
            (array) $this->define(SchemaInterface::PRIMARY_KEY),
77
            (array) $this->schema[Relation::OUTER_KEY],
78
            (array) $this->schema[Relation::INNER_KEY],
79
        );
80
    }
81
}
82