Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

ComponentPositionSortValueHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 28
ccs 0
cts 16
cp 0
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B calculateSortValue() 0 26 7
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\ComponentPosition;
15
16
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
17
18
/**
19
 * @author Daniel West <[email protected]>
20
 */
21
class ComponentPositionSortValueHelper
22
{
23
    public function calculateSortValue(ComponentPosition $componentPosition): void
24
    {
25
        $sortCollection = $componentPosition->getSortCollection();
26
        $sortValueSet = null !== $componentPosition->sortValue;
27
        if (!$sortCollection) {
28
            if (!$sortValueSet) {
29
                $componentPosition->setSortValue(0);
30
            }
31
32
            return;
33
        }
34
35
        if (!$sortValueSet) {
36
            /** @var ComponentPosition|null $lastPosition */
37
            $lastPosition = $sortCollection->last();
38
            if ($lastPosition) {
39
                $nextValue = $lastPosition->sortValue + 1;
40
                $componentPosition->setSortValue($nextValue);
41
            } else {
42
                $componentPosition->setSortValue(0);
43
            }
44
        }
45
46
        foreach ($sortCollection as $existingComponentPosition) {
47
            if ($existingComponentPosition->sortValue >= $componentPosition->sortValue) {
48
                ++$existingComponentPosition->sortValue;
49
            }
50
        }
51
    }
52
}
53