Completed
Push — master ( 0f478c...b73fd3 )
by Kamil
14:27 queued 08:29
created

TemplateBlockRegistry::findFinalizedForEvents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\UiBundle\Registry;
15
16
use Zend\Stdlib\SplPriorityQueue;
17
18
final class TemplateBlockRegistry implements TemplateBlockRegistryInterface
19
{
20
    /**
21
     * Blocks within an event should be sorted by their priority descending.
22
     *
23
     * @var TemplateBlock[][]
24
     * @psalm-var array<string, array<string, TemplateBlock>>
25
     */
26
    private $eventsToTemplateBlocks;
27
28
    public function __construct(array $eventsToTemplateBlocks)
29
    {
30
        $this->eventsToTemplateBlocks = $eventsToTemplateBlocks;
31
    }
32
33
    public function all(): array
34
    {
35
        return $this->eventsToTemplateBlocks;
36
    }
37
38
    public function findEnabledForEvents(array $eventNames): array
39
    {
40
        // No need to sort blocks again if there's only one event because we have them sorted already
41
        if (count($eventNames) === 1) {
42
            $eventName = reset($eventNames);
43
44
            return array_values(array_filter(
45
                $this->eventsToTemplateBlocks[$eventName] ?? [],
46
                static function (TemplateBlock $templateBlock): bool {
47
                    return $templateBlock->isEnabled();
48
                }
49
            ));
50
        }
51
52
        $enabledFinalizedTemplateBlocks = array_filter(
53
            $this->findFinalizedForEvents($eventNames),
54
            static function (TemplateBlock $templateBlock): bool {
55
                return $templateBlock->isEnabled();
56
            }
57
        );
58
59
        $templateBlocksPriorityQueue = new SplPriorityQueue();
60
        foreach ($enabledFinalizedTemplateBlocks as $templateBlock) {
61
            $templateBlocksPriorityQueue->insert($templateBlock, $templateBlock->getPriority());
62
        }
63
64
        return $templateBlocksPriorityQueue->toArray();
65
    }
66
67
    /**
68
     * @param string[] $eventNames
69
     * @psalm-param non-empty-list<string> $eventNames
70
     *
71
     * @return TemplateBlock[]
72
     */
73
    private function findFinalizedForEvents(array $eventNames): array
74
    {
75
        /**
76
         * @var TemplateBlock[]
77
         * @psalm-var array<string, TemplateBlock> $finalizedTemplateBlocks
78
         */
79
        $finalizedTemplateBlocks = [];
80
        $reversedEventNames = array_reverse($eventNames);
81
        foreach ($reversedEventNames as $eventName) {
82
            $templateBlocks = $this->eventsToTemplateBlocks[$eventName] ?? [];
83
            foreach ($templateBlocks as $blockName => $templateBlock) {
84
                if (array_key_exists($blockName, $finalizedTemplateBlocks)) {
85
                    $templateBlock = $finalizedTemplateBlocks[$blockName]->overwriteWith($templateBlock);
86
                }
87
88
                $finalizedTemplateBlocks[$blockName] = $templateBlock;
89
            }
90
        }
91
92
        return $finalizedTemplateBlocks;
93
    }
94
}
95