Completed
Push — master ( 747c15...4b3c41 )
by Tarmo
42:40
created

RepositoryWrappersTrait::createQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
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
namespace App\Repository\Traits;
9
10
use Doctrine\Common\Persistence\ManagerRegistry;
11
use Doctrine\Common\Persistence\ObjectManager;
12
use Doctrine\Common\Proxy\Proxy;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\QueryBuilder;
15
16
/**
17
 * Class RepositoryWrappersTrait
18
 *
19
 * @package App\Repository\Traits
20
 * @author  TLe, Tarmo Leppänen <[email protected]>
21
 *
22
 * @method string        getEntityName(): string
23
 */
24
trait RepositoryWrappersTrait
25
{
26
    /**
27
     * @var ManagerRegistry
28
     */
29
    protected $managerRegistry;
30
31
    /** @noinspection GenericObjectTypeUsageInspection */
32
    /**
33
     * Gets a reference to the entity identified by the given type and identifier without actually loading it,
34
     * if the entity is not yet loaded.
35
     *
36
     * @param string $id
37
     *
38
     * @return Proxy|object|null
39
     *
40
     * @throws \Doctrine\ORM\ORMException
41
     */
42 1
    public function getReference(string $id)
43
    {
44 1
        return $this->getEntityManager()->getReference($this->getEntityName(), $id);
1 ignored issue
show
Bug introduced by
The method getReference() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

44
        return $this->getEntityManager()->/** @scrutinizer ignore-call */ getReference($this->getEntityName(), $id);
Loading history...
45
    }
46
47
    /**
48
     * Gets all association mappings of the class.
49
     *
50
     * @return array
51
     */
52 10
    public function getAssociations(): array
53
    {
54 10
        return $this->getClassMetaData()->getAssociationMappings();
1 ignored issue
show
Bug introduced by
The method getAssociationMappings() does not exist on Doctrine\Common\Persistence\Mapping\ClassMetadata. Did you maybe mean getAssociationNames()? ( Ignorable by Annotation )

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

54
        return $this->getClassMetaData()->/** @scrutinizer ignore-call */ getAssociationMappings();

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...
55
    }
56
57
    /**
58
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata
59
     */
60 10
    public function getClassMetaData()
61
    {
62 10
        return $this->getEntityManager()->getClassMetadata($this->getEntityName());
63
    }
64
65
    /**
66
     * Getter method for EntityManager for current entity.
67
     *
68
     * @return EntityManager|ObjectManager
69
     */
70 907
    public function getEntityManager()
71
    {
72 907
        return $this->managerRegistry->getManagerForClass(static::$entityName);
0 ignored issues
show
Bug Best Practice introduced by
The property entityName does not exist on App\Repository\Traits\RepositoryWrappersTrait. Did you maybe forget to declare it?
Loading history...
73
    }
74
75
    /**
76
     * Method to create new query builder for current entity.
77
     *
78
     * @param string $alias
79
     * @param string $indexBy
80
     *
81
     * @return QueryBuilder
82
     */
83 80
    public function createQueryBuilder(string $alias = null, string $indexBy = null): QueryBuilder
84
    {
85 80
        $alias = $alias ?? 'entity';
86
87
        // Create new query builder
88
        $queryBuilder = $this
89 80
            ->getEntityManager()
90 80
            ->createQueryBuilder()
1 ignored issue
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

90
            ->/** @scrutinizer ignore-call */ createQueryBuilder()
Loading history...
91 80
            ->select($alias)
92 80
            ->from($this->getEntityName(), $alias, $indexBy);
93
94 80
        return $queryBuilder;
95
    }
96
}
97