Test Failed
Push — develop ( 05150f...e72854 )
by Daniel
04:23
created

PublishableFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 22
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setExpressionBuilder() 0 3 1
A getWhereStatement() 0 18 1
A addFilterConstraint() 0 11 3
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Filter\Doctrine;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Doctrine\ORM\Query\Expr;
7
use Doctrine\ORM\Query\Filter\SQLFilter;
8
use Silverback\ApiComponentBundle\Entity\PublishableInterface;
9
10
final class PublishableFilter extends SQLFilter
11
{
12
    /** @var Expr */
13
    private $exprBuilder;
14
15
    public function setExpressionBuilder(Expr $exprBuilder): void
16
    {
17
        $this->exprBuilder = $exprBuilder;
18
    }
19
20
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
21
    {
22
        if (null === $this->exprBuilder) {
23
            throw new \RuntimeException(sprintf('An expression builder. Be sure to call "%s::setExpressionBuilder()".', __CLASS__));
24
        }
25
26
        $reflection = $targetEntity->getReflectionClass();
27
        if (!$reflection->implementsInterface(PublishableInterface::class)) {
28
            return '';
29
        }
30
        return $this->getWhereStatement($targetTableAlias);
31
    }
32
33
    private function getWhereStatement(string $alias): string
34
    {
35
        $this->setParameter('published', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of Doctrine\ORM\Query\Filte...LFilter::setParameter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->setParameter('published', /** @scrutinizer ignore-type */ true);
Loading history...
36
        $this->setParameter('published_date', date('Y-m-d H:i:s'));
37
38
        $pColumn = sprintf('%s.published', $alias);
39
        $stmt = '(' . $this->exprBuilder->orX(
40
            $this->exprBuilder->isNull($pColumn),
41
            $this->exprBuilder->eq($pColumn, $this->getParameter('published'))
42
        ) . ')';
43
44
        $pdColumn = sprintf('%s.published_date', $alias);
45
        $stmt .= ' AND (' . $this->exprBuilder->orX(
46
            $this->exprBuilder->isNull($pdColumn),
47
            $this->exprBuilder->gte($pdColumn, $this->getParameter('published_date'))
48
        ) . ')';
49
50
        return $stmt;
51
    }
52
}
53