Completed
Pull Request — master (#5586)
by Mihai
25:46 queued 05:14
created

ClassMetadataBuilder   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 528
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 73.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 37
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 528
ccs 95
cts 130
cp 0.7308
rs 8.6

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClassMetadata() 0 4 1
A setMappedSuperClass() 0 7 1
A setEmbeddable() 0 7 1
A addEmbedded() 0 10 1
A setCustomRepositoryClass() 0 6 1
A setCustomPersisterClass() 0 6 1
A setReadOnly() 0 6 1
A setTable() 0 6 1
A addIndex() 0 10 2
A addUniqueConstraint() 0 10 2
A addNamedQuery() 0 9 1
A setJoinedTableInheritance() 0 6 1
A setSingleTableInheritance() 0 6 1
A setDiscriminatorColumn() 0 10 1
A addDiscriminatorMapClass() 0 6 1
A setChangeTrackingPolicyDeferredExplicit() 0 6 1
A setChangeTrackingPolicyNotify() 0 6 1
A addLifecycleEvent() 0 6 1
A addField() 0 9 1
A createField() 0 10 1
A createEmbedded() 0 11 1
A addManyToOne() 0 10 2
A createManyToOne() 0 11 1
A createOneToOne() 0 11 1
A addInverseOneToOne() 0 7 1
A addOwningOneToOne() 0 10 2
A createManyToMany() 0 11 1
A addOwningManyToMany() 0 10 2
A addInverseManyToMany() 0 7 1
A createOneToMany() 0 11 1
A addOneToMany() 0 7 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)
93
    {
94 2
        $this->cm->mapEmbedded(array(
95 2
            'fieldName'    => $fieldName,
96 2
            'class'        => $class,
97 2
            'columnPrefix' => $columnPrefix
98
        ));
99
100 2
        return $this;
101
    }
102
103
    /**
104
     * Sets custom Repository class name.
105
     *
106
     * @param string $repositoryClassName
107
     *
108
     * @return ClassMetadataBuilder
109
     */
110 1
    public function setCustomRepositoryClass($repositoryClassName)
