Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

PublicScope   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 24
rs 10
c 0
b 0
f 0
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\Modules\Blog\Comment\Scope;
6
7
use Cycle\ORM\Select\QueryBuilder;
8
use Cycle\ORM\Select\ScopeInterface as ConstrainInterface;
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
    public function __construct(?array $publicOrCondition = null)
20
    {
21
        $this->publicOrCondition = $publicOrCondition;
22
    }
23
24
    public function apply(QueryBuilder $query): void
25
    {
26
        // public or...
27
        if ($this->publicOrCondition !== null) {
28
            $query->where([
29
                '@or' => [
30
                    ['public' => true],
31
                    $this->publicOrCondition,
32
                ],
33
            ]);
34
        } else {
35
            $query->andWhere('public', '=', true);
36
        }
37
        // sort
38
        $query->orderBy('published_at', 'DESC');
39
    }
40
}
41