Completed
Pull Request — master (#907)
by Antoine
03:08
created

IdentifierManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C normalizeIdentifiers() 0 38 8
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
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
13
14
use ApiPlatform\Core\Exception\PropertyNotFoundException;
15
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
16
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
17
use Doctrine\Common\Persistence\ObjectManager;
18
19
class IdentifierManager implements IdentifierManagerInterface
20
{
21
    private $propertyNameCollectionFactory;
22
    private $propertyMetadataFactory;
23
24
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory)
25
    {
26
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
27
        $this->propertyMetadataFactory = $propertyMetadataFactory;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
34
    {
35
        $identifierValues = [$id];
36
        $doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
37
38
        if (2 <= count($doctrineMetadataIdentifier)) {
39
            $identifiers = explode(';', $id);
40
            $identifiersMap = [];
41
42
            // first transform identifiers to a proper key/value array
43
            foreach ($identifiers as $identifier) {
44
                $keyValue = explode('=', $identifier);
45
                $identifiersMap[$keyValue[0]] = $keyValue[1];
46
            }
47
        }
48
49
        $identifiers = [];
50
        $i = 0;
51
52
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
53
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
54
55
            $identifier = $propertyMetadata->isIdentifier();
56
            if (null === $identifier || false === $identifier) {
57
                continue;
58
            }
59
60
            $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
61
            if (null === $identifier) {
62
                throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
63
            }
64
65
            $identifiers[$propertyName] = $identifier;
66
            ++$i;
67
        }
68
69
        return $identifiers;
70
    }
71
}
72