Passed
Push — dev_2x ( fa57a5...f00e1a )
by Adrian
01:48
created

ManyToMany::setPivotGuards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Blueprint\Relation;
5
6
use Sirius\Orm\Blueprint\Mapper;
7
use Sirius\Orm\Blueprint\Relation;
8
use Sirius\Orm\Helpers\Inflector;
9
use Sirius\Orm\Relation\RelationConfig;
10
11
class ManyToMany extends Relation
12
{
13
    protected $type = RelationConfig::TYPE_MANY_TO_MANY;
14
15
    protected $foreignKey = 'id';
16
17
    protected $pivotTable;
18
19
    protected $pivotTableAlias;
20
21
    protected $pivotGuards = [];
22
23
    protected $pivotColumns = [];
24
25
    protected $pivotNativeColumn = '';
26
27
    protected $pivotForeignColumn = '';
28
29
    protected $aggregates = [];
30
31
    public function setMapper(Mapper $mapper): Relation
32
    {
33
        $this->nativeKey = $mapper->getPrimaryKey();
34
        parent::setMapper($mapper);
35
        $this->maybeSetAdditionalProperties();
36
37
        return $this;
38
    }
39
40
    public function setForeignMapper($foreignMapper): Relation
41
    {
42
        parent::setForeignMapper($foreignMapper);
43
        $this->maybeSetAdditionalProperties();
44
45
        return $this;
46
    }
47
48
    public function getObservers(): array
49
    {
50
        $observer = $this->getObserver()->with($this);
0 ignored issues
show
Bug introduced by
The method with() does not exist on Sirius\Orm\CodeGenerator\Observer\Base. Since it exists in all sub-types, consider adding an abstract or default implementation to Sirius\Orm\CodeGenerator\Observer\Base. ( Ignorable by Annotation )

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

50
        $observer = $this->getObserver()->/** @scrutinizer ignore-call */ with($this);
Loading history...
51
52
        return [
53
            $this->getMapper()->getName() . '_base_entity' => [$observer],
54
            $this->getForeignMapper() . '_base_entity'     => [$observer],
55
            $this->getForeignMapper() . '_mapper_config'   => [$observer],
56
        ];
57
    }
58
59
    protected function maybeSetAdditionalProperties()
60
    {
61
        if (! $this->mapper || ! $this->foreignMapper) {
62
            return;
63
        }
64
65
        if (! $this->pivotTable) {
66
            $tablePrefix = $this->mapper->getTableAlias() ?
67
                str_replace($this->mapper->getTable(), '', $this->mapper->getTableAlias())
68
                : '';
69
70
            $tables = [$this->mapper->getTableAlias() ?: $this->mapper->getTable(), $this->foreignMapper];
71
            sort($tables);
72
73
            $this->pivotTable = $tablePrefix . implode('_', $tables);
74
75
            if ($tablePrefix) {
76
                $this->pivotTableAlias = implode('_', $tables);
77
            }
78
        }
79
80
        if (! $this->pivotNativeColumn) {
81
            $this->pivotNativeColumn = Inflector::singularize($this->mapper->getName()) . '_id';
82
        }
83
84
        if (! $this->pivotForeignColumn) {
85
            $this->pivotForeignColumn = Inflector::singularize($this->foreignMapper) . '_id';
86
        }
87
    }
88
89
    public function addAggregate($name, $aggregate)
90
    {
91
        $this->aggregates[$name] = $aggregate;
92
93
        return $this;
94
    }
95
96
    public function getAggregates(): array
97
    {
98
        return $this->aggregates;
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function getPivotTable()
105
    {
106
        return $this->pivotTable;
107
    }
108
109
    /**
110
     * @param mixed $pivotTable
111
     *
112
     * @return ManyToMany
113
     */
114
    public function setPivotTable(string $pivotTable)
115
    {
116
        $this->pivotTable = $pivotTable;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getPivotTableAlias()
125
    {
126
        return $this->pivotTableAlias;
127
    }
128
129
    /**
130
     * @param mixed $pivotTableAlias
131
     *
132
     * @return ManyToMany
133
     */
134
    public function setPivotTableAlias($pivotTableAlias)
135
    {
136
        $this->pivotTableAlias = $pivotTableAlias;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getPivotGuards(): array
145
    {
146
        return $this->pivotGuards;
147
    }
148
149
    /**
150
     * @param array $pivotGuards
151
     *
152
     * @return ManyToMany
153
     */
154
    public function setPivotGuards(array $pivotGuards): ManyToMany
155
    {
156
        $this->pivotGuards = $pivotGuards;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getPivotColumns(): array
165
    {
166
        return $this->pivotColumns;
167
    }
168
169
    /**
170
     * Pairs of column name (from table) and attribute name (in the linked model)
171
     *
172
     * @param array $pivotColumns
173
     *
174
     * @return ManyToMany
175
     */
176
    public function setPivotColumns(array $pivotColumns): ManyToMany
177
    {
178
        $this->pivotColumns = $pivotColumns;
179
180
        $this->maybeSetAdditionalProperties();
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getPivotNativeColumn(): string
189
    {
190
        return $this->pivotNativeColumn;
191
    }
192
193
    /**
194
     * @param string $pivotNativeColumn
195
     *
196
     * @return ManyToMany
197
     */
198
    public function setPivotNativeColumn(string $pivotNativeColumn): ManyToMany
199
    {
200
        $this->pivotNativeColumn = $pivotNativeColumn;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getPivotForeignColumn(): string
209
    {
210
        return $this->pivotForeignColumn;
211
    }
212
213
    /**
214
     * @param string $pivotForeignColumn
215
     *
216
     * @return ManyToMany
217
     */
218
    public function setPivotForeignColumn(string $pivotForeignColumn): ManyToMany
219
    {
220
        $this->pivotForeignColumn = $pivotForeignColumn;
221
222
        return $this;
223
    }
224
}
225