ListingPageProcessor   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 129
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 12 1
A applyCustomDesign() 0 7 2
A applyCustomPageLayout() 0 7 2
A getCategoryType() 0 10 4
A addPageLayoutHandles() 0 9 2
A applyLayoutUpdates() 0 9 4
A configurePage() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: ListingPageProcessor.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\AllProductsListing\Model;
13
14
use LizardMedia\AllProductsListing\Api\ListingPageProcessorInterface;
15
use Magento\Catalog\Api\Data\CategoryInterface;
16
use Magento\Catalog\Model\Category;
17
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
18
use Magento\Catalog\Model\Design;
19
use Magento\Framework\DataObject;
20
use Magento\Framework\View\Page\Config as PageConfig;
21
use Magento\Framework\View\Result\Page;
22
23
/**
24
 * Class ListingPageProcessor
25
 * @package LizardMedia\AllProductsListing\Model
26
 */
27
class ListingPageProcessor implements ListingPageProcessorInterface
28
{
29
    /**
30
     * @var Design
31
     */
32
    private $catalogDesign;
33
34
    /**
35
     * @var CategoryUrlPathGenerator
36
     */
37
    private $categoryUrlPathGenerator;
38
39
    /**
40
     * @var PageConfig
41
     */
42
    private $pageConfig;
43
44
    /**
45
     * ListingPageProcessor constructor.
46
     * @param Design $design
47
     * @param CategoryUrlPathGenerator $categoryUrlPathGenerator
48
     */
49
    public function __construct(Design $design, CategoryUrlPathGenerator $categoryUrlPathGenerator)
50
    {
51
        $this->catalogDesign = $design;
52
        $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
53
    }
54
55
    /**
56
     * @param CategoryInterface | Category $category
57
     * @param Page $page
58
     * @return void
59
     */
60
    public function process(CategoryInterface $category, Page $page): void
61
    {
62
        $settings = $this->catalogDesign->getDesignSettings($category);
63
        $this->pageConfig = $page->getConfig();
64
        $this->applyCustomDesign($settings);
65
66
        $this->applyCustomPageLayout($page, $settings);
67
        $type = $this->getCategoryType($category);
68
        $this->addPageLayoutHandles($category, $page, $type);
69
        $this->applyLayoutUpdates($page, $settings);
70
        $this->configurePage($page, $category);
71
    }
72
73
    /**
74
     * @param DataObject $settings
75
     * @return void
76
     */
77
    private function applyCustomDesign(DataObject $settings) : void
78
    {
79
        $customDesign = $settings->getCustomDesign();
80
        if ($customDesign) {
81
            $this->catalogDesign->applyCustomDesign($customDesign);
82
        }
83
    }
84
85
    /**
86
     * @param Page $page
87
     * @param DataObject $settings
88
     */
89
    private function applyCustomPageLayout(Page $page, DataObject $settings) : void
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed.

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

Loading history...
90
    {
91
        $pageLayout = $settings->getPageLayout();
92
        if ($pageLayout) {
93
            $this->pageConfig->setPageLayout($pageLayout);
94
        }
95
    }
96
97
    /**
98
     * @param CategoryInterface | Category $category
99
     * @return string
100
     */
101
    private function getCategoryType(CategoryInterface $category) : string
102
    {
103
        if ($category->getData('is_anchor')) {
104
            $type = $category->hasChildren() ? 'layered' : 'layered_without_children';
105
        } else {
106
            $type = $category->hasChildren() ? 'default' : 'default_without_children';
107
        }
108
109
        return $type;
110
    }
111
112
    /**
113
     * @param CategoryInterface | Category  $category
114
     * @param Page $page
115
     * @param string $type
116
     * @return void
117
     */
118
    private function addPageLayoutHandles(CategoryInterface $category, Page $page, string $type) : void
119
    {
120
        if (!$category->hasChildren()) {
121
            $parentType = strtok($type, '_');
122
            $page->addPageLayoutHandles(['type' => $parentType]);
123
        }
124
125
        $page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
126
    }
127
128
    /**
129
     * @param Page $page
130
     * @param DataObject $settings
131
     * @return void
132
     */
133
    private function applyLayoutUpdates(Page $page, DataObject $settings) : void
134
    {
135
        $layoutUpdates = $settings->getLayoutUpdates();
136
        if ($layoutUpdates && is_array($layoutUpdates)) {
137
            foreach ($layoutUpdates as $layoutUpdate) {
138
                $page->addUpdate($layoutUpdate);
139
            }
140
        }
141
    }
142
143
    /**
144
     * @param Page $page
145
     * @param CategoryInterface $category
146
     * @return void
147
     */
148
    private function configurePage(Page $page, CategoryInterface $category) : void
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed.

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

Loading history...
149
    {
150
        $this->pageConfig->addBodyClass('page-products')
151
            ->addBodyClass(sprintf('categorypath-%s', $this->categoryUrlPathGenerator->getUrlPath($category)))
152
            ->addBodyClass(sprintf('category-%s', $category->getUrlKey()));
153
        $this->pageConfig->getTitle()->set(__('All products'));
154
    }
155
}
156