Passed
Pull Request — master (#74)
by
unknown
03:56
created

AbstractRelation::isNullable()   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
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Relation;
13
14
use Cycle\ORM\Exception\RelationException;
15
use Cycle\ORM\Heap\Node;
16
use Cycle\ORM\MapperInterface;
17
use Cycle\ORM\ORMInterface;
18
use Cycle\ORM\Promise\PromiseInterface;
19
use Cycle\ORM\Promise\ReferenceInterface;
20
use Cycle\ORM\Relation;
21
use Cycle\ORM\Schema;
22
use Cycle\ORM\Select\SourceInterface;
23
use Cycle\ORM\Select\SourceProviderInterface;
24
25
abstract class AbstractRelation implements RelationInterface
26
{
27
    use Traits\ContextTrait;
28
    use Relation\Traits\NodeTrait;
29
30
    /** @var ORMInterface|SourceProviderInterface @internal */
31
    protected $orm;
32
33
    /** @var string */
34
    protected $name;
35
36
    /** @var string */
37
    protected $target;
38
39
    /** @var array */
40
    protected $schema;
41
42
    /** @var string */
43
    protected $innerKey;
44
45
    /** @var string */
46
    protected $outerKey;
47
48
    /**
49
     * @param ORMInterface $orm
50
     * @param string       $name
51
     * @param string       $target
52
     * @param array        $schema
53
     */
54
    public function __construct(ORMInterface $orm, string $name, string $target, array $schema)
55
    {
56
        $this->orm = $orm;
57
        $this->name = $name;
58
        $this->target = $target;
59
        $this->schema = $schema;
60
        $this->innerKey = $schema[Relation::INNER_KEY];
61
        $this->outerKey = $schema[Relation::OUTER_KEY];
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function __toString()
68
    {
69
        // this is incorrect class
70
        return sprintf('%s(%s)->%s', $this->name, get_class($this), $this->target);
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function getName(): string
77
    {
78
        return $this->name;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function isCascade(): bool
85
    {
86
        return $this->schema[Relation::CASCADE] ?? false;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function init(Node $node, array $data): array
93
    {
94
        $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

94
        /** @scrutinizer ignore-call */ 
95
        $item = $this->orm->make($this->target, $data, Node::MANAGED);
Loading history...
95
96
        return [$item, $item];
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function extract($data)
103
    {
104
        return $data;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    protected function isNullable(): bool
111
    {
112
        if (array_key_exists(Relation::NULLABLE, $this->schema)) {
113
            return $this->schema[Relation::NULLABLE];
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Get the source associated with the role.
121
     *
122
     * @param string|null $role
123
     * @return SourceInterface
124
     */
125
    protected function getSource(string $role = null): SourceInterface
126
    {
127
        return $this->orm->getSource($role ?? $this->target);
128
    }
129
130
    /**
131
     * Get the mapper associated with a role.
132
     *
133
     * @param string|null $role
134
     * @return MapperInterface
135
     */
136
    protected function getMapper(string $role = null): MapperInterface
137
    {
138
        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

138
        return $this->orm->/** @scrutinizer ignore-call */ getMapper($role ?? $this->target);
Loading history...
139
    }
140
141
    /**
142
     * @param Node   $node
143
     * @param string $field
144
     * @return string
145
     */
146
    protected function columnName(Node $node, string $field): string
147
    {
148
        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

148
        return $this->orm->/** @scrutinizer ignore-call */ getSchema()->define($node->getRole(), Schema::COLUMNS)[$field] ?? $field;
Loading history...
149
    }
150
151
    /**
152
     * Assert that given entity is allowed for the relation.
153
     *
154
     * @param Node $related
155
     *
156
     * @throws RelationException
157
     */
158
    protected function assertValid(Node $related): void
159
    {
160
        if ($related->getRole() != $this->target) {
161
            throw new RelationException(sprintf('Unable to link %s, given `%s`', $this, $related->getRole()));
162
        }
163
    }
164
165
    /**
166
     * Resolve the reference to the object.
167
     *
168
     * @param ReferenceInterface $reference
169
     * @return mixed|null
170
     */
171
    protected function resolve(ReferenceInterface $reference)
172
    {
173
        if ($reference instanceof PromiseInterface) {
174
            return $reference->__resolve();
175
        }
176
177
        return $this->orm->get($reference->__role(), $reference->__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(), $reference->__scope(), true);
Loading history...
178
    }
179
}
180