UnarySorter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A apply() 0 19 4
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Listing\Sorters;
9
10
use Spiral\Database\Builders\SelectQuery;
11
use Spiral\Listing\Dependency;
12
use Spiral\Listing\Prototypes\DependedModificator;
13
use Spiral\Listing\SorterInterface;
14
use Spiral\Listing\Traits\SelectorValidationTrait;
15
use Spiral\ORM\Entities\RecordSelector;
16
17
/**
18
 * Simple sorter with ability to specify sorting fields/columns in array form. Compatible with
19
 * both ORM and ODM selectors.
20
 */
21
class UnarySorter extends DependedModificator implements SorterInterface
22
{
23
    use SelectorValidationTrait;
24
25
    /**
26
     * Expression in a form or [column => direction, ...]
27
     *
28
     * @var array
29
     */
30
    private $sortBy = [];
31
32
    /**
33
     * @param array            $sortBy
34
     * @param array|Dependency $dependencies
35
     */
36
    public function __construct(array $sortBy, $dependencies = [])
37
    {
38
        parent::__construct($dependencies);
0 ignored issues
show
Bug introduced by
It seems like $dependencies defined by parameter $dependencies on line 36 can also be of type array; however, Spiral\Listing\Prototype...ificator::__construct() does only seem to accept array<integer,object<Spi...ral\Listing\Dependency>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
39
        $this->sortBy = $sortBy;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function apply($selector)
46
    {
47
        $this->validateSelector($selector);
48
49
        if ($selector instanceof RecordSelector) {
50
            $selector = $this->loadDependencies($selector);
51
52
            foreach ($this->sortBy as $expression => $direction) {
53
                $selector = $selector->orderBy(
54
                    $expression,
55
                    $direction == self::ASC ? SelectQuery::SORT_ASC : SelectQuery::SORT_DESC
56
                );
57
            }
58
59
            return $selector;
60
        }
61
62
        return $selector->sortBy($this->sortBy);
63
    }
64
}
65