Completed
Push — master ( 1c4fa3...04dec7 )
by Willem
24s queued 11s
created

CategoryLayoutUpdateManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setFakeFiles() 0 6 2
A fetchAvailableFiles() 0 8 2
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