Failed Conditions
Push — develop ( c19264...352830 )
by Guilherme
65:34
created

IdentifierFlattener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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
declare(strict_types=1);
21
22
namespace Doctrine\ORM\Utility;
23
24
use Doctrine\ORM\Mapping\FieldMetadata;
25
use Doctrine\ORM\UnitOfWork;
26
use Doctrine\ORM\Mapping\ClassMetadata;
27
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
28
29
/**
30
 * The IdentifierFlattener utility now houses some of the identifier manipulation logic from unit of work, so that it
31
 * can be re-used elsewhere.
32
 *
33
 * @since       2.5
34
 * @author      Rob Caiger <[email protected]>
35
 */
36
final class IdentifierFlattener
37
{
38
    /**
39
     * The UnitOfWork used to coordinate object-level transactions.
40
     *
41
     * @var UnitOfWork
42
     */
43
    private $unitOfWork;
44
45
    /**
46
     * The metadata factory, used to retrieve the ORM metadata of entity classes.
47
     *
48
     * @var ClassMetadataFactory
49
     */
50
    private $metadataFactory;
51
52
    /**
53
     * Initializes a new IdentifierFlattener instance, bound to the given EntityManager.
54
     *
55 2292
     * @param UnitOfWork           $unitOfWork
56
     * @param ClassMetadataFactory $metadataFactory
57 2292
     */
58 2292
    public function __construct(UnitOfWork $unitOfWork, ClassMetadataFactory $metadataFactory)
59 2292
    {
60
        $this->unitOfWork = $unitOfWork;
61
        $this->metadataFactory = $metadataFactory;
62
    }
63
64
    /**
65
     * convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures.
66
     *
67
     * @param ClassMetadata $class
68
     * @param array         $id
69 848
     *
70
     * @return array
71 848
     */
72
    public function flattenIdentifier(ClassMetadata $class, array $id)
73 848
    {
74 848
        $flatId = [];
75
76 11
        foreach ($class->identifier as $field) {
77 11
            $property = $class->getProperty($field);
78
79
            if ($property instanceof FieldMetadata) {
80 11
                $flatId[$field] = $id[$field];
81 10
82
                continue;
83 1
            }
84
85
            if (isset($id[$field]) && is_object($id[$field])) {
86 11
                /* @var $targetClassMetadata ClassMetadata */
87 848
                $targetClassMetadata  = $this->metadataFactory->getMetadataFor($property->getTargetEntity());
88 54
                $targetClassPersister = $this->unitOfWork->getEntityPersister($property->getTargetEntity());
89
                $identifiers          = $targetClassPersister->getIdentifier($id[$field]);
90 54
91 54
                $associatedId = $this->flattenIdentifier($targetClassMetadata, $identifiers);
92
93
                $flatId[$field] = implode(' ', $associatedId);
94 54
95
                continue;
96 848
            }
97
98
            $associatedId = [];
99
100 848
            foreach ($property->getJoinColumns() as $joinColumn) {
101
                $associatedId[] = $id[$joinColumn->getColumnName()];
102
            }
103
104
            $flatId[$field] = implode(' ', $associatedId);
105
        }
106
107
        return $flatId;
108
    }
109
}
110