SectionsSorter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 29
rs 10
c 2
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateSectionsArray() 0 15 3
A sortBySections() 0 10 2
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Sorter;
12
13
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
14
use Webmozart\Assert\Assert;
15
16
final class SectionsSorter implements SectionsSorterInterface
17
{
18
    public function sortBySections(array $pages): array
19
    {
20
        $result = [];
21
22
        /** @var PageInterface $page */
23
        foreach ($pages as $page) {
24
            $result = $this->updateSectionsArray($page, $result);
25
        }
26
27
        return $result;
28
    }
29
30
    private function updateSectionsArray(PageInterface $page, array $currentResult): array
31
    {
32
        Assert::isIterable($page->getSections());
33
        foreach ($page->getSections() as $section) {
34
            $sectionCode = $section->getCode();
35
            Assert::notNull($sectionCode);
36
            if (!array_key_exists($sectionCode, $currentResult)) {
37
                $currentResult[$sectionCode] = [];
38
                $currentResult[$sectionCode]['section'] = $section;
39
            }
40
41
            $currentResult[$sectionCode][] = $page;
42
        }
43
44
        return $currentResult;
45
    }
46
}
47