Completed
Push — master ( d80aef...0536a3 )
by Willem
13s queued 12s
created

CategoryLayoutPlugin::getLayoutProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.3149

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 8
cts 14
cp 0.5714
rs 10
cc 2
nc 2
nop 0
crap 2.3149
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 1
    public function __construct(
45
        FlyweightFactory $themeFactory,
46
        DesignInterface $design,
47
        LayoutProcessorFactory $layoutProcessorFactory
48
    )
49
    {
50 1
        $this->themeFactory = $themeFactory;
51 1
        $this->design = $design;
52 1
        $this->layoutProcessorFactory = $layoutProcessorFactory;
53 1
    }
54
55
    /**
56
     * Get the processor instance.
57
     *
58
     * @return LayoutProcessor
59
     *
60
     * Unchanged private method copied over from @var LayoutUpdateManager
61
     */
62 1
    private function getLayoutProcessor(): LayoutProcessor
63
    {
64 1
        if (!$this->layoutProcessor) {
65 1
            $this->layoutProcessor = $this->layoutProcessorFactory->create(
66
                [
67 1
                    'theme' => $this->themeFactory->create(
68 1
                        $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
69
                    )
70
                ]
71
            );
72 1
            $this->themeFactory = null;
73 1
            $this->design = null;
74
        }
75
76 1
        return $this->layoutProcessor;
77
    }
78
79
    /**
80
     * Fetch list of available global files/handles for the category.
81
     *
82
     * @param LayoutUpdateManager $subject
83
     * @param array $result
84
     * @param CategoryInterface $category
85
     * @return array
86
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
87
     */
88 1
    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
        CategoryInterface $category
92
    ): array
93
    {
94 1
        if (!$category->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $category->getId() of type integer|null is loosely compared to false; 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...
95
            return $result;
96
        }
97
98 1
        $handles = $this->getLayoutProcessor()->getAvailableHandles();
99
100 1
        return array_merge($result, array_filter(
101 1
            array_map(
102 1
                function(string $handle) use ($category) : ?string {
0 ignored issues
show
Unused Code introduced by
The import $category is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
103 1
                    preg_match(
104 1
                        '/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
105 1
                        $handle,
106 1
                        $selectable
107
                    );
108 1
                    if (!empty($selectable[1])) {
109
                        return $selectable[1];
110
                    }
111
112 1
                    return null;
113 1
                },
114 1
                $handles
115
            )
116
        ));
117
    }
118
119
    /**
120
     * Extract selected global custom layout settings.
121
     *
122
     * If no update is selected none will apply.
123
     *
124
     * @param LayoutUpdateManager $subject
125
     * @param $result
126
     * @param CategoryInterface $category
127
     * @param DataObject $intoSettings
128
     * @return void
129
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
130
     */
131 1
    public function afterExtractCustomSettings(
132
        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

132
        /** @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...
133
        $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

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