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

AbstractRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
c 0
b 0
f 0
dl 0
loc 36
ccs 10
cts 17
cp 0.5881
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
    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