Completed
Push — 2.x ( 86616a...8ac5a6 )
by Aleksei
14s queued 11s
created

ParentLoader::generateSubclassLoaders()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select\Loader;
6
7
use Cycle\Database\Query\SelectQuery;
8
use Cycle\ORM\FactoryInterface;
9
use Cycle\ORM\Parser\AbstractNode;
10
use Cycle\ORM\Parser\ParentMergeNode;
11
use Cycle\ORM\Service\SourceProviderInterface;
12
use Cycle\ORM\Relation;
13
use Cycle\ORM\SchemaInterface;
14
use Cycle\ORM\Select\JoinableLoader;
15
use Cycle\ORM\Select\Traits\JoinOneTableTrait;
16
17
/**
18
 * Load parent data.
19
 *
20
 * @internal
21
 */
22
class ParentLoader extends JoinableLoader
23
{
24
    use JoinOneTableTrait;
25
26
    /**
27
     * Default set of relation options. Child implementation might defined their of default options.
28
     */
29
    protected array $options = [
30
        'load' => true,
31
        'constrain' => true,
32
        'method' => self::INLOAD,
33
        'minify' => true,
34
        'as' => null,
35
        'using' => null,
36
    ];
37
38 744
    public function __construct(
39
        SchemaInterface $ormSchema,
40
        SourceProviderInterface $sourceProvider,
41
        FactoryInterface $factory,
42
        string $role,
43
        string $target,
44
    ) {
45 744
        $schemaArray = [
46 744
            Relation::INNER_KEY => $ormSchema->define($role, SchemaInterface::PRIMARY_KEY),
47 744
            Relation::OUTER_KEY => $ormSchema->define($role, SchemaInterface::PARENT_KEY)
48 744
                ?? $ormSchema->define($target, SchemaInterface::PRIMARY_KEY),
49
        ];
50 744
        $this->options['as'] ??= $target;
51 744
        parent::__construct($ormSchema, $sourceProvider, $factory, $role, $target, $schemaArray);
52
    }
53
54 744
    public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery
55
    {
56 744
        if ($this->options['using'] !== null) {
57
            // use pre-defined query
58
            return parent::configureQuery($query, $outerKeys);
59 744
        }
60
61 744
        $this->configureParentQuery($query, $outerKeys);
62
63
        return parent::configureQuery($query);
64
    }
65
66 744
    protected function generateSubclassLoaders(): iterable
67
    {
68 744
        return [];
69
    }
70
71 744
    protected function getJoinMethod(): string
72
    {
73 744
        return 'INNER';
74
    }
75
76 744
    protected function initNode(): AbstractNode
77
    {
78 744
        return new ParentMergeNode(
79 744
            $this->target,
80 744
            $this->columnNames(),
81 744
            (array) $this->define(SchemaInterface::PRIMARY_KEY),
82 744
            (array) $this->schema[Relation::OUTER_KEY],
83 744
            (array) $this->schema[Relation::INNER_KEY],
84
        );
85
    }
86
}
87