NewsRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 33
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessibleSubQuery() 0 7 3
A getIds() 0 12 1
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