Completed
Pull Request — master (#6500)
by Mathew
17:49 queued 08:41
created

AssignedGenerator::generate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 8.7972
cc 4
eloc 12
nc 4
nop 2
crap 4
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\Id;
21
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\ORMException;
24
25
/**
26
 * Special generator for application-assigned identifiers (doesn't really generate anything).
27
 *
28
 * @since   2.0
29
 * @author  Benjamin Eberlei <[email protected]>
30
 * @author  Guilherme Blanco <[email protected]>
31
 * @author  Jonathan Wage <[email protected]>
32
 * @author  Roman Borschel <[email protected]>
33
 */
34
class AssignedGenerator extends AbstractIdGenerator
35
{
36
    /**
37
     * Returns the identifier assigned to the given entity.
38
     *
39
     * {@inheritDoc}
40
     *
41
     * @throws \Doctrine\ORM\ORMException
42
     */
43 274
    public function generate(EntityManager $em, $entity)
44
    {
45 274
        $class      = $em->getClassMetadata(get_class($entity));
46 274
        $idFields   = $class->getIdentifierFieldNames();
47 274
        $identifier = [];
48
49 274
        foreach ($idFields as $idField) {
50 274
            $value = $class->getFieldValue($entity, $idField);
51
52 274
            if ( ! isset($value)) {
53 2
                throw ORMException::entityMissingAssignedIdForField($entity, $idField);
54
            }
55
56 272
            if (isset($class->associationMappings[$idField])) {
57
                // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
58 74
                $value = $em->getUnitOfWork()->getSingleIdentifierValue($value);
59
            }
60
61 272
            $identifier[$idField] = $value;
62
        }
63
64 272
        return $identifier;
65
    }
66
}
67