Passed
Push — tests ( bccf67...74f795 )
by Willem
04:13
created

CategoryLayoutUpdateManager::fetchAvailableFiles()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Test\src;
5
6
use Magento\Catalog\Api\Data\CategoryInterface;
7
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
8
9
/**
10
 * Easy way to fake available files.
11
 */
12
class CategoryLayoutUpdateManager extends LayoutUpdateManager
13
{
14
    /**
15
     * @var array Keys are category IDs, values - file names.
16
     */
17
    private $fakeFiles = [];
18
19
    /**
20
     * Supply fake files for a Category.
21
     *
22
     * @param int $forCategoryId
23
     * @param string[]|null $files Pass null to reset.
24
     */
25
    public function setFakeFiles(int $forCategoryId, ?array $files): void
26
    {
27
        if ($files === null) {
0 ignored issues
show
introduced by
The condition $files === null is always false.
Loading history...
28
            unset($this->fakeFiles[$forCategoryId]);
29
        } else {
30
            $this->fakeFiles[$forCategoryId] = $files;
31
        }
32
    }
33
34
    /**
35
     * Fetches fake/mock files added through $this->setCategoryFakeFiles()
36
     * for current Category and Global (0)
37
     *
38
     * If none found, fall back to original method
39
     *
40
     * @param CategoryInterface $category
41
     * @return array
42
     */
43
    public function fetchAvailableFiles(CategoryInterface $category): array
44
    {
45
        return array_unique(
46
            array_merge(
47
                ($this->fakeFiles[$category->getId()] ?? []),
48
                ($this->fakeFiles[0] ?? [])
49
            )
50
        ) ?: parent::fetchAvailableFiles($category);
51
    }
52
}
53