Passed
Push — dev_2x ( 241dab...1e248d )
by Adrian
01:59
created

ManyToMany::addAggregate()   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 2
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 $throughTable;
18
19
    protected $throughTableAlias;
20
21
    protected $throughGuards = [];
22
23
    protected $throughColumns = [];
24
25
    protected $throughNativeColumn = '';
26
27
    protected $throughForeignColumn = '';
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
        ];
56
    }
57
58
    protected function maybeSetAdditionalProperties()
59
    {
60
        if ( ! $this->mapper || ! $this->foreignMapper) {
61
            return;
62
        }
63
64
        if ( ! $this->throughTable) {
65
            $tablePrefix = $this->mapper->getTableAlias() ?
66
                str_replace($this->mapper->getTable(), '', $this->mapper->getTableAlias())
67
                : '';
68
69
            $tables = [$this->mapper->getTableAlias() ?: $this->mapper->getTable(), $this->foreignMapper];
70
            sort($tables);
71
72
            $this->throughTable = $tablePrefix . implode('_', $tables);
73
74
            if ($tablePrefix) {
75
                $this->throughTableAlias = implode('_', $tables);
76
            }
77
        }
78
79
        if ( ! $this->throughNativeColumn) {
80
            $this->throughNativeColumn = Inflector::singularize($this->mapper->getName()) . '_id';
81
        }
82
83
        if ( ! $this->throughForeignColumn) {
84
            $this->throughForeignColumn = Inflector::singularize($this->foreignMapper) . '_id';
85
        }
86
    }
87
88
    public function addAggregate($name, $aggregate)
89
    {
90
        $this->aggregates[$name] = $aggregate;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    public function getThroughTable()
99
    {
100
        return $this->throughTable;
101
    }
102
103
    /**
104
     * @param mixed $throughTable
105
     *
106
     * @return ManyToMany
107
     */
108
    public function setThroughTable(string $throughTable)
109
    {
110
        $this->throughTable = $throughTable;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118
    public function getThroughTableAlias()
119
    {
120
        return $this->throughTableAlias;
121
    }
122
123
    /**
124
     * @param mixed $throughTableAlias
125
     *
126
     * @return ManyToMany
127
     */
128
    public function setThroughTableAlias($throughTableAlias)
129
    {
130
        $this->throughTableAlias = $throughTableAlias;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function getThroughGuards(): array
139
    {
140
        return $this->throughGuards;
141
    }
142
143
    /**
144
     * @param array $throughGuards
145
     *
146
     * @return ManyToMany
147
     */
148
    public function setThroughGuards(array $throughGuards): ManyToMany
149
    {
150
        $this->throughGuards = $throughGuards;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getThroughColumns(): array
159
    {
160
        return $this->throughColumns;
161
    }
162
163
    /**
164
     * Pairs of column name (from table) and attribute name (in the linked model)
165
     *
166
     * @param array $throughColumns
167
     *
168
     * @return ManyToMany
169
     */
170
    public function setThroughColumns(array $throughColumns): ManyToMany
171
    {
172
        $this->throughColumns = $throughColumns;
173
174
        $this->maybeSetAdditionalProperties();
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getThroughNativeColumn(): string
183
    {
184
        return $this->throughNativeColumn;
185
    }
186
187
    /**
188
     * @param string $throughNativeColumn
189
     *
190
     * @return ManyToMany
191
     */
192
    public function setThroughNativeColumn(string $throughNativeColumn): ManyToMany
193
    {
194
        $this->throughNativeColumn = $throughNativeColumn;
195
196
        return $this;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getThroughForeignColumn(): string
203
    {
204
        return $this->throughForeignColumn;
205
    }
206
207
    /**
208
     * @param string $throughForeignColumn
209
     *
210
     * @return ManyToMany
211
     */
212
    public function setThroughForeignColumn(string $throughForeignColumn): ManyToMany
213
    {
214
        $this->throughForeignColumn = $throughForeignColumn;
215
216
        return $this;
217
    }
218
219
220
}
221