111
    {
112 1
        $this->cm->setCustomRepositoryClass($repositoryClassName);
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * Sets custom Persister class name.
119
     *
120
     * @param string $persisterClassName
121
     *
122
     * @return ClassMetadataBuilder
123
     */
124
    public function setCustomPersisterClass($persisterClassName)
125
    {
126
        $this->cm->setCustomPersisterClass($persisterClassName);
127
128
        return $this;
129
    }
130
131
    /**
132
     * Marks class read only.
133
     *
134
     * @return ClassMetadataBuilder
135
     */
136 1
    public function setReadOnly()
137
    {
138 1
        $this->cm->markReadOnly();
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * Sets the table name.
145
     *
146
     * @param string $name
147
     *
148
     * @return ClassMetadataBuilder
149
     */
150 2
    public function setTable($name)
151
    {
152 2
        $this->cm->setPrimaryTable(array('name' => $name));
153
154 2
        return $this;
155
    }
156
157
    /**
158
     * Adds Index.
159
     *
160
     * @param array  $columns
161
     * @param string $name
162
     *
163
     * @return ClassMetadataBuilder
164
     */
165 2
    public function addIndex(array $columns, $name)
166
    {
167 2
        if (!isset($this->cm->table['indexes'])) {
168 2
            $this->cm->table['indexes'] = array();
169
        }
170
171 2
        $this->cm->table['indexes'][$name] = array('columns' => $columns);
172
173 2
        return $this;
174
    }
175
176
    /**
177
     * Adds Unique Constraint.
178
     *
179
     * @param array  $columns
180
     * @param string $name
181
     *
182
     * @return ClassMetadataBuilder
183
     */
184 2
    public function addUniqueConstraint(array $columns, $name)
185
    {
186 2
        if ( ! isset($this->cm->table['uniqueConstraints'])) {
187 2
            $this->cm->table['uniqueConstraints'] = array();
188
        }
189
190 2
        $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns);
191
192 2
        return $this;
193
    }
194
195
    /**
196
     * Adds named query.
197
     *
198
     * @param string $name
199
     * @param string $dqlQuery
200
     *
201
     * @return ClassMetadataBuilder
202
     */
203
    public function addNamedQuery($name, $dqlQuery)
204
    {
205
        $this->cm->addNamedQuery(array(
206
            'name' => $name,
207
            'query' => $dqlQuery,
208
        ));
209
210
        return $this;
211
    }
212
213
    /**
214
     * Sets class as root of a joined table inheritance hierarchy.
215
     *
216
     * @return ClassMetadataBuilder
217
     */
218 1
    public function setJoinedTableInheritance()
219
    {
220 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
221
222 1
        return $this;
223
    }
224
225
    /**
226
     * Sets class as root of a single table inheritance hierarchy.
227
     *
228
     * @return ClassMetadataBuilder
229
     */
230 1
    public function setSingleTableInheritance()
231
    {
232 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * Sets the discriminator column details.
239
     *
240
     * @param string $name
241
     * @param string $type
242
     * @param int    $length
243
     *
244
     * @return ClassMetadataBuilder
245
     */
246 1
    public function setDiscriminatorColumn($name, $type = 'string', $length = 255)
247
    {
248 1
        $this->cm->setDiscriminatorColumn(array(
249 1
            'name' => $name,
250 1
            'type' => $type,
251 1
            'length' => $length,
252
        ));
253
254 1
        return $this;
255
    }
256
257
    /**
258
     * Adds a subclass to this inheritance hierarchy.
259
     *
260
     * @param string $name
261
     * @param string $class
262
     *
263
     * @return ClassMetadataBuilder
264
     */
265 1
    public function addDiscriminatorMapClass($name, $class)
266
    {
267 1
        $this->cm->addDiscriminatorMapClass($name, $class);
268
269 1
        return $this;
270
    }
271
272
    /**
273
     * Sets deferred explicit change tracking policy.
274
     *
275
     * @return ClassMetadataBuilder
276
     */
277 1
    public function setChangeTrackingPolicyDeferredExplicit()
278
    {
279 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
280
281 1
        return $this;
282
    }
283
284
    /**
285
     * Sets notify change tracking policy.
286
     *
287
     * @return ClassMetadataBuilder
288
     */
289 1
    public function setChangeTrackingPolicyNotify()
290
    {
291 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY);
292
293 1
        return $this;
294
    }
295
296
    /**
297
     * Adds lifecycle event.
298
     *
299
     * @param string $methodName
300
     * @param string $event
301
     *
302
     * @return ClassMetadataBuilder
303
     */
304 1
    public function addLifecycleEvent($methodName, $event)
305
    {
306 1
        $this->cm->addLifecycleCallback($methodName, $event);
307
308 1
        return $this;
309
    }
310
311
    /**
312
     * Adds Field.
313
     *
314
     * @param string $name
315
     * @param string $type
316
     * @param array  $mapping
317
     *
318
     * @return ClassMetadataBuilder
319
     */
320 1
    public function addField($name, $type, array $mapping = array())
321
    {
322 1
        $mapping['fieldName'] = $name;
323 1
        $mapping['type'] = $type;
324
325 1
        $this->cm->mapField($mapping);
326
327 1
        return $this;
328
    }
329
330
    /**
331
     * Creates a field builder.
332
     *
333
     * @param string $name
334
     * @param string $type
335
     *
336
     * @return FieldBuilder
337
     */
338 5
    public function createField($name, $type)
339
    {
340 5
        return new FieldBuilder(
341
            $this,
342
            array(
343 5
                'fieldName' => $name,
344 5
                'type'      => $type
345
            )
346
        );
347
    }
348
349
    /**
350
     * Creates an embedded builder.
351
     *
352
     * @param string $fieldName
353
     * @param string $class
354
     *
355
     * @return EmbeddedBuilder
356
     */
357 2
    public function createEmbedded($fieldName, $class)
358
    {
359 2
        return new EmbeddedBuilder(
360
            $this,
361
            array(
362 2
                'fieldName'    => $fieldName,
363 2
                'class'        => $class,
364
                'columnPrefix' => null
365
            )
366
        );
367
    }
368
369
    /**
370
     * Adds a simple many to one association, optionally with the inversed by field.
371
     *
372
     * @param string      $name
373
     * @param string      $targetEntity
374
     * @param string|null $inversedBy
375
     *
376
     * @return ClassMetadataBuilder
377
     */
378
    public function addManyToOne($name, $targetEntity, $inversedBy = null)
379
    {
380
        $builder = $this->createManyToOne($name, $targetEntity);
381
382
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
383
            $builder->inversedBy($inversedBy);
384
        }
385
386
        return $builder->build();
387
    }
388
389
    /**
390
     * Creates a ManyToOne Association Builder.
391
     *
392
     * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
393
     *
394
     * @param string $name
395
     * @param string $targetEntity
396
     *
397
     * @return AssociationBuilder
398
     */
399 3
    public function createManyToOne($name, $targetEntity)
