Completed
Pull Request — 2.0 (#1097)
by Han Hui
03:19
created

IdentifierManagerTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 62
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C normalizeIdentifiers() 0 45 12
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
15
16
use ApiPlatform\Core\Exception\PropertyNotFoundException;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Doctrine\DBAL\Types\Type as DBALType;
19
use Doctrine\ORM\EntityManagerInterface;
20
21
/**
22
 * @internal
23
 */
24
trait IdentifierManagerTrait
25
{
26
    private $propertyNameCollectionFactory;
27
    private $propertyMetadataFactory;
28
29
    /**
30
     * Transform and check the identifier, composite or not.
31
     *
32
     * @param int|string    $id
33
     * @param ObjectManager $manager
34
     * @param string        $resourceClass
35
     *
36
     * @throws PropertyNotFoundException
37
     *
38
     * @return array
39
     */
40
    private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
41
    {
42
        $identifierValues = [$id];
43
        $doctrineClassMetadata = $manager->getClassMetadata($resourceClass);
44
        $doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier();
45
        $isOrm = interface_exists(EntityManagerInterface::class) && $manager instanceof EntityManagerInterface;
46
        $platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null;
47
48
        if (count($doctrineIdentifierFields) > 1) {
49
            $identifiersMap = [];
50
51
            // first transform identifiers to a proper key/value array
52
            foreach (explode(';', $id) as $identifier) {
53
                $identifierPair = explode('=', $identifier);
54
                $identifiersMap[$identifierPair[0]] = $identifierPair[1];
55
            }
56
        }
57
58
        $identifiers = [];
59
        $i = 0;
60
61
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
62
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
63
64
            if (!$propertyMetadata->isIdentifier()) {
65
                continue;
66
            }
67
68
            $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
69
            if (null === $identifier) {
70
                throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
71
            }
72
73
            $doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName);
74
75
            if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) {
76
                $identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform);
77
            }
78
79
            $identifiers[$propertyName] = $identifier;
80
            ++$i;
81
        }
82
83
        return $identifiers;
84
    }
85
}
86