Passed
Push — master ( f6c6fd...6dedbf )
by Anton
03:07 queued 01:15
created

BelongsTo   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A compute() 0 12 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Relation;
11
12
use Cycle\ORM\Relation;
13
use Cycle\Schema\Registry;
14
use Cycle\Schema\Relation\Traits\FieldTrait;
15
16
class BelongsTo extends AbstractSchema
17
{
18
    use FieldTrait;
19
20
    protected const RELATION_TYPE = Relation::HAS_ONE;
21
22
    protected const OPTION_SCHEMA = [
23
        // save with parent
24
        Relation::CASCADE   => true,
25
26
        // use outer entity constrain by default
27
        Relation::CONSTRAIN => true,
28
29
        // nullable by default
30
        Relation::NULLABLE  => true,
31
32
        // link to parent entity primary key by default
33
        Relation::INNER_KEY => '{relation}_{outerKey}',
34
35
        // default field name for inner key
36
        Relation::OUTER_KEY => '{target:primaryKey}',
37
    ];
38
39
    /**
40
     * @param Registry $registry
41
     */
42
    public function compute(Registry $registry)
43
    {
44
        parent::compute($registry);
45
46
        $source = $registry->getEntity($this->source);
47
        $target = $registry->getEntity($this->target);
48
49
        // create target outer field
50
        $this->ensureField(
51
            $source,
52
            $this->options->get(Relation::INNER_KEY),
53
            $this->getField($target, Relation::OUTER_KEY)
54
        );
55
    }
56
}