Passed
Pull Request — master (#209)
by Aleksei
03:39
created

ScopeTrait::setScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select\Traits;
6
7
use Cycle\ORM\Select\AbstractLoader;
8
use Cycle\ORM\Select\ConstrainInterface;
9
use Cycle\ORM\Select\QueryBuilder;
10
use Cycle\ORM\Select\ScopeInterface;
11
use Spiral\Database\Query\SelectQuery;
12
13
/**
14
 * Provides the ability to assign the scope to the AbstractLoader.
15
 */
16
trait ScopeTrait
17
{
18
    /** @var null|ScopeInterface */
19
    protected $constrain;
20
21
    abstract public function getAlias(): string;
22
23
    /**
24
     * Associate scope with the selector.
25
     *
26
     * @return AbstractLoader|$this
27
     */
28
    public function setScope(ScopeInterface $scope = null): self
29
    {
30
        $this->constrain = $scope;
31
        return $this;
32
    }
33
34
    /**
35
     * @deprecated Use {@see setScope()} instead.
36
     *
37
     * @return AbstractLoader|$this
38
     */
39
    public function setConstrain(ConstrainInterface $constrain = null): self
40
    {
41
        return $this->setScope($constrain);
42
    }
43
44
    protected function applyScope(SelectQuery $query): SelectQuery
45
    {
46
        if ($this->constrain !== null) {
47
            $this->constrain->apply(new QueryBuilder($query, $this));
0 ignored issues
show
Bug introduced by
$this of type Cycle\ORM\Select\Traits\ScopeTrait is incompatible with the type Cycle\ORM\Select\AbstractLoader expected by parameter $loader of Cycle\ORM\Select\QueryBuilder::__construct(). ( Ignorable by Annotation )

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

47
            $this->constrain->apply(new QueryBuilder($query, /** @scrutinizer ignore-type */ $this));
Loading history...
48
        }
49
50
        return $query;
51
    }
52
53
    /**
54
     * @deprecated Use {@see applyScope()} instead.
55
     */
56
    protected function applyConstrain(SelectQuery $query): SelectQuery
57
    {
58
        return $this->applyScope($query);
59
    }
60
}
61