Completed
Push — tests ( c1270e...84083a )
by Willem
03:47
created

ProductLayoutPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\GlobalCustomLayout\Plugin;
5
6
use Magento\Catalog\Api\Data\CategoryInterface;
7
use Magento\Catalog\Api\Data\ProductInterface;
8
use Magento\Catalog\Model\Product;
9
use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager;
10
use Magento\Framework\App\Area;
11
use Magento\Framework\DataObject;
12
use Magento\Framework\View\Design\Theme\FlyweightFactory;
13
use Magento\Framework\View\DesignInterface;
14
use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor;
15
use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\View\Model\Layout\MergeFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class ProductLayoutPlugin {
18
19
    /**
20
     * @var FlyweightFactory
21
     */
22
    private $themeFactory;
23
24
    /**
25
     * @var DesignInterface
26
     */
27
    private $design;
28
29
    /**
30
     * @var LayoutProcessorFactory
31
     */
32
    private $layoutProcessorFactory;
33
34
    /**
35
     * @var LayoutProcessor|null
36
     */
37
    private $layoutProcessor;
38
39
    /**
40
     * @param FlyweightFactory $themeFactory
41
     * @param DesignInterface $design
42
     * @param LayoutProcessorFactory $layoutProcessorFactory
43
     */
44 3
    public function __construct(
45
        FlyweightFactory $themeFactory,
46
        DesignInterface $design,
47
        LayoutProcessorFactory $layoutProcessorFactory
48
    ) {
49 3
        $this->themeFactory = $themeFactory;
50 3
        $this->design = $design;
51 3
        $this->layoutProcessorFactory = $layoutProcessorFactory;
52 3
    }
53
54
    /**
55
     * Get the processor instance.
56
     *
57
     * @return LayoutProcessor
58
     *
59
     * Unchanged private method copied over from @var LayoutUpdateManager
60
     */
61 3
    private function getLayoutProcessor(): LayoutProcessor
62
    {
63 3
        if (!$this->layoutProcessor) {
64 3
            $this->layoutProcessor = $this->layoutProcessorFactory->create(
65
                [
66 3
                    'theme' => $this->themeFactory->create(
67 3
                        $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
68
                    )
69
                ]
70
            );
71 3
            $this->themeFactory = null;
72 3
            $this->design = null;
73
        }
74
75 3
        return $this->layoutProcessor;
76
    }
77
78
    /**
79
     * Fetch list of available global files/handles for the product.
80
     *
81
     * @param LayoutUpdateManager $subject
82
     * @param array $result
83
     * @param ProductInterface $product
84
     * @return array
85
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
86
     */
87 3
    public function afterFetchAvailableFiles(
88
        LayoutUpdateManager $subject,
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
        /** @scrutinizer ignore-unused */ LayoutUpdateManager $subject,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
        array $result,
90
        ProductInterface $product
91
    ): array {
92 3
        if (!$product->getSku()) {
93
            return [];
94
        }
95
96 3
        $handles = $this->getLayoutProcessor()->getAvailableHandles();
97
98 3
        return array_merge($result, array_filter(
99 3
            array_map(
100 3
                function(string $handle) : ?string {
101 3
                    preg_match(
102 3
                        '/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
103 1
                        $handle,
104 1
                        $selectable
105
                    );
106 3
                    if (!empty($selectable[1])) {
107
                        return $selectable[1];
108
                    }
109
110 3
                    return null;
111 3
                },
112 1
                $handles
113
            )
114
        ));
115
    }
116
117
    /**
118
     * Extract selected global custom layout settings.
119
     *
120
     * If no update is selected none will apply.
121
     *
122
     * @param LayoutUpdateManager $subject
123
     * @param $result
124
     * @param ProductInterface $product
125
     * @param DataObject $intoSettings
126
     * @return void
127
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
128
     */
129 3
    public function afterExtractCustomSettings(
130
        LayoutUpdateManager $subject,
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
        /** @scrutinizer ignore-unused */ LayoutUpdateManager $subject,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
        $result,
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
        /** @scrutinizer ignore-unused */ $result,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
        ProductInterface $product,
133
        DataObject $intoSettings
134
    ): void {
135 3
        if ($product->getSku() && $value = $this->extractAttributeValue($product)) {
136 3
            $handles = $intoSettings->getPageLayoutHandles() ?? [];
137 3
            $handles = array_merge_recursive(
138 3
                $handles,
139 3
                ['selectable_0' => $value]
140
            );
141 3
            $intoSettings->setPageLayoutHandles($handles);
142
        }
143 3
    }
144
145
    /**
146
     * Extract custom layout attribute value.
147
     *
148
     * @param ProductInterface $product
149
     * @return mixed
150
     *
151
     * Unchanged private method copied over from @var LayoutUpdateManager
152
     */
153 3
    private function extractAttributeValue(ProductInterface $product)
154
    {
155 3
        if ($product instanceof Product && !$product->hasData(ProductInterface::CUSTOM_ATTRIBUTES)) {
156 3
            return $product->getData('custom_layout_update_file');
157
        }
158
        if ($attr = $product->getCustomAttribute('custom_layout_update_file')) {
159
            return $attr->getValue();
160
        }
161
162
        return null;
163
    }
164
}
165