Completed
Push — master ( 3a917b...8faa80 )
by Alexander
15s queued 12s
created

PublicScope::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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