Passed
Pull Request — 2.x (#418)
by Maxim
20:09
created

ParentLoader::calculateAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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