MetadataMapper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 63
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 13 3
B mapRelation() 0 40 8
1
<?php
2
3
/**
4
 * Copyright 2015-2021 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2015-2021 Fabian Grutschus. All rights reserved.
33
 * @license   BSD-2-Clause
34
 */
35
36
namespace Fabiang\DoctrineDynamic\Mapper;
37
38
use Fabiang\DoctrineDynamic\Configuration\Field;
39
use Doctrine\Persistence\Mapping\ClassMetadata;
40
use Fabiang\DoctrineDynamic\Configuration\Entity as EntityConfiguration;
41
42
class MetadataMapper implements Mapper
43
{
44
45 1
    public function map(ClassMetadata $metadata, EntityConfiguration $configuration)
46
    {
47 1
        if ($configuration->getRepository()) {
48 1
            $metadata->customRepositoryClassName = $configuration->getRepository();
0 ignored issues
show
Bug introduced by
Accessing customRepositoryClassName on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49 1
        }
50
51 1
        foreach ($configuration->getFields() as $field) {
52 1
            $this->mapRelation('oneToOne', $field, $metadata);
53 1
            $this->mapRelation('manyToOne', $field, $metadata);
54 1
            $this->mapRelation('oneToMany', $field, $metadata);
55 1
            $this->mapRelation('manyToMany', $field, $metadata);
56 1
        }
57 1
    }
58
59
    /**
60
     * @param string $type
61
     * @param Field $field
62
     * @param ClassMetadata $metadata
63
     */
64 1
    private function mapRelation($type, Field $field, ClassMetadata $metadata)
65
    {
66 1
        $getMethod = 'get' . ucfirst($type);
67 1
        $relations = $field->{$getMethod}();
68 1
        $mapMethod = 'map' . ucfirst($type);
69
70 1
        foreach ($relations as $relation) {
71
            $relationConfig = [
72 1
                'fieldName'    => $field->getName(),
73 1
                'targetEntity' => $relation->getTargetEntity(),
74 1
            ];
75
76 1
            if (method_exists($relation, 'getInversedBy')) {
77 1
                $relationConfig['inversedBy'] = $relation->getInversedBy();
78 1
            }
79
80 1
            if (method_exists($relation, 'getMappedBy')) {
81 1
                $relationConfig['mappedBy'] = $relation->getMappedBy();
82 1
            }
83
84 1
            if (method_exists($relation, 'getJoinColumn')
85 1
                && null !== ($joinColumn = $relation->getJoinColumn())) {
86 1
                $relationConfig['joinColumns'] = [
87
                    [
88 1
                        'name'                 => $joinColumn->getName(),
89 1
                        'referencedColumnName' => $joinColumn->getReferencedColumnName(),
90
                    ]
91 1
                ];
92 1
            }
93
94 1
            if (method_exists($relation, 'getJoinTable')
95 1
                && null !== ($joinTable = $relation->getJoinTable())) {
96 1
                $relationConfig['joinTable'] = [
97 1
                    'name' => $joinTable->getName(),
98
                ];
99 1
            }
100
101 1
            $metadata->{$mapMethod}($relationConfig);
102 1
        }
103 1
    }
104
}
105