Passed
Pull Request — master (#1119)
by Tarmo
08:41
created

RepositoryWrappersTrait::getEntityManager()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Repository/Traits/RepositoryWrappersTrait.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Repository\Traits;
10
11
use App\Rest\UuidHelper;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\Mapping\ClassMetadataInfo;
14
use Doctrine\ORM\QueryBuilder;
15
use Ramsey\Uuid\Exception\InvalidUuidStringException;
16
use UnexpectedValueException;
17
18
/**
19
 * Class RepositoryWrappersTrait
20
 *
21
 * @package App\Repository\Traits
22
 * @author TLe, Tarmo Leppänen <[email protected]>
23
 */
24
trait RepositoryWrappersTrait
25
{
26 210
    public function getReference(string $id): ?object
27
    {
28
        try {
29 210
            $referenceId = UuidHelper::fromString($id);
30 1
        } catch (InvalidUuidStringException) {
31 1
            $referenceId = $id;
32
        }
33
34 210
        return $this->getEntityManager()->getReference($this->getEntityName(), $referenceId);
0 ignored issues
show
Bug introduced by
The method getEntityName() does not exist on App\Repository\Traits\RepositoryWrappersTrait. Did you maybe mean getEntityManager()? ( Ignorable by Annotation )

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

34
        return $this->getEntityManager()->getReference($this->/** @scrutinizer ignore-call */ getEntityName(), $referenceId);

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...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @return array<string, string>
41
     */
42 10
    public function getAssociations(): array
43
    {
44 10
        return $this->getClassMetaData()->getAssociationMappings();
45
    }
46
47 11
    public function getClassMetaData(): ClassMetadataInfo
48
    {
49 11
        return $this->getEntityManager()->getClassMetadata($this->getEntityName());
50
    }
51
52 530
    public function getEntityManager(): EntityManager
53
    {
54 530
        $manager = $this->managerRegistry->getManagerForClass($this->getEntityName());
55
56 530
        if (!($manager instanceof EntityManager)) {
57 1
            throw new UnexpectedValueException(
58 1
                'Cannot get entity manager for entity \'' . $this->getEntityName() . '\''
59
            );
60
        }
61
62 529
        if ($manager->isOpen() === false) {
63 1
            $this->managerRegistry->resetManager();
64
65 1
            $manager = $this->getEntityManager();
66
        }
67
68 529
        return $manager;
69
    }
70
71 312
    public function createQueryBuilder(?string $alias = null, ?string $indexBy = null): QueryBuilder
72
    {
73 312
        $alias ??= 'entity';
74
75
        // Create new query builder
76
        return $this
77 312
            ->getEntityManager()
78 312
            ->createQueryBuilder()
79 312
            ->select($alias)
80 312
            ->from($this->getEntityName(), $alias, $indexBy);
81
    }
82
}
83