400
    {
401 3
        return new AssociationBuilder(
402
            $this,
403
            array(
404 3
                'fieldName'    => $name,
405 3
                'targetEntity' => $targetEntity
406
            ),
407 3
            ClassMetadata::MANY_TO_ONE
408
        );
409
    }
410
411
    /**
412
     * Creates a OneToOne Association Builder.
413
     *
414
     * @param string $name
415
     * @param string $targetEntity
416
     *
417
     * @return AssociationBuilder
418
     */
419 4
    public function createOneToOne($name, $targetEntity)
420
    {
421 4
        return new AssociationBuilder(
422
            $this,
423
            array(
424 4
                'fieldName'    => $name,
425 4
                'targetEntity' => $targetEntity
426
            ),
427 4
            ClassMetadata::ONE_TO_ONE
428
        );
429
    }
430
431
    /**
432
     * Adds simple inverse one-to-one association.
433
     *
434
     * @param string $name
435
     * @param string $targetEntity
436
     * @param string $mappedBy
437
     *
438
     * @return ClassMetadataBuilder
439
     */
440
    public function addInverseOneToOne($name, $targetEntity, $mappedBy)
441
    {
442
        $builder = $this->createOneToOne($name, $targetEntity);
443
        $builder->mappedBy($mappedBy);
444
445
        return $builder->build();
446
    }
447
448
    /**
449
     * Adds simple owning one-to-one association.
450
     *
451
     * @param string      $name
452
     * @param string      $targetEntity
453
     * @param string|null $inversedBy
454
     *
455
     * @return ClassMetadataBuilder
456
     */
457
    public function addOwningOneToOne($name, $targetEntity, $inversedBy = null)
458
    {
459
        $builder = $this->createOneToOne($name, $targetEntity);
460
461
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
462
            $builder->inversedBy($inversedBy);
463
        }
464
465
        return $builder->build();
466
    }
467
468
    /**
469
     * Creates a ManyToMany Association Builder.
470
     *
471
     * @param string $name
472
     * @param string $targetEntity
473
     *
474
     * @return ManyToManyAssociationBuilder
475
     */
476 3
    public function createManyToMany($name, $targetEntity)
477
    {
478 3
        return new ManyToManyAssociationBuilder(
479
            $this,
480
            array(
481 3
                'fieldName'    => $name,
482 3
                'targetEntity' => $targetEntity
483
            ),
484 3
            ClassMetadata::MANY_TO_MANY
485
        );
486
    }
487
488
    /**
489
     * Adds a simple owning many to many association.
490
     *
491
     * @param string      $name
492
     * @param string      $targetEntity
493
     * @param string|null $inversedBy
494
     *
495
     * @return ClassMetadataBuilder
496
     */
497
    public function addOwningManyToMany($name, $targetEntity, $inversedBy = null)
498
    {
499
        $builder = $this->createManyToMany($name, $targetEntity);
500
501
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
502
            $builder->inversedBy($inversedBy);
503
        }
504
505
        return $builder->build();
506
    }
507
508
    /**
509
     * Adds a simple inverse many to many association.
510
     *
511
     * @param string $name
512
     * @param string $targetEntity
513
     * @param string $mappedBy
514
     *
515
     * @return ClassMetadataBuilder
516
     */
517
    public function addInverseManyToMany($name, $targetEntity, $mappedBy)
518
    {
519
        $builder = $this->createManyToMany($name, $targetEntity);
520
        $builder->mappedBy($mappedBy);
521
522
        return $builder->build();
523
    }
524
525
    /**
526
     * Creates a one to many association builder.
527
     *
528
     * @param string $name
529
     * @param string $targetEntity
530
     *
531
     * @return OneToManyAssociationBuilder
532
     */
533 3
    public function createOneToMany($name, $targetEntity)
534
    {
535 3
        return new OneToManyAssociationBuilder(
536
            $this,
537
            array(
538 3
                'fieldName'    => $name,
539 3
                'targetEntity' => $targetEntity
540
            ),
541 3
            ClassMetadata::ONE_TO_MANY
542
        );
543
    }
544
545
    /**
546
     * Adds simple OneToMany association.
547
     *
548
     * @param string $name
549
     * @param string $targetEntity
550
     * @param string $mappedBy
551
     *
552
     * @return ClassMetadataBuilder
553
     */
554
    public function addOneToMany($name, $targetEntity, $mappedBy)
555
    {
556
        $builder = $this->createOneToMany($name, $targetEntity);
557
        $builder->mappedBy($mappedBy);
558
559
        return $builder->build();
560
    }
561
}
562