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

CategoryLayoutPlugin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 92.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 145
ccs 37
cts 40
cp 0.925
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getLayoutProcessor() 0 15 2
A afterExtractCustomSettings() 0 13 3
A extractAttributeValue() 0 10 4
A afterFetchAvailableFiles() 0 23 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\Model\Category;
8
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
9
use Magento\Framework\App\Area;
10
use Magento\Framework\DataObject;
11
use Magento\Framework\View\Design\Theme\FlyweightFactory;
12
use Magento\Framework\View\DesignInterface;
13
use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor;
14
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...
15
16
class CategoryLayoutPlugin
17
{
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 6
    public function __construct(
45
        FlyweightFactory $themeFactory,
46
        DesignInterface $design,
47
        LayoutProcessorFactory $layoutProcessorFactory)
48
    {
49 6
        $this->themeFactory = $themeFactory;
50 6
        $this->design = $design;
51 6
        $this->layoutProcessorFactory = $layoutProcessorFactory;
52 6
    }
53
54
    /**
55
     * Get the processor instance.
56
     *
57
     * @return LayoutProcessor
58
     *
59
     * Unchanged private method copied over from @var LayoutUpdateManager
60
     */
61 6
    private function getLayoutProcessor(): LayoutProcessor
62
    {
63 6
        if (!$this->layoutProcessor) {
64 6
            $this->layoutProcessor = $this->layoutProcessorFactory->create(
65
                [
66 6
                    'theme' => $this->themeFactory->create(
67 6
                        $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
68
                    ),
69
                ]
70
            );
71 6
            $this->themeFactory = null;
72 6
            $this->design = null;
73
        }
74
75 6
        return $this->layoutProcessor;
76
    }
77
78
    /**
79
     * Fetch list of available global files/handles for the category.
80
     *
81
     * @param LayoutUpdateManager $subject
82
     * @param array $result
83
     * @param CategoryInterface $category
84
     * @return array
85
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
86
     */
87 6
    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
        CategoryInterface $category): array
0 ignored issues
show
Unused Code introduced by
The parameter $category 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

90
        /** @scrutinizer ignore-unused */ CategoryInterface $category): 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...
91
    {
92 6
        $handles = $this->getLayoutProcessor()->getAvailableHandles();
93 6
        return array_merge(
94 6
            $result,
95 6
            array_filter(
96 6
                array_map(
97 2
                    function (string $handle): ?string {
98 6
                        preg_match(
99 6
                            '/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
100
                            $handle,
101
                            $selectable
102
                        );
103 6
                        if (!empty($selectable[1])) {
104 6
                            return $selectable[1];
105
                        }
106
107 6
                        return null;
108 6
                    },
109
                    $handles
110
                )
111
            )
112
        );
113
    }
114
115
    /**
116
     * Extract selected global custom layout settings.
117
     *
118
     * If no update is selected none will apply.
119
     *
120
     * @param LayoutUpdateManager $subject
121
     * @param $result
122
     * @param CategoryInterface $category
123
     * @param DataObject $intoSettings
124
     * @return void
125
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
126
     */
127 6
    public function afterExtractCustomSettings(
128
        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

128
        /** @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...
129
        $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

129
        /** @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...
130
        CategoryInterface $category,
131
        DataObject $intoSettings): void
132
    {
133 6
        if ($category->getId() && $value = $this->extractAttributeValue($category)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $category->getId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
134 6
            $handles = $intoSettings->getPageLayoutHandles() ?? [];
135 6
            $handles = array_merge_recursive(
136 6
                $handles,
137 6
                ['selectable_0' => $value]
138
            );
139 6
            $intoSettings->setPageLayoutHandles($handles);
140
        }
141 6
    }
142
143
    /**
144
     * Extract custom layout attribute value.
145
     *
146
     * @param CategoryInterface $category
147
     * @return mixed
148
     *
149
     * Unchanged private method copied over from @var LayoutUpdateManager
150
     */
151 6
    private function extractAttributeValue(CategoryInterface $category)
152
    {
153 6
        if ($category instanceof Category && !$category->hasData(CategoryInterface::CUSTOM_ATTRIBUTES)) {
154 6
            return $category->getData('custom_layout_update_file');
155
        }
156
        if ($attr = $category->getCustomAttribute('custom_layout_update_file')) {
157
            return $attr->getValue();
158
        }
159
160
        return null;
161
    }
162
}
163