Failed Conditions
Push — master ( c4208e...4b96fe )
by Sylvain
09:02
created

TransactionLineRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 36
c 0
b 0
f 0
ccs 6
cts 9
cp 0.6667
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessibleSubQuery() 0 13 3
A exportExcel() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Action\ExportTransactionLinesAction;
8
use Application\Model\User;
9
use Doctrine\ORM\Query;
10
11
class TransactionLineRepository extends AbstractRepository implements LimitedAccessSubQueryInterface, ExportExcelInterface
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 7
    public function getAccessibleSubQuery(?User $user): string
21
    {
22 7
        if (!$user) {
23 1
            return '-1';
24
        }
25
26 6
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
27 3
            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
     * Generates an Excel spreadsheet with the query result
37
     *
38
     * @param Query $query
39
     *
40
     * @return string name of the temporary file
41
     */
42
    public function exportExcel(Query $query): string
43
    {
44
        global $container;
45
46
        return ($container->get(ExportTransactionLinesAction::class))->generate($query);
47
    }
48
}
49