Failed Conditions
Push — master ( a427b2...509ea4 )
by Adrien
16:44
created

LogRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
dl 0
loc 37
ccs 4
cts 9
cp 0.4444
rs 10
c 2
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAccessibleSubQuery() 0 17 3
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
    final public const DOOR_OPENED = 'door opened: ';
16
    /**
17
     * Log message to be used when the datatrans webhook starts.
18
     */
19
    final public const DATATRANS_WEBHOOK_BEGIN = 'datatrans webhook begin';
20
    /**
21
     * Log message to be used when the datatrans webhook finishes.
22
     */
23
    final public const DATATRANS_WEBHOOK_END = 'datatrans webhook end';
24
25
    use \Ecodev\Felix\Repository\Traits\LogRepository;
26
27
    /**
28
     * Returns pure SQL to get ID of all objects that are accessible to given user.
29
     */
30 1
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
31
    {
32 1
        if (!$user) {
33
            return '-1';
34
        }
35
36
        // Sysops and responsible can read all logs
37 1
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
38 1
            return $this->getAllIdsQuery();
39
        }
40
41
        $subquery = '
42
            SELECT log.id FROM `log` WHERE
43
            message LIKE ' . $this->getEntityManager()->getConnection()->quote(self::DOOR_OPENED . '%') . '
44
            AND log.creator_id = ' . $user->getId();
45
46
        return $subquery;
47
    }
48
}
49