Passed
Pull Request — feature/publishable (#47)
by Daniel
22:19
created

ClassMetadataTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 26
ccs 3
cts 10
cp 0.3
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityManager() 0 9 2
A getClassMetadata() 0 4 1
A initRegistry() 0 3 1
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\Utility;
15
16
use ApiPlatform\Core\Util\ClassInfoTrait;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
use Doctrine\ORM\ORMInvalidArgumentException;
20
use Doctrine\Persistence\ManagerRegistry;
21
22
/**
23
 * @author Vincent Chalamon <[email protected]>
24
 *
25
 * @internal
26
 */
27
trait ClassMetadataTrait
28
{
29
    use ClassInfoTrait;
30
31
    private ?ManagerRegistry $registry;
32
33 9
    private function initRegistry(ManagerRegistry $registry): void
34
    {
35 9
        $this->registry = $registry;
36 9
    }
37
38
    private function getClassMetadata(object $data): ClassMetadataInfo
39
    {
40
        /* @var ClassMetadataInfo $classMetadata */
41
        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...
42
    }
43
44
    private function getEntityManager(object $data): EntityManagerInterface
45
    {
46
        /** @var EntityManagerInterface|null $em */
47
        $em = $this->registry->getManagerForClass($this->getObjectClass($data));
0 ignored issues
show
Bug introduced by
The method getManagerForClass() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        /** @scrutinizer ignore-call */ 
48
        $em = $this->registry->getManagerForClass($this->getObjectClass($data));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        if (!$em) {
0 ignored issues
show
introduced by
$em is of type Doctrine\ORM\EntityManagerInterface, thus it always evaluated to true.
Loading history...
49
            throw ORMInvalidArgumentException::invalidObject(__CLASS__ . '::' . __FUNCTION__, $data);
50
        }
51
52
        return $em;
53
    }
54
}
55