Passed
Push — master ( 6a1b66...1520a2 )
by Przemysław eRIZ
04:33
created

SectionsSorter::updateSectionsArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
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