Passed
Push — master ( b9a837...4518ef )
by Anton
01:49
created

HasOne   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A compute() 0 13 2
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 HasOne 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
        // not nullable by default
30
        Relation::NULLABLE  => false,
31
32
        // link to parent entity primary key by default
33
        Relation::INNER_KEY => '{source:primaryKey}',
34
35
        // default field name for inner key
36
        Relation::OUTER_KEY => '{source:role}_{innerKey}',
37
    ];
38
39
    /**
40
     * @param Registry $registry
41
     */
42
    public function compute(Registry $registry)
43
    {
44
        parent::compute($registry);
45
46
        // generate external field
47
        $target = $registry->getEntity($this->target);
48
49
        if (!$this->hasField($target, Relation::OUTER_KEY)) {
50
            // create target outer field
51
            $this->createField(
52
                $target,
53
                $this->getField($registry->getEntity($this->source), Relation::INNER_KEY),
54
                $this->options->get(Relation::OUTER_KEY)
55
            );
56
        }
57
    }
58
}