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

AbstractRepository::getAllIdsForAnyOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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