Passed
Pull Request — feature/publishable (#31)
by Vincent
06:31
created

ClassMetadataTrait::getEntityManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentBundle\Publishable;
15
16
use ApiPlatform\Core\Util\ClassInfoTrait;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
use Doctrine\ORM\ORMInvalidArgumentException;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 *
24
 * @internal
25
 */
26
trait ClassMetadataTrait
27
{
28
    use ClassInfoTrait;
29
30
    private function getClassMetadata(object $data): ClassMetadataInfo
31
    {
32
        /* @var ClassMetadataInfo $classMetadata */
33
        return $this->getEntityManager($data)->getClassMetadata($this->getObjectClass($data));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...>getObjectClass($data)) returns the type Doctrine\Persistence\Mapping\ClassMetadata which includes types incompatible with the type-hinted return Doctrine\ORM\Mapping\ClassMetadataInfo.
Loading history...
34
    }
35
36
    private function getEntityManager(object $data): EntityManagerInterface
37
    {
38
        /** @var EntityManagerInterface $em */
39
        $em = $this->registry->getManagerForClass($this->getObjectClass($data));
40
        if (!$em) {
0 ignored issues
show
introduced by
$em is of type Doctrine\ORM\EntityManagerInterface, thus it always evaluated to true.
Loading history...
41
            throw ORMInvalidArgumentException::invalidObject(__CLASS__ . '::' . __FUNCTION__, $data);
42
        }
43
44
        return $em;
45
    }
46
}
47