Completed
Push — master ( 0d4b62...49eab6 )
by Adrien
07:35
created

TransactionLineRepository::findByDebitOrCredit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\Account;
8
use Application\Model\TransactionLine;
9
use Application\Model\User;
10
11
class TransactionLineRepository extends AbstractRepository implements LimitedAccessSubQueryInterface
12
{
13
    /**
14
     * Returns pure SQL to get ID of all objects that are accessible to given user.
15
     *
16
     * @param null|User $user
17
     *
18
     * @return string
19
     */
20 6
    public function getAccessibleSubQuery(?User $user): string
21
    {
22 6
        if (!$user) {
23 1
            return '-1';
24
        }
25
26 5
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
27 2
            return $this->getAllIdsQuery();
28
        }
29
30
        return 'SELECT transaction_line.id FROM transaction_line
31
              JOIN account ON transaction_line.debit_id = account.id OR transaction_line.credit_id = account.id 
32 3
              WHERE account.owner_id = ' . $user->getId();
33
    }
34
35
    /**
36
     * Get all transaction lines matching a given account in credit or debit
37
     *
38
     * @return TransactionLine[]
39
     */
40 1
    public function findByDebitOrCredit(Account $account): array
41
    {
42 1
        $qb = $this->createQueryBuilder('line')
43 1
            ->where('line.debit = :account')
44 1
            ->orWhere('line.credit = :account')
45 1
            ->setParameter('account', $account);
46
47 1
        $query = $qb->getQuery();
48
49 1
        $result = $query->getResult();
50
51 1
        return $result;
52
    }
53
}
54