SortService::sortArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * Class description
4
 *
5
 * @package HDNET\CacheCheck\Service
6
 * @author  Julian Seitz
7
 */
8
9
namespace HDNET\CacheCheck\Service;
10
11
use HDNET\CacheCheck\Domain\Model\Cache;
12
use TYPO3\CMS\Core\Utility\MathUtility;
13
14
/**
15
 * Class SortService
16
 *
17
 */
18
class SortService extends AbstractService
19
{
20
21
    /**
22
     * First sorting of caches
23
     *
24
     * @param Cache $a
25
     * @param Cache $b
26
     *
27
     * @return int
28
     */
29
    public function cmp(Cache $a, Cache $b)
30
    {
31
        // current in analyse mode
32
        if ($a->getIsInAnalyseMode() !== $b->getIsInAnalyseMode()) {
33
            return $a->getIsInAnalyseMode() ? -1 : 1;
34
        }
35
36
        // check if the item has Dynamic information
37
        if ((bool)$a->getDynamicKpi() !== (bool)$b->getDynamicKpi()) {
38
            return (bool)$a->getDynamicKpi() ? -1 : 1;
39
        }
40
41
        return MathUtility::forceIntegerInRange(strcmp($a->getName(), $b->getName()), -1, 1, 0);
42
    }
43
44
    /**
45
     * Sorting of given array
46
     *
47
     * @param array $array
48
     *
49
     * @return array
50
     */
51
    public function sortArray($array)
52
    {
53
        usort($array, [
54
            $this,
55
            'cmp'
56
        ]);
57
        return $array;
58
    }
59
}
60