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

TransactionLineRepository::exportExcel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 5
c 0
b 0
f 0
cc 1
ccs 0
cts 3
cp 0
crap 2
rs 10
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