ProductLayoutPlugin   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 89.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 146
ccs 35
cts 39
cp 0.8974
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractAttributeValue() 0 10 4
A afterExtractCustomSettings() 0 13 3
A getLayoutProcessor() 0 15 2
A __construct() 0 8 1
A afterFetchAvailableFiles() 0 24 2
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
    /**
21
     * @var FlyweightFactory
22
     */
23
    private $themeFactory;
24
25
    /**
26
     * @var DesignInterface
27
     */
28
    private $design;
29
30
    /**
31
     * @var LayoutProcessorFactory
32
     */
33
    private $layoutProcessorFactory;
34
35
    /**
36
     * @var LayoutProcessor|null
37
     */
38
    private $layoutProcessor;
39
40
    /**
41
     * @param FlyweightFactory $themeFactory
42
     * @param DesignInterface $design
43
     * @param LayoutProcessorFactory $layoutProcessorFactory
44
     */
45 2
    public function __construct(
46
        FlyweightFactory $themeFactory,
47
        DesignInterface $design,
48
        LayoutProcessorFactory $layoutProcessorFactory)
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
49
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
50 2
        $this->themeFactory = $themeFactory;
51 2
        $this->design = $design;
52 2
        $this->layoutProcessorFactory = $layoutProcessorFactory;
53 2
    }
54
55
    /**
56
     * Get the processor instance.
57
     *
58
     * @return LayoutProcessor
59
     *
60
     * Unchanged private method copied over from @var LayoutUpdateManager
61
     */
62 2
    private function getLayoutProcessor(): LayoutProcessor
63
    {
64 2
        if (!$this->layoutProcessor) {
65 2
            $this->layoutProcessor = $this->layoutProcessorFactory->create(
66
                [
67 2
                    'theme' => $this->themeFactory->create(
68 2
                        $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
69
                    ),
70
                ]
71
            );
72 2
            $this->themeFactory = null;
73 2
            $this->design = null;
74
        }
75
76 2
        return $this->layoutProcessor;
77
    }
78
79
    /**
80
     * Fetch list of available global files/handles for the product.
81
     *
82
     * @param LayoutUpdateManager $subject
83
     * @param array $result
84
     * @param ProductInterface $product
85
     * @return array
86
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
87
     */
88 2
    public function afterFetchAvailableFiles(
89
        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

89
        /** @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...
90
        array $result,
91
        ProductInterface $product): array
0 ignored issues
show
Unused Code introduced by
The parameter $product 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

91
        /** @scrutinizer ignore-unused */ ProductInterface $product): array

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...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
92
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
93 2
        $handles = $this->getLayoutProcessor()->getAvailableHandles();
94
95 2
        return array_merge(
96 2
            $result,
97 2
            array_filter(
98 2
                array_map(
99
                    function (string $handle): ?string {
100 2
                        preg_match(
101 2
                            '/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
102
                            $handle,
103
                            $selectable
104
                        );
105 2
                        if (!empty($selectable[1])) {
106
                            return $selectable[1];
107
                        }
108
109 2
                        return null;
110 2
                    },
111
                    $handles
112
                )
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 2
    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): void
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
134
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
135 2
        if ($product->getSku() && $value = $this->extractAttributeValue($product)) {
136 2
            $handles = $intoSettings->getPageLayoutHandles() ?? [];
137 2
            $handles = array_merge_recursive(
138 2
                $handles,
139 2
                ['selectable_0' => $value]
140
            );
141 2
            $intoSettings->setPageLayoutHandles($handles);
142
        }
143 2
    }
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 2
    private function extractAttributeValue(ProductInterface $product)
154
    {
155 2
        if ($product instanceof Product && !$product->hasData(ProductInterface::CUSTOM_ATTRIBUTES)) {
156 2
            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