Failed Conditions
Push — master ( 51347b...4787ce )
by Adrien
08:37
created

LogRepository::getAccessibleSubQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 4
cts 8
cp 0.5
crap 4.125
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\User;
8
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
9
10
class LogRepository extends AbstractRepository implements \Ecodev\Felix\Repository\LogRepository, LimitedAccessSubQuery
11
{
12
    /**
13
     * Log message to be used when a door is opened
14
     */
15
    const DOOR_OPENED = 'door opened: ';
16
17
    use \Ecodev\Felix\Repository\Traits\LogRepository;
18
19
    /**
20
     * Returns pure SQL to get ID of all objects that are accessible to given user.
21
     */
22 1
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
23
    {
24 1
        if (!$user) {
25
            return '-1';
26
        }
27
28
        // Sysops and responsible can read all logs
29 1
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
30 1
            return $this->getAllIdsQuery();
31
        }
32
33
        $subquery = '
34
            SELECT log.id FROM `log` WHERE
35
            message LIKE ' . $this->getEntityManager()->getConnection()->quote(self::DOOR_OPENED . '%') . '
36
            AND log.creator_id = ' . $user->getId();
37
38
        return $subquery;
39
    }
40
}
41