SelectorComparator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A selector() 0 4 1
A direction() 0 4 1
A compare() 0 7 1
A reverse() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Comparable;
12
13
use Cubiche\Core\Delegate\Delegate;
14
15
/**
16
 * Selector Comparator class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 */
20
class SelectorComparator extends Comparator
21
{
22
    /**
23
     * @var Delegate
24
     */
25
    protected $selector;
26
27
    /**
28
     * @var Direction
29
     */
30
    protected $direction;
31
32
    /**
33
     * @param callable  $selector
34
     * @param Direction $direction
35
     */
36
    public function __construct(callable $selector, Direction $direction = null)
37
    {
38
        $this->selector = new Delegate($selector);
39
        $this->direction = Direction::ensure($direction);
40
    }
41
42
    /**
43
     * @return callable
44
     */
45
    public function selector()
46
    {
47
        return $this->selector->target();
48
    }
49
50
    /**
51
     * @return \Cubiche\Core\Comparable\Direction
52
     */
53
    public function direction()
54
    {
55
        return $this->direction;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function compare($a, $b)
62
    {
63
        return $this->direction()->value() * parent::compare(
64
            $this->selector->__invoke($a),
65
            $this->selector->__invoke($b)
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function reverse()
73
    {
74
        return new self($this->selector(), $this->direction()->reverse());
75
    }
76
}
77