Passed
Pull Request — master (#448)
by
unknown
13:50
created

PublicScope   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 24
ccs 6
cts 9
cp 0.6667
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A apply() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Comment\Scope;
6
7
use Cycle\ORM\Select\ScopeInterface as ConstrainInterface;
8
use Cycle\ORM\Select\QueryBuilder;
9
10
/**
11
 * Not deleted
12
 * Public with condition
13
 * Sorted
14
 */
15
final class PublicScope implements ConstrainInterface
16
{
17
    private ?array $publicOrCondition;
18
19 6
    public function __construct(?array $publicOrCondition = null)
20
    {
21 6
        $this->publicOrCondition = $publicOrCondition;
22
    }
23
24 1
    public function apply(QueryBuilder $query): void
25
    {
26
        // public or...
27 1
        if ($this->publicOrCondition !== null) {
28
            $query->where([
29
                '@or' => [
30
                    ['public' => true],
31
                    $this->publicOrCondition,
32
                ],
33
            ]);
34
        } else {
35 1
            $query->andWhere('public', '=', true);
36
        }
37
        // sort
38 1
        $query->orderBy('published_at', 'DESC');
39
    }
40
}
41