Failed Conditions
Push — master ( 8edf11...8ca883 )
by Sylvain
08:08 queued 01:18
created

AbstractRepository::getAllIdsForAnyOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
    protected function getAllIdsForAnyOwner(): string
44
    {
45
        $connection = $this->getEntityManager()->getConnection();
46
        $qb = $connection->createQueryBuilder()
47
            ->select('id')
48
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
49
            ->andWhere('owner_id IS NOT NULL');
50
51
        return $qb->getSQL();
52
    }
53
}
54