MultiComparator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 56
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A firstComparator() 0 4 1
A secondComparator() 0 4 1
A compare() 0 6 2
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
/**
14
 * Multi Comparator class.
15
 *
16
 * @author Karel Osorio Ramírez <[email protected]>
17
 */
18
class MultiComparator extends Comparator
19
{
20
    /**
21
     * @var ComparatorInterface
22
     */
23
    protected $firstComparator;
24
25
    /**
26
     * @var ComparatorInterface
27
     */
28
    protected $secondComparator;
29
30
    /**
31
     * @param callable $firstComparator
32
     * @param callable $secondComparator
33
     */
34
    public function __construct(callable $firstComparator, callable $secondComparator)
35
    {
36
        $this->firstComparator = self::from($firstComparator);
37
        $this->secondComparator = self::from($secondComparator);
38
    }
39
40
    /**
41
     * @return \Cubiche\Core\Comparable\ComparatorInterface
42
     */
43
    public function firstComparator()
44
    {
45
        return $this->firstComparator;
46
    }
47
48
    /**
49
     * @return \Cubiche\Core\Comparable\ComparatorInterface
50
     */
51
    public function secondComparator()
52
    {
53
        return $this->secondComparator;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function compare($a, $b)
60
    {
61
        $result = $this->firstComparator()->compare($a, $b);
62
63
        return $result !== 0 ? $result : $this->secondComparator()->compare($a, $b);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function reverse()
70
    {
71
        return new self($this->firstComparator()->reverse(), $this->secondComparator()->reverse());
72
    }
73
}
74