Completed
Pull Request — master (#6)
by Willem
07:24 queued 04:18
created

ProductLayoutUpdateManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchAvailableFiles() 0 8 2
A setFakeFiles() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Test\Util;
5
6
use Magento\Catalog\Api\Data\ProductInterface;
7
use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager;
8
9
/**
10
 * Easy way to fake available files.
11
 */
12
class ProductLayoutUpdateManager extends LayoutUpdateManager
13
{
14
    /**
15
     * @var array Keys are Product IDs, values - file names.
16
     */
17
    private $fakeFiles = [];
18
19
    /**
20
     * Supply fake files for a Product.
21
     *
22
     * @param int $forProductId
23
     * @param string[]|null $files Pass null to reset.
24
     */
25
    public function setFakeFiles(int $forProductId, ?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[$forProductId]);
29
        } else {
30
            $this->fakeFiles[$forProductId] = $files;
31
        }
32
    }
33
34
    /**
35
     * Fetches fake/mock files added through $this->setFakeFiles()
36
     * for current Product and Global (0)
37
     *
38
     * If none found, fall back to original method
39
     *
40
     * @param ProductInterface $product
41
     * @return array
42
     */
43
    public function fetchAvailableFiles(ProductInterface $product): array
44
    {
45
        return array_unique(
46
            array_merge(
47
                ($this->fakeFiles[$product->getId()] ?? []),
48
                ($this->fakeFiles[0] ?? [])
49
            )
50
        ) ?: parent::fetchAvailableFiles($product);
51
    }
52
}
53