Passed
Pull Request — master (#185)
by
unknown
02:05
created

ScopeTrait::applyScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Select\Traits;
13
14
use Cycle\ORM\Select\AbstractLoader;
15
use Cycle\ORM\Select\ConstrainInterface;
16
use Cycle\ORM\Select\QueryBuilder;
17
use Cycle\ORM\Select\ScopeInterface;
18
use Spiral\Database\Query\SelectQuery;
19
20
/**
21
 * Provides the ability to assign the scope to the AbstractLoader.
22
 */
23
trait ScopeTrait
24
{
25
    /** @var null|ScopeInterface */
26
    protected $scope;
27
28
    public function getScope(): ?ScopeInterface
29
    {
30
        return $this->scope;
31
    }
32
33
    /**
34
     * Associate scope with the selector.
35
     * @param ScopeInterface|null $scope
36
     */
37
    public function setScope(?ScopeInterface $scope): void
38
    {
39
        $this->scope = $scope;
40
    }
41
42
    /**
43
     * @deprecated Use {@see setScope()} instead.
44
     * @param ConstrainInterface|null $constrain
45
     * @return AbstractLoader|$this
46
     */
47
    public function setConstrain(?ConstrainInterface $constrain = null): self
48
    {
49
        $this->setScope($constrain);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param SelectQuery $query
56
     * @return SelectQuery
57
     */
58
    protected function applyScope(SelectQuery $query): SelectQuery
59
    {
60
        if ($this->scope !== null) {
61
            $this->scope->apply(new QueryBuilder($query, $this));
62
        }
63
64
        return $query;
65
    }
66
}
67