NewsRepository::getAccessibleSubQuery()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\News;
8
use Application\Model\User;
9
10
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
11
12
/**
13
 * @extends AbstractRepository<News>
14
 */
15
class NewsRepository extends AbstractRepository implements LimitedAccessSubQuery
16
{
17
    /**
18
     * Returns pure SQL to get ID of all objects that are accessible to given user.
19
     *
20
     * A user can access a news if at least one of the following conditions is true:
21
     *
22
     * - he is administrator
23
     * - the news is active
24
     *
25
     * @param null|User $user
26
     */
27
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
28
    {
29
        if ($user && $user->getRole() === User::ROLE_ADMINISTRATOR) {
30
            return '';
31
        }
32
33
        return 'SELECT id FROM news WHERE is_active';
34
    }
35
36
    public function getIds(): array
37
    {
38
        $query = $this->createQueryBuilder('news')
39
            ->select('news.id')
40
            ->where('news.date < CURRENT_TIMESTAMP()')
41
            ->andWhere('news.isActive = true')
42
            ->orderBy('news.id')
43
            ->getQuery();
44
45
        $result = $query->getArrayResult();
46
47
        return $result;
48
    }
49
}
50