Completed
Push — master ( 0664ed...cfd45d )
by Karsten
03:27 queued 26s
created

SortByOperation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 25
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 18 3
1
<?php
2
/**
3
 * File was created 12.06.2015 12:38
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi\Operation\FullSet;
9
10
use PeekAndPoke\Component\Psi\FullSetOperation;
11
use PeekAndPoke\Component\Psi\Operation\AbstractUnaryFunctionOperation;
12
13
/**
14
 * SortByOperation
15
 *
16
 * @author Karsten J. Gerber <[email protected]>
17
 */
18
class SortByOperation extends AbstractUnaryFunctionOperation implements FullSetOperation
19
{
20
    /**
21
     * @param \Iterator $set
22
     *
23
     * @return \Iterator
24
     */
25 2
    public function apply(\Iterator $set)
26
    {
27 2
        $func = $this->function;
28 2
        $data = iterator_to_array($set);
29
30 2
        usort($data, function ($i1, $i2) use ($func) {
31
32 2
            $val1 = $func($i1);
33 2
            $val2 = $func($i2);
34
35 2
            if ($val1 === $val2) {
36
                return 0;
37
            }
38
39 2
            return $val1 > $val2 ? 1 : -1;
40 2
        });
41
42 2
        return new \ArrayIterator($data);
43
    }
44
}
45