AbstractRepository::getAllIdsForFamilyQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9332
ccs 10
cts 10
cp 1
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
 * @template T of AbstractModel
13
 *
14
 * @extends EntityRepository<T>
15
 *
16
 * @method null|T findOneById(integer $id)
17
 */
18
abstract class AbstractRepository extends EntityRepository
19
{
20
    use \Ecodev\Felix\Repository\Traits\Repository;
21
22
    /**
23
     * Return native SQL query to get all ID of object owned by anybody from the family.
24
     */
25 18
    protected function getAllIdsForFamilyQuery(User $user): string
26
    {
27 18
        if ($user->getOwner()) {
28 9
            $id = $user->getOwner()->getId();
29
        } else {
30 9
            $id = $user->getId();
31
        }
32
33 18
        $connection = $this->getEntityManager()->getConnection();
34 18
        $qb = $connection->createQueryBuilder()
35 18
            ->select('id')
36 18
            ->from($connection->quoteIdentifier($this->getClassMetadata()->getTableName()))
37 18
            ->andWhere('owner_id IN (SELECT id FROM user WHERE id = ' . $id . ' OR owner_id = ' . $id . ')');
38
39 18
        return $qb->getSQL();
40
    }
41
}
42