Completed
Pull Request — master (#904)
by Antoine
03:50
created

IdentifiersHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C normalizeIdentifiers() 0 39 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
/**
20
 * Tools that helps managing Identifiers (composite or regular) and related where clauses.
21
 */
22
class IdentifiersHelper
23
{
24
    private $propertyNameCollectionFactory;
25
    private $propertyMetadataFactory;
26
27
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory)
28
    {
29
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
30
        $this->propertyMetadataFactory = $propertyMetadataFactory;
31
    }
32
33
    /**
34
     * Transform and check the identifier, composite or not.
35
     *
36
     * @param int|string    $id
37
     * @param ObjectManager $manager
38
     * @param string        $resourceClass
39
     *
40
     * @throws PropertyNotFoundException
41
     *
42
     * @return array
43
     */
44
    public function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
45
    {
46
        $identifierValues = [$id];
47
        $doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
48
49
        if (count($doctrineMetadataIdentifier) >= 2) {
50
            $identifiers = explode(';', $id);
51
            $identifiersMap = [];
52
53
            // first transform identifiers to a proper key/value array
54
            foreach ($identifiers as $identifier) {
55
                $keyValue = explode('=', $identifier);
56
                $identifiersMap[$keyValue[0]] = $keyValue[1];
57
            }
58
        }
59
60
        $identifiers = [];
61
        $i = 0;
62
63
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
64
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
65
66
            $identifier = $propertyMetadata->isIdentifier();
67
            if (null === $identifier || false === $identifier) {
68
                continue;
69
            }
70
71
            $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
72
73
            if (null === $identifier) {
74
                throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
75
            }
76
77
            $identifiers[$propertyName] = $identifier;
78
            ++$i;
79
        }
80
81
        return $identifiers;
82
    }
83
}
84