AbstractSorter::sort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace PHPExtra\Sorter;
4
5
use PHPExtra\Sorter\Strategy\SimpleSortStrategy;
6
use PHPExtra\Sorter\Strategy\StrategyInterface;
7
8
/**
9
 * The AbstractSorterInterface class
10
 *
11
 * @author Jacek Kobus <[email protected]>
12
 */
13
abstract class AbstractSorter implements SorterInterface
14
{
15
    /**
16
     * @var StrategyInterface
17
     */
18
    private $strategy;
19
20
    /**
21
     * Create new sorter with given strategy
22
     * If no strategy given, default StringArraySortStrategy will be used
23
     *
24
     * @see SimpleSortStrategy
25
     * @see ComplexSortStrategy
26
     *
27
     * @param StrategyInterface $strategy
28
     */
29 6
    function __construct(StrategyInterface $strategy = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31 6
        if (!$strategy) {
32 5
            $strategy = new SimpleSortStrategy();
33 5
        }
34
35 6
        $this->setStrategy($strategy);
36 6
    }
37
38
    /**
39
     * @param StrategyInterface $strategy
40
     *
41
     * @return $this
42
     */
43 6
    public function setStrategy($strategy)
44
    {
45 6
        $this->strategy = $strategy;
46
47 6
        return $this;
48
    }
49
50
    /**
51
     * Set sort order
52
     *
53
     * @param int $order
54
     *
55
     * @return $this
56
     */
57 1
    public function setSortOrder($order)
58
    {
59 1
        $this->strategy->setSortOrder($order);
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 3
    public function sort(array $collection)
68
    {
69 3
        if (!$this->strategy) {
70
            throw new \RuntimeException('Strategy was not defined');
71
        }
72
73 3
        return $this->strategy->sort($collection);
74
    }
75
}