RepositoryWrappersTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 59
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAssociations() 0 3 1
A getClassMetaData() 0 3 1
A getReference() 0 9 2
A createQueryBuilder() 0 12 2
A getEntityManager() 0 17 3
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
use function preg_replace;
18
19
/**
20
 * Class RepositoryWrappersTrait
21
 *
22
 * @package App\Repository\Traits
23
 * @author TLe, Tarmo Leppänen <[email protected]>
24
 */
25
trait RepositoryWrappersTrait
26
{
27 263
    public function getReference(string $id): ?object
28
    {
29
        try {
30 263
            $referenceId = UuidHelper::fromString($id);
31 1
        } catch (InvalidUuidStringException) {
32 1
            $referenceId = $id;
33
        }
34
35 263
        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

35
        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...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @psalm-return array<string, array<string, mixed>>
42
     */
43 10
    public function getAssociations(): array
44
    {
45 10
        return $this->getClassMetaData()->getAssociationMappings();
46
    }
47
48 11
    public function getClassMetaData(): ClassMetadataInfo
49
    {
50 11
        return $this->getEntityManager()->getClassMetadata($this->getEntityName());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...$this->getEntityName()) returns the type Doctrine\Persistence\Mapping\ClassMetadata which includes types incompatible with the type-hinted return Doctrine\ORM\Mapping\ClassMetadataInfo.
Loading history...
51
    }
52
53 666
    public function getEntityManager(): EntityManager
54
    {
55 666
        $manager = $this->managerRegistry->getManagerForClass($this->getEntityName());
56
57 666
        if (!($manager instanceof EntityManager)) {
58 1
            throw new UnexpectedValueException(
59 1
                'Cannot get entity manager for entity \'' . $this->getEntityName() . '\''
60 1
            );
61
        }
62
63 665
        if ($manager->isOpen() === false) {
64 1
            $this->managerRegistry->resetManager();
65
66 1
            $manager = $this->getEntityManager();
67
        }
68
69 665
        return $manager;
70
    }
71
72 356
    public function createQueryBuilder(?string $alias = null, ?string $indexBy = null): QueryBuilder
73
    {
74 356
        $alias ??= 'entity';
75 356
        $alias = (string)preg_replace('#[\W]#', '', $alias);
76 356
        $indexBy = $indexBy !== null ? (string)preg_replace('#[\W]#', '', $indexBy) : null;
77
78
        // Create new query builder
79 356
        return $this
80 356
            ->getEntityManager()
81 356
            ->createQueryBuilder()
82 356
            ->select($alias)
83 356
            ->from($this->getEntityName(), $alias, $indexBy);
84
    }
85
}
86