Passed
Pull Request — 2.8.x (#8022)
by Benjamin
08:51
created

ClassMetadataBuilder   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 519
Duplicated Lines 0 %

Test Coverage

Coverage 76.12%

Importance

Changes 0
Metric Value
wmc 36
eloc 105
c 0
b 0
f 0
dl 0
loc 519
ccs 102
cts 134
cp 0.7612
rs 9.52

31 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getClassMetadata() 0 3 1
A setMappedSuperClass() 0 6 1
A setEmbeddable() 0 6 1
A createOneToOne() 0 9 1
A setChangeTrackingPolicyNotify() 0 5 1
A createOneToMany() 0 9 1
A setJoinedTableInheritance() 0 5 1
A addOwningManyToMany() 0 9 2
A createManyToOne() 0 9 1
A addInverseManyToMany() 0 6 1
A addNamedQuery() 0 10 1
A addUniqueConstraint() 0 9 2
A addLifecycleEvent() 0 5 1
A setCustomRepositoryClass() 0 5 1
A addOwningOneToOne() 0 9 2
A addField() 0 8 1
A setTable() 0 5 1
A addOneToMany() 0 6 1
A addEmbedded() 0 12 1
A setDiscriminatorColumn() 0 11 1
A addManyToOne() 0 9 2
A addIndex() 0 9 2
A createEmbedded() 0 8 1
A setReadOnly() 0 5 1
A addInverseOneToOne() 0 6 1
A setSingleTableInheritance() 0 5 1
A setChangeTrackingPolicyDeferredExplicit() 0 5 1
A createField() 0 7 1
A addDiscriminatorMapClass() 0 5 1
A createManyToMany() 0 9 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Mapping\Builder;
21
22
use Doctrine\ORM\Mapping\ClassMetadata;
23
use Doctrine\ORM\Mapping\ClassMetadataInfo;
24
25
/**
26
 * Builder Object for ClassMetadata
27
 *
28
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
29
 * @link        www.doctrine-project.com
30
 * @since       2.2
31
 * @author      Benjamin Eberlei <[email protected]>
32
 * @author      Guilherme Blanco <[email protected]>
33
 */
34
class ClassMetadataBuilder
35
{
36
    /**
37
     * @var \Doctrine\ORM\Mapping\ClassMetadataInfo
38
     */
39
    private $cm;
40
41
    /**
42
     * @param \Doctrine\ORM\Mapping\ClassMetadataInfo $cm
43
     */
44 38
    public function __construct(ClassMetadataInfo $cm)
45
    {
46 38
        $this->cm = $cm;
47 38
    }
48
49
    /**
50
     * @return ClassMetadata
51
     */
52 20
    public function getClassMetadata()
53
    {
54 20
        return $this->cm;
55
    }
56
57
    /**
58
     * Marks the class as mapped superclass.
59
     *
60
     * @return ClassMetadataBuilder
61
     */
62 1
    public function setMappedSuperClass()
63
    {
64 1
        $this->cm->isMappedSuperclass = true;
65 1
        $this->cm->isEmbeddedClass = false;
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * Marks the class as embeddable.
72
     *
73
     * @return ClassMetadataBuilder
74
     */
75 1
    public function setEmbeddable()
76
    {
77 1
        $this->cm->isEmbeddedClass = true;
78 1
        $this->cm->isMappedSuperclass = false;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Adds and embedded class
85
     *
86
     * @param string      $fieldName
87
     * @param string      $class
88
     * @param string|null $columnPrefix
89
     *
90
     * @return $this
91
     */
92 2
    public function addEmbedded($fieldName, $class, $columnPrefix = null, bool $nullable = false)
93
    {
94 2
        $this->cm->mapEmbedded(
95
            [
96 2
                'fieldName'    => $fieldName,
97 2
                'class'        => $class,
98 2
                'columnPrefix' => $columnPrefix,
99 2
                'nullable'     => $nullable,
100
            ]
101
        );
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * Sets custom Repository class name.
108
     *
109
     * @param string $repositoryClassName
110
     *
111
     * @return ClassMetadataBuilder
112
     */
113 1
    public function setCustomRepositoryClass($repositoryClassName)
114
    {
115 1
        $this->cm->setCustomRepositoryClass($repositoryClassName);
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * Marks class read only.
122
     *
123
     * @return ClassMetadataBuilder
124
     */
125 1
    public function setReadOnly()
126
    {
127 1
        $this->cm->markReadOnly();
128
129 1
        return $this;
130
    }
131
132
    /**
133
     * Sets the table name.
134
     *
135
     * @param string $name
136
     *
137
     * @return ClassMetadataBuilder
138
     */
139 2
    public function setTable($name)
140
    {
141 2
        $this->cm->setPrimaryTable(['name' => $name]);
142
143 2
        return $this;
144
    }
145
146
    /**
147
     * Adds Index.
148
     *
149
     * @param array  $columns
150
     * @param string $name
151
     *
152
     * @return ClassMetadataBuilder
153
     */
154 2
    public function addIndex(array $columns, $name)
155
    {
156 2
        if (!isset($this->cm->table['indexes'])) {
157 2
            $this->cm->table['indexes'] = [];
158
        }
159
160 2
        $this->cm->table['indexes'][$name] = ['columns' => $columns];
161
162 2
        return $this;
163
    }
164
165
    /**
166
     * Adds Unique Constraint.
167
     *
168
     * @param array  $columns
169
     * @param string $name
170
     *
171
     * @return ClassMetadataBuilder
172
     */
173 2
    public function addUniqueConstraint(array $columns, $name)
174
    {
175 2
        if ( ! isset($this->cm->table['uniqueConstraints'])) {
176 2
            $this->cm->table['uniqueConstraints'] = [];
177
        }
178
179 2
        $this->cm->table['uniqueConstraints'][$name] = ['columns' => $columns];
180
181 2
        return $this;
182
    }
183
184
    /**
185
     * Adds named query.
186
     *
187
     * @param string $name
188
     * @param string $dqlQuery
189
     *
190
     * @return ClassMetadataBuilder
191
     */
192
    public function addNamedQuery($name, $dqlQuery)
193
    {
194
        $this->cm->addNamedQuery(
195
            [
196
                'name' => $name,
197
                'query' => $dqlQuery,
198
            ]
199
        );
200
201
        return $this;
202
    }
203
204
    /**
205
     * Sets class as root of a joined table inheritance hierarchy.
206
     *
207
     * @return ClassMetadataBuilder
208
     */
209 1
    public function setJoinedTableInheritance()
210
    {
211 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
212
213 1
        return $this;
214
    }
215
216
    /**
217
     * Sets class as root of a single table inheritance hierarchy.
218
     *
219
     * @return ClassMetadataBuilder
220
     */
221 1
    public function setSingleTableInheritance()
222
    {
223 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * Sets the discriminator column details.
230
     *
231
     * @param string $name
232
     * @param string $type
233
     * @param int    $length
234
     *
235
     * @return ClassMetadataBuilder
236
     */
237 1
    public function setDiscriminatorColumn($name, $type = 'string', $length = 255)
238
    {
239 1
        $this->cm->setDiscriminatorColumn(
240
            [
241 1
                'name' => $name,
242 1
                'type' => $type,
243 1
                'length' => $length,
244
            ]
245
        );
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * Adds a subclass to this inheritance hierarchy.
252
     *
253
     * @param string $name
254
     * @param string $class
255
     *
256
     * @return ClassMetadataBuilder
257
     */
258 1
    public function addDiscriminatorMapClass($name, $class)
259
    {
260 1
        $this->cm->addDiscriminatorMapClass($name, $class);
261
262 1
        return $this;
263
    }
264
265
    /**
266
     * Sets deferred explicit change tracking policy.
267
     *
268
     * @return ClassMetadataBuilder
269
     */
270 1
    public function setChangeTrackingPolicyDeferredExplicit()
271
    {
272 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
273
274 1
        return $this;
275
    }
276
277
    /**
278
     * Sets notify change tracking policy.
279
     *
280
     * @return ClassMetadataBuilder
281
     */
282 1
    public function setChangeTrackingPolicyNotify()
283
    {
284 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY);
285
286 1
        return $this;
287
    }
288
289
    /**
290
     * Adds lifecycle event.
291
     *
292
     * @param string $methodName
293
     * @param string $event
294
     *
295
     * @return ClassMetadataBuilder
296
     */
297 1
    public function addLifecycleEvent($methodName, $event)
298
    {
299 1
        $this->cm->addLifecycleCallback($methodName, $event);
300
301 1
        return $this;
302
    }
303
304
    /**
305
     * Adds Field.
306
     *
307
     * @param string $name
308
     * @param string $type
309
     * @param array  $mapping
310
     *
311
     * @return ClassMetadataBuilder
312
     */
313 1
    public function addField($name, $type, array $mapping = [])
314
    {
315 1
        $mapping['fieldName'] = $name;
316 1
        $mapping['type'] = $type;
317
318 1
        $this->cm->mapField($mapping);
319
320 1
        return $this;
321
    }
322
323
    /**
324
     * Creates a field builder.
325
     *
326
     * @param string $name
327
     * @param string $type
328
     *
329
     * @return FieldBuilder
330
     */
331 5
    public function createField($name, $type)
332
    {
333 5
        return new FieldBuilder(
334 5
            $this,
335
            [
336 5
                'fieldName' => $name,
337 5
                'type'      => $type
338
            ]
339
        );
340
    }
341
342
    /**
343
     * Creates an embedded builder.
344
     *
345
     * @param string $fieldName
346
     * @param string $class
347
     *
348
     * @return EmbeddedBuilder
349
     */
350 2
    public function createEmbedded($fieldName, $class)
351
    {
352 2
        return new EmbeddedBuilder(
353 2
            $this,
354
            [
355 2
                'fieldName'    => $fieldName,
356 2
                'class'        => $class,
357
                'columnPrefix' => null
358
            ]
359
        );
360
    }
361
362
    /**
363
     * Adds a simple many to one association, optionally with the inversed by field.
364
     *
365
     * @param string      $name
366
     * @param string      $targetEntity
367
     * @param string|null $inversedBy
368
     *
369
     * @return ClassMetadataBuilder
370
     */
371
    public function addManyToOne($name, $targetEntity, $inversedBy = null)
372
    {
373
        $builder = $this->createManyToOne($name, $targetEntity);
374
375
        if ($inversedBy) {
376
            $builder->inversedBy($inversedBy);
377
        }
378
379
        return $builder->build();
380
    }
381
382
    /**
383
     * Creates a ManyToOne Association Builder.
384
     *
385
     * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
386
     *
387
     * @param string $name
388
     * @param string $targetEntity
389
     *
390
     * @return AssociationBuilder
391
     */
392 3
    public function createManyToOne($name, $targetEntity)
393
    {
394 3
        return new AssociationBuilder(
395 3
            $this,
396
            [
397 3
                'fieldName'    => $name,
398 3
                'targetEntity' => $targetEntity
399
            ],
400 3
            ClassMetadata::MANY_TO_ONE
401
        );
402
    }
403
404
    /**
405
     * Creates a OneToOne Association Builder.
406
     *
407
     * @param string $name
408
     * @param string $targetEntity
409
     *
410
     * @return AssociationBuilder
411
     */
412 4
    public function createOneToOne($name, $targetEntity)
413
    {
414 4
        return new AssociationBuilder(
415 4
            $this,
416
            [
417 4
                'fieldName'    => $name,
418 4
                'targetEntity' => $targetEntity
419
            ],
420 4
            ClassMetadata::ONE_TO_ONE
421
        );
422
    }
423
424
    /**
425
     * Adds simple inverse one-to-one association.
426
     *
427
     * @param string $name
428
     * @param string $targetEntity
429
     * @param string $mappedBy
430
     *
431
     * @return ClassMetadataBuilder
432
     */
433
    public function addInverseOneToOne($name, $targetEntity, $mappedBy)
434
    {
435
        $builder = $this->createOneToOne($name, $targetEntity);
436
        $builder->mappedBy($mappedBy);
437
438
        return $builder->build();
439
    }
440
441
    /**
442
     * Adds simple owning one-to-one association.
443
     *
444
     * @param string      $name
445
     * @param string      $targetEntity
446
     * @param string|null $inversedBy
447
     *
448
     * @return ClassMetadataBuilder
449
     */
450
    public function addOwningOneToOne($name, $targetEntity, $inversedBy = null)
451
    {
452
        $builder = $this->createOneToOne($name, $targetEntity);
453
454
        if ($inversedBy) {
455
            $builder->inversedBy($inversedBy);
456
        }
457
458
        return $builder->build();
459
    }
460
461
    /**
462
     * Creates a ManyToMany Association Builder.
463
     *
464
     * @param string $name
465
     * @param string $targetEntity
466
     *
467
     * @return ManyToManyAssociationBuilder
468
     */
469 3
    public function createManyToMany($name, $targetEntity)
470
    {
471 3
        return new ManyToManyAssociationBuilder(
472 3
            $this,
473
            [
474 3
                'fieldName'    => $name,
475 3
                'targetEntity' => $targetEntity
476
            ],
477 3
            ClassMetadata::MANY_TO_MANY
478
        );
479
    }
480
481
    /**
482
     * Adds a simple owning many to many association.
483
     *
484
     * @param string      $name
485
     * @param string      $targetEntity
486
     * @param string|null $inversedBy
487
     *
488
     * @return ClassMetadataBuilder
489
     */
490
    public function addOwningManyToMany($name, $targetEntity, $inversedBy = null)
491
    {
492
        $builder = $this->createManyToMany($name, $targetEntity);
493
494
        if ($inversedBy) {
495
            $builder->inversedBy($inversedBy);
496
        }
497
498
        return $builder->build();
499
    }
500
501
    /**
502
     * Adds a simple inverse many to many association.
503
     *
504
     * @param string $name
505
     * @param string $targetEntity
506
     * @param string $mappedBy
507
     *
508
     * @return ClassMetadataBuilder
509
     */
510
    public function addInverseManyToMany($name, $targetEntity, $mappedBy)
511
    {
512
        $builder = $this->createManyToMany($name, $targetEntity);
513
        $builder->mappedBy($mappedBy);
514
515
        return $builder->build();
516
    }
517
518
    /**
519
     * Creates a one to many association builder.
520
     *
521
     * @param string $name
522
     * @param string $targetEntity
523
     *
524
     * @return OneToManyAssociationBuilder
525
     */
526 3
    public function createOneToMany($name, $targetEntity)
527
    {
528 3
        return new OneToManyAssociationBuilder(
529 3
            $this,
530
            [
531 3
                'fieldName'    => $name,
532 3
                'targetEntity' => $targetEntity
533
            ],
534 3
            ClassMetadata::ONE_TO_MANY
535
        );
536
    }
537
538
    /**
539
     * Adds simple OneToMany association.
540
     *
541
     * @param string $name
542
     * @param string $targetEntity
543
     * @param string $mappedBy
544
     *
545
     * @return ClassMetadataBuilder
546
     */
547
    public function addOneToMany($name, $targetEntity, $mappedBy)
548
    {
549
        $builder = $this->createOneToMany($name, $targetEntity);
550
        $builder->mappedBy($mappedBy);
551
552
        return $builder->build();
553
    }
554
}
555