Completed
Push — 2.6 ( ffb7d4...87ee40 )
by Marco
13s
created

PersisterHelper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 60.98%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 25
cts 41
cp 0.6098
rs 10
c 0
b 0
f 0
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getTypeOfColumn() 0 46 12
B getTypeOfField() 0 30 6
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Utility;
22
23
use Doctrine\ORM\EntityManagerInterface;
24
use Doctrine\ORM\Mapping\ClassMetadata;
25
use Doctrine\ORM\Query\QueryException;
26
27
/**
28
 * The PersisterHelper contains logic to infer binding types which is used in
29
 * several persisters.
30
 *
31
 * @link   www.doctrine-project.org
32
 * @since  2.5
33
 * @author Jasper N. Brouwer <[email protected]>
34
 */
35
class PersisterHelper
36
{
37
    /**
38
     * @param string                 $fieldName
39
     * @param ClassMetadata          $class
40
     * @param EntityManagerInterface $em
41
     *
42
     * @return array
43
     *
44
     * @throws QueryException
45
     */
46 6
    public static function getTypeOfField($fieldName, ClassMetadata $class, EntityManagerInterface $em)
47
    {
48 6
        if (isset($class->fieldMappings[$fieldName])) {
49 5
            return [$class->fieldMappings[$fieldName]['type']];
50
        }
51
52 2
        if ( ! isset($class->associationMappings[$fieldName])) {
53
            return [];
54
        }
55
56 2
        $assoc = $class->associationMappings[$fieldName];
57
58 2
        if (! $assoc['isOwningSide']) {
59
            return self::getTypeOfField($assoc['mappedBy'], $em->getClassMetadata($assoc['targetEntity']), $em);
60
        }
61
62 2
        if ($assoc['type'] & ClassMetadata::MANY_TO_MANY) {
63
            $joinData = $assoc['joinTable'];
64
        } else {
65 2
            $joinData = $assoc;
66
        }
67
68 2
        $types       = [];
69 2
        $targetClass = $em->getClassMetadata($assoc['targetEntity']);
70
71 2
        foreach ($joinData['joinColumns'] as $joinColumn) {
72 2
            $types[] = self::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $em);
73
        }
74
75 2
        return $types;
76
    }
77
78
    /**
79
     * @param string                 $columnName
80
     * @param ClassMetadata          $class
81
     * @param EntityManagerInterface $em
82
     *
83
     * @return string
84
     *
85
     * @throws \RuntimeException
86
     */
87 1013
    public static function getTypeOfColumn($columnName, ClassMetadata $class, EntityManagerInterface $em)
88
    {
89 1013
        if (isset($class->fieldNames[$columnName])) {
90 1013
            $fieldName = $class->fieldNames[$columnName];
91
92 1013
            if (isset($class->fieldMappings[$fieldName])) {
93 1013
                return $class->fieldMappings[$fieldName]['type'];
94
            }
95
        }
96
97
        // iterate over to-one association mappings
98 36
        foreach ($class->associationMappings as $assoc) {
99 36
            if ( ! isset($assoc['joinColumns'])) {
100
                continue;
101
            }
102
103 36
            foreach ($assoc['joinColumns'] as $joinColumn) {
104 36
                if ($joinColumn['name'] == $columnName) {
105 36
                    $targetColumnName = $joinColumn['referencedColumnName'];
106 36
                    $targetClass      = $em->getClassMetadata($assoc['targetEntity']);
107
108 36
                    return self::getTypeOfColumn($targetColumnName, $targetClass, $em);
109
                }
110
            }
111
        }
112
113
        // iterate over to-many association mappings
114
        foreach ($class->associationMappings as $assoc) {
115
            if ( ! (isset($assoc['joinTable']) && isset($assoc['joinTable']['joinColumns']))) {
116
                continue;
117
            }
118
119
            foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
120
                if ($joinColumn['name'] == $columnName) {
121
                    $targetColumnName = $joinColumn['referencedColumnName'];
122
                    $targetClass      = $em->getClassMetadata($assoc['targetEntity']);
123
124
                    return self::getTypeOfColumn($targetColumnName, $targetClass, $em);
125
                }
126
            }
127
        }
128
129
        throw new \RuntimeException(sprintf(
130
            'Could not resolve type of column "%s" of class "%s"',
131
            $columnName,
132
            $class->getName()
133
        ));
134
    }
135
}
136