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

TransactionRepository::getAccessibleSubQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\User;
8
9
class TransactionRepository extends AbstractRepository implements LimitedAccessSubQueryInterface
10
{
11
    /**
12
     * Returns pure SQL to get ID of all objects that are accessible to given user.
13
     *
14
     * @param null|User $user
15
     *
16
     * @return string
17
     */
18 6
    public function getAccessibleSubQuery(?User $user): string
19
    {
20 6
        if (!$user) {
21 1
            return '-1';
22
        }
23
24 5
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
25 2
            return $this->getAllIdsQuery();
26
        }
27
28
        return 'SELECT transaction.id FROM transaction
29
              JOIN transaction_line ON transaction.id = transaction_line.transaction_id
30
              JOIN account ON transaction_line.debit_id = account.id OR transaction_line.credit_id = account.id 
31 3
              WHERE account.owner_id = ' . $user->getId();
32
    }
33
}
34