AbstractSorter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 2 Features 2
Metric Value
wmc 6
c 3
b 2
f 2
lcom 1
cbo 2
dl 0
loc 63
ccs 15
cts 16
cp 0.9375
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setStrategy() 0 6 1
A setSortOrder() 0 6 1
A sort() 0 8 2
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
}