Passed
Push — master ( d62077...4ada73 )
by Sylvain
08:20
created

AbstractRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
c 0
b 0
f 0
dl 0
loc 36
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllIdsForFamilyQuery() 0 15 2
A getAllIdsForAnyOwner() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\AbstractModel;
8
use Application\Model\User;
9
use Doctrine\ORM\EntityRepository;
10
11
/**
12
 * Class AbstractRepository
13
 *
14
 * @method null|AbstractModel findOneById(integer $id)
15
 */
16
abstract class AbstractRepository extends EntityRepository
17
{
18
    use \Ecodev\Felix\Repository\Traits\Repository;
19
20
    /**
21
     * Return native SQL query to get all ID of object owned by anybody from the family
22
     */
23 18
    protected function getAllIdsForFamilyQuery(User $user): string
24
    {
25 18
        if ($user->getOwner()) {
26 9
            $id = $user->getOwner()->getId();
27
        } else {
28 9
            $id = $user->getId();
29
        }
30
31 18
        $connection = $this->getEntityManager()->getConnection();
32 18
        $qb = $connection->createQueryBuilder()
33 18
            ->select('id')
34 18
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
35 18
            ->andWhere('owner_id IN (SELECT id FROM user WHERE id = ' . $id . ' OR owner_id = ' . $id . ')');
36
37 18
        return $qb->getSQL();
38
    }
39
40
    /**
41
     * Return native SQL query to get all ID of objects own by anybody
42
     */
43 1
    protected function getAllIdsForAnyOwner(): string
44
    {
45 1
        $connection = $this->getEntityManager()->getConnection();
46 1
        $qb = $connection->createQueryBuilder()
47 1
            ->select('id')
48 1
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
49 1
            ->andWhere('owner_id IS NOT NULL');
50
51 1
        return $qb->getSQL();
52
    }
53
}
54