Completed
Branch feature/pre-split (ce4b6b)
by Anton
03:56
created

TablesTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sourceTable() 0 6 1
A targetTable() 0 9 2
getDefinition() 0 1 ?
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Schemas\Relations\Traits;
8
9
use Spiral\Database\Schemas\Prototypes\AbstractTable;
10
use Spiral\ORM\Exceptions\RelationSchemaException;
11
use Spiral\ORM\Schemas\Definitions\RelationDefinition;
12
use Spiral\ORM\Schemas\SchemaBuilder;
13
14
trait TablesTrait
15
{
16
    /**
17
     * Get table linked with source relation model.
18
     *
19
     * @param SchemaBuilder $builder
20
     *
21
     * @return AbstractTable
22
     *
23
     * @throws RelationSchemaException
24
     */
25
    protected function sourceTable(SchemaBuilder $builder): AbstractTable
26
    {
27
        $source = $this->getDefinition()->sourceContext();
28
29
        return $builder->requestTable($source->getTable(), $source->getDatabase());
30
    }
31
32
    /**
33
     * Get table linked with target relation model.
34
     *
35
     * @param SchemaBuilder $builder
36
     *
37
     * @return AbstractTable
38
     *
39
     * @throws RelationSchemaException
40
     */
41
    protected function targetTable(SchemaBuilder $builder): AbstractTable
42
    {
43
        $target = $this->getDefinition()->targetContext();
44
        if (empty($target)) {
45
            throw new RelationSchemaException("Unable to get target context in " . get_class($this));
46
        }
47
48
        return $builder->requestTable($target->getTable(), $target->getDatabase());
49
    }
50
51
    /**
52
     * @return RelationDefinition
53
     */
54
    abstract protected function getDefinition(): RelationDefinition;
55
}