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

TransactionLineRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 46
ccs 13
cts 15
cp 0.8667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessibleSubQuery() 0 19 4
A exportExcel() 0 5 1
A importedIdExists() 0 6 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