Completed
Push — master ( abc7fc...ef6c42 )
by Anton
02:25
created

ConstrainTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureWindow() 0 11 3
getAlias() 0 1 ?
1
<?php
2
/**
3
 * orm
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Loaders\Traits;
9
10
use Spiral\Database\Builders\SelectQuery;
11
use Spiral\ORM\Helpers\AliasDecorator;
12
13
/**
14
 * Provides ability to set LIMIT and ORDER BY in relation loaders.
15
 */
16
trait ConstrainTrait
17
{
18
    /**
19
     * @param \Spiral\Database\Builders\SelectQuery $query
20
     *
21
     * @param array                                 $orderBy
22
     * @param int                                   $limit 0 when no selection.
23
     */
24
    private function configureWindow(SelectQuery $query, array $orderBy, int $limit = 0)
25
    {
26
        if (!empty($orderBy)) {
27
            $decorator = new AliasDecorator($query, 'where', $this->getAlias());
28
            $decorator->orderBy($orderBy);
29
        }
30
31
        if ($limit !== 0) {
32
            $query->limit($limit);
33
        }
34
    }
35
36
    /**
37
     * Joined table alias.
38
     *
39
     * @return string
40
     */
41
    abstract protected function getAlias(): string;
42
}