Passed
Push — master ( 4e7aa8...dae217 )
by Adrien
35:16 queued 30:13
created

TransactionLineRepository::importedIdExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Handler\ExportTransactionLinesHandler;
8
use Application\Model\User;
9
use Doctrine\ORM\Query;
10
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
11
12
class TransactionLineRepository extends AbstractRepository implements ExportExcelInterface, LimitedAccessSubQuery
13
{
14
    /**
15
     * Returns pure SQL to get ID of all objects that are accessible to given user.
16
     *
17
     * @param null|User $user
18
     */
19 14
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
20
    {
21 14
        if (!$user) {
22 2
            return '-1';
23
        }
24
25 12
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
26 9
            return $this->getAllIdsQuery();
27
        }
28
29 3
        if ($user->getOwner()) {
30 1
            $id = $user->getOwner()->getId();
31
        } else {
32 2
            $id = $user->getId();
33
        }
34
35
        return 'SELECT transaction_line.id FROM transaction_line
36
              JOIN account ON transaction_line.debit_id = account.id OR transaction_line.credit_id = account.id 
37 3
              WHERE account.owner_id = ' . $id;
38
    }
39
40
    /**
41
     * Generates an Excel spreadsheet with the query result
42
     *
43
     * @return string name of the temporary file
44
     */
45
    public function exportExcel(Query $query): string
46
    {
47
        global $container;
48
49
        return ($container->get(ExportTransactionLinesHandler::class))->generate($query);
50
    }
51
52 4
    public function importedIdExists(string $importedId): bool
53
    {
54 4
        $connection = $this->getEntityManager()->getConnection();
55 4
        $count = $connection->fetchOne('SELECT COUNT(*) > 0 FROM transaction_line WHERE imported_id = :importedId', ['importedId' => $importedId]);
56
57 4
        return (bool) $count;
58
    }
59
}
60