ChainTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 41
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isChain() 0 3 1
A loadChain() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select\Traits;
6
7
use Cycle\ORM\Exception\LoaderException;
8
use Cycle\ORM\Select\AbstractLoader;
9
use Cycle\ORM\Select\LoaderInterface;
10
11
/**
12
 * @internal
13
 */
14
trait ChainTrait
15
{
16
    abstract public function loadRelation(
17
        string|LoaderInterface $relation,
18
        array $options,
19
        bool $join = false,
20
        bool $load = false,
21
    ): LoaderInterface;
22
23
    /**
24
     * Check if given relation points to the relation chain.
25
     */
26 4082
    protected function isChain(string $relation): bool
27
    {
28 4082
        return \str_contains($relation, '.');
29
    }
30
31
    /**
32
     * @param array $options Final loader options.
33
     * @param bool  $join    See loadRelation().
34
     *
35
     * @throws LoaderException When one of the elements can not be chained.
36
     *
37
     * @see joinRelation()
38
     * @see loadRelation()
39
     */
40 362
    protected function loadChain(string $chain, array $options, bool $join, bool $load): LoaderInterface
41
    {
42 362
        $position = \strpos($chain, '.');
43
44
        // chain of relations provided (relation.nestedRelation)
45 362
        $child = $this->loadRelation(\substr($chain, 0, $position), [], $join, $load);
46
47 362
        if (!$child instanceof AbstractLoader) {
48
            throw new LoaderException(
49
                \sprintf('Loader `%s` does not support chain relation loading.', $child::class),
50
            );
51
        }
52
53
        // load nested relation through chain (chainOptions prior to user options)
54 362
        return $child->loadRelation(\substr($chain, $position + 1), $options, $join, $load);
55
    }
56
}
57