Repository::getAllIdsQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Repository\Traits;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Ecodev\Felix\Model\User;
10
use Ecodev\Felix\ORM\Query\Filter\AclFilter;
11
12
/**
13
 * Trait for common method of repository.
14
 */
15
trait Repository
16
{
17
    /**
18
     * @return EntityManager
19
     */
20
    abstract protected function getEntityManager();
21
22
    /**
23
     * @return ClassMetadata
24
     */
25
    abstract protected function getClassMetadata();
26
27
    /**
28
     * Returns the AclFilter to fetch ACL filtering SQL.
29
     */
30
    public function getAclFilter(): AclFilter
31
    {
32
        /** @var AclFilter $aclFilter */
33
        $aclFilter = $this->getEntityManager()->getFilters()->getFilter(AclFilter::class);
34
35
        return $aclFilter;
36
    }
37
38
    /**
39
     * Return native SQL query to get all ID.
40
     */
41
    public function getAllIdsQuery(): string
42
    {
43
        $connection = $this->getEntityManager()->getConnection();
44
        $qb = $connection->createQueryBuilder()
45
            ->select('id')
46
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()));
47
48
        return $qb->getSQL();
49
    }
50
51
    /**
52
     * Return native SQL query to get all ID of object owned by given user.
53
     */
54
    protected function getAllIdsForOwnerQuery(User $user): string
55
    {
56
        $connection = $this->getEntityManager()->getConnection();
57
        $qb = $connection->createQueryBuilder()
58
            ->select('id')
59
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
60
            ->andWhere('owner_id = ' . $user->getId());
61
62
        return $qb->getSQL();
63
    }
64
}
65