Completed
Push — master ( 57558f...bc6de4 )
by Pavel
03:43
created

IdentifierFlattener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 42.31%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 1
cbo 4
dl 0
loc 63
ccs 11
cts 26
cp 0.4231
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B flattenIdentifier() 0 33 6
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
21
namespace Bankiru\Api\Doctrine\Utility;
22
23
use Bankiru\Api\Doctrine\ApiEntityManager;
24
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
25
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
26
use Bankiru\Api\Doctrine\UnitOfWork;
27
28
/**
29
 * The IdentifierFlattener utility now houses some of the identifier manipulation logic from unit of work, so that it
30
 * can be re-used elsewhere.
31
 *
32
 * @since       2.5
33
 * @author      Rob Caiger <[email protected]>
34
 */
35
final class IdentifierFlattener
36
{
37
    /** @var UnitOfWork */
38
    private $unitOfWork;
39
    /** @var  ApiEntityManager */
40
    private $manager;
41
42
    /**
43
     * Initializes a new IdentifierFlattener instance, bound to the given EntityManager.
44
     *
45
     * @param ApiEntityManager $manager
46
     *
47
     * @internal param UnitOfWork $unitOfWork
48
     * @internal param ClassMetadataFactory $metadataFactory
49
     */
50 14
    public function __construct(ApiEntityManager $manager)
51
    {
52 14
        $this->manager    = $manager;
53 14
        $this->unitOfWork = $this->manager->getUnitOfWork();
54 14
    }
55
56
    /**
57
     * convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures.
58
     *
59
     * @param ApiMetadata $class
60
     * @param array       $id
61
     *
62
     * @return array
63
     */
64 12
    public function flattenIdentifier(ApiMetadata $class, array $id)
65
    {
66 12
        $flatId = [];
67
68 12
        foreach ($class->getIdentifierFieldNames() as $field) {
69 12
            if ($class->hasAssociation($field) && array_key_exists($field, $id) && is_object($id[$field])) {
70
                /* @var EntityMetadata $targetClassMetadata */
71
                $targetClassMetadata = $this->manager->getClassMetadata(
72
                    $class->getAssociationMapping($field)['target']
73
                );
74
75
                if ($this->unitOfWork->isInIdentityMap($id[$field])) {
76
                    $associatedId =
77
                        $this->flattenIdentifier(
78
                            $targetClassMetadata,
79
                            $this->unitOfWork->getEntityIdentifier($id[$field])
80
                        );
81
                } else {
82
                    $associatedId =
83
                        $this->flattenIdentifier(
84
                            $targetClassMetadata,
85
                            $targetClassMetadata->getIdentifierValues($id[$field])
86
                        );
87
                }
88
89
                $flatId[$field] = implode(' ', $associatedId);
90
            } else {
91 12
                $flatId[$field] = $id[$field];
92
            }
93 12
        }
94
95 12
        return $flatId;
96
    }
97
}
98