Completed
Push — master ( 1cce57...bc6f4b )
by Anton
02:24 queued 10s
created

AbstractRelation::isNotNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Cycle DataMapper ORM
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Cycle\ORM\Relation;
11
12
use Cycle\ORM\Exception\RelationException;
13
use Cycle\ORM\Heap\Node;
14
use Cycle\ORM\MapperInterface;
15
use Cycle\ORM\ORMInterface;
16
use Cycle\ORM\Promise\PromiseInterface;
17
use Cycle\ORM\Promise\ReferenceInterface;
18
use Cycle\ORM\Relation;
19
use Cycle\ORM\Schema;
20
use Cycle\ORM\Select\SourceInterface;
21
use Cycle\ORM\Select\SourceProviderInterface;
22
23
abstract class AbstractRelation implements RelationInterface
24
{
25
    use Traits\ContextTrait, Relation\Traits\NodeTrait;
26
27
    /** @var ORMInterface|SourceProviderInterface @internal */
28
    protected $orm;
29
30
    /** @var string */
31
    protected $name;
32
33
    /** @var string */
34
    protected $target;
35
36
    /** @var array */
37
    protected $schema;
38
39
    /** @var string */
40
    protected $innerKey;
41
42
    /** @var string */
43
    protected $outerKey;
44
45
    /**
46
     * @param ORMInterface $orm
47
     * @param string       $name
48
     * @param string       $target
49
     * @param array        $schema
50
     */
51
    public function __construct(ORMInterface $orm, string $name, string $target, array $schema)
52
    {
53
        $this->orm = $orm;
54
        $this->name = $name;
55
        $this->target = $target;
56
        $this->schema = $schema;
57
        $this->innerKey = $schema[Relation::INNER_KEY];
58
        $this->outerKey = $schema[Relation::OUTER_KEY];
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getName(): string
65
    {
66
        return $this->name;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function isCascade(): bool
73
    {
74
        return $this->schema[Relation::CASCADE] ?? false;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function init(Node $node, array $data): array
81
    {
82
        $item = $this->orm->make($this->target, $data, Node::MANAGED);
0 ignored issues
show
Bug introduced by
The method make() does not exist on Cycle\ORM\Select\SourceProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Cycle\ORM\Select\SourceProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        /** @scrutinizer ignore-call */ 
83
        $item = $this->orm->make($this->target, $data, Node::MANAGED);
Loading history...
83
84
        return [$item, $item];
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function extract($data)
91
    {
92
        return $data;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function __toString()
99
    {
100
        // this is incorrect class
101
        return sprintf("%s(%s)->%s", $this->name, get_class($this), $this->target);
102
    }
103
104
    /**
105
     * Indicates that relation can not be nullable.
106
     *
107
     * @return bool
108
     */
109
    protected function isNotNullable(): bool
110
    {
111
        if (array_key_exists(Relation::NULLABLE, $this->schema)) {
112
            return !$this->schema[Relation::NULLABLE];
113
        }
114
115
        return true;
116
    }
117
118
    /**
119
     * Get the source associated with the role.
120
     *
121
     * @param string|null $role
122
     * @return SourceInterface
123
     */
124
    protected function getSource(string $role = null): SourceInterface
125
    {
126
        return $this->orm->getSource($role ?? $this->target);
127
    }
128
129
    /**
130
     * Get the mapper associated with a role.
131
     *
132
     * @param string|null $role
133
     * @return MapperInterface
134
     */
135
    protected function getMapper(string $role = null): MapperInterface
136
    {
137
        return $this->orm->getMapper($role ?? $this->target);
0 ignored issues
show
Bug introduced by
The method getMapper() does not exist on Cycle\ORM\Select\SourceProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Cycle\ORM\Select\SourceProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        return $this->orm->/** @scrutinizer ignore-call */ getMapper($role ?? $this->target);
Loading history...
138
    }
139
140
    /**
141
     * @param Node   $node
142
     * @param string $field
143
     * @return string
144
     */
145
    protected function columnName(Node $node, string $field): string
146
    {
147
        return $this->orm->getSchema()->define($node->getRole(), Schema::COLUMNS)[$field] ?? $field;
0 ignored issues
show
Bug introduced by
The method getSchema() does not exist on Cycle\ORM\Select\SourceProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Cycle\ORM\Select\SourceProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        return $this->orm->/** @scrutinizer ignore-call */ getSchema()->define($node->getRole(), Schema::COLUMNS)[$field] ?? $field;
Loading history...
148
    }
149
150
    /**
151
     * Assert that given entity is allowed for the relation.
152
     *
153
     * @param Node $related
154
     *
155
     * @throws RelationException
156
     */
157
    protected function assertValid(Node $related)
158
    {
159
        if ($related->getRole() != $this->target) {
160
            throw new RelationException(sprintf("Unable to link %s, given `%s`", $this, $related->getRole()));
161
        }
162
    }
163
164
    /**
165
     * Resolve the reference to the object.
166
     *
167
     * @param ReferenceInterface $reference
168
     * @return mixed|null
169
     */
170
    protected function resolve(ReferenceInterface $reference)
171
    {
172
        if ($reference instanceof PromiseInterface) {
173
            return $reference->__resolve();
174
        }
175
176
        $scope = $reference->__scope();
177
        return $this->orm->get($reference->__role(), key($scope), current($scope), true);
0 ignored issues
show
Bug introduced by
The method get() does not exist on Cycle\ORM\Select\SourceProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Cycle\ORM\Select\SourceProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        return $this->orm->/** @scrutinizer ignore-call */ get($reference->__role(), key($scope), current($scope), true);
Loading history...
178
    }
179
}