Failed Conditions
Push — master ( 6ad71e...33755a )
by Adrien
15:46
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
 * @template T of AbstractModel
13
 * @extends EntityRepository<T>
14
 *
15
 * @method null|T findOneById(integer $id)
16
 */
17
abstract class AbstractRepository extends EntityRepository
18
{
19
    use \Ecodev\Felix\Repository\Traits\Repository;
20
21
    /**
22
     * Return native SQL query to get all ID of object owned by anybody from the family.
23
     */
24 19
    protected function getAllIdsForFamilyQuery(User $user): string
25
    {
26 19
        if ($user->getOwner()) {
27 9
            $id = $user->getOwner()->getId();
28
        } else {
29 10
            $id = $user->getId();
30
        }
31
32 19
        $connection = $this->getEntityManager()->getConnection();
33 19
        $qb = $connection->createQueryBuilder()
34 19
            ->select('id')
35 19
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
36 19
            ->andWhere('owner_id IN (SELECT id FROM user WHERE id = ' . $id . ' OR owner_id = ' . $id . ')');
37
38 19
        return $qb->getSQL();
39
    }
40
}
41