AbstractSorter::order()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace TRex\Collection;
3
4
abstract class AbstractSorter
5
{
6
    const ASC_ORDER = 1;
7
    const DESC_ORDER = -1;
8
9
    /**
10
     * @var int
11
     */
12
    private $order;
13
14
    /**
15
     * @param int $order
16
     */
17 2
    public function __construct($order = self::ASC_ORDER)
18
    {
19 2
        $this->order = $order;
20 2
    }
21
22
    /**
23
     * @param mixed $object1
24
     * @param mixed $object2
25
     * @return int
26
     */
27 2
    public function __invoke($object1, $object2)
28
    {
29 2
        return $this->compare($this->invoke($object1), $this->invoke($object2));
30
    }
31
32
    /**
33
     * calls the property/method/key to extract the value to sort.
34
     *
35
     * @param mixed $object
36
     * @return mixed
37
     */
38
    abstract protected function invoke($object);
39
40
    /**
41
     * @param mixed $value1
42
     * @param mixed $value2
43
     * @return int
44
     */
45 2
    private function compare($value1, $value2)
46
    {
47 2
        if ($value1 == $value2) {
48 2
            return 0;
49
        }
50 2
        return $this->order(($value1 < $value2) ? -1 : 1);
51
    }
52
53
    /**
54
     * @param int $value
55
     * @return int
56
     */
57 2
    private function order($value)
58
    {
59 2
        return $this->order * $value;
60
    }
61
}
62
63