Passed
Push — 2.x ( 0b5227...cb81b7 )
by butschster
16:17
created

RelationConfig::getLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Config;
6
7
use Cycle\ORM\Exception\ConfigException;
8
use Cycle\ORM\Relation;
9
use Cycle\ORM\Select;
10
use JetBrains\PhpStorm\Pure;
11
use Spiral\Core\Container\Autowire;
12
use Spiral\Core\InjectableConfig;
13
14
final class RelationConfig extends InjectableConfig
15
{
16
    public const LOADER = 'loader';
17
    public const RELATION = 'relation';
18
    public const SCHEMA = 'schema';
19
20
    /** @var array */
21
    protected $config = [];
22
23 4082
    public function getLoader(int|string $type): Autowire
24
    {
25 4082
        if (!isset($this->config[$type][self::LOADER])) {
26
            throw new ConfigException("Unable to get relation loader `{$type}`.");
27
        }
28
29 4082
        return new Autowire($this->config[$type][self::LOADER]);
30
    }
31
32 5364
    public function getRelation(int|string $type): Autowire
33
    {
34 5364
        if (!isset($this->config[$type][self::RELATION])) {
35
            throw new ConfigException("Unable to get relation `{$type}`.");
36
        }
37
38 5364
        return new Autowire($this->config[$type][self::RELATION]);
39
    }
40
41 7414
    #[Pure]
42
    public static function getDefault(): self
43
    {
44 7414
        return new self([
45
            Relation::EMBEDDED => [
46
                self::LOADER => Select\Loader\EmbeddedLoader::class,
47
                self::RELATION => Relation\Embedded::class,
48
            ],
49
            Relation::HAS_ONE => [
50
                self::LOADER => Select\Loader\HasOneLoader::class,
51
                self::RELATION => Relation\HasOne::class,
52
            ],
53
            Relation::BELONGS_TO => [
54
                self::LOADER => Select\Loader\BelongsToLoader::class,
55
                self::RELATION => Relation\BelongsTo::class,
56
            ],
57
            Relation::REFERS_TO => [
58
                self::LOADER => Select\Loader\BelongsToLoader::class,
59
                self::RELATION => Relation\RefersTo::class,
60
            ],
61
            Relation::HAS_MANY => [
62
                self::LOADER => Select\Loader\HasManyLoader::class,
63
                self::RELATION => Relation\HasMany::class,
64
            ],
65
            Relation::MANY_TO_MANY => [
66
                self::LOADER => Select\Loader\ManyToManyLoader::class,
67
                self::RELATION => Relation\ManyToMany::class,
68
            ],
69
            Relation::MORPHED_HAS_ONE => [
70
                self::LOADER => Select\Loader\Morphed\MorphedHasOneLoader::class,
71
                self::RELATION => Relation\Morphed\MorphedHasOne::class,
72
            ],
73
            Relation::MORPHED_HAS_MANY => [
74
                self::LOADER => Select\Loader\Morphed\MorphedHasManyLoader::class,
75
                self::RELATION => Relation\Morphed\MorphedHasMany::class,
76
            ],
77
            Relation::BELONGS_TO_MORPHED => [
78
                self::RELATION => Relation\Morphed\BelongsToMorphed::class,
79
            ],
80
        ]);
81
    }
82
}
83