Completed
Push — master ( cd41f7...e61615 )
by Pavel
07:44 queued 02:46
created

EntityToIdConverter::convert()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\DoctrineOrm;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
8
final class EntityToIdConverter
9
{
10
    /** @var  ManagerRegistry */
11
    private $registry;
12
13
    /**
14
     * EntityToIdConverter constructor.
15
     *
16
     * @param ManagerRegistry $registry
17
     */
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        $this->registry = $registry;
21
    }
22
23
    /**
24
     * @param $entity
25
     *
26
     * @return mixed|array
27
     */
28
    public function convert($entity)
29
    {
30
        $class    = get_class($entity);
31
        $metadata = $this->registry->getManagerForClass($class)->getClassMetadata($class);
32
33
        $ids = $metadata->getIdentifierValues($entity);
34
35
        if (!$metadata instanceof ClassMetadata || $metadata->isIdentifierComposite) {
36
            return $ids;
37
        }
38
39
        return array_shift($ids);
40
    }
41
}
42