Repository   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 48
ccs 0
cts 16
cp 0
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAclFilter() 0 6 1
A getAllIdsForOwnerQuery() 0 9 1
A getAllIdsQuery() 0 8 1
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