Index::execute()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.7666
cc 2
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: Index.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\AllProductsListing\Controller\Index;
13
14
use LizardMedia\AllProductsListing\Api\ListingPageProcessorInterface;
15
use LizardMedia\AllProductsListing\Api\RootCategoryProviderInterface;
16
use Magento\Catalog\Api\Data\CategoryInterface;
17
use Magento\Catalog\Model\Layer\Resolver;
18
use Magento\Catalog\Model\Session;
19
use Magento\Framework\App\Action\Action;
20
use Magento\Framework\App\Action\Context;
21
use Magento\Framework\App\ResponseInterface;
22
use Magento\Framework\Controller\ResultInterface;
23
use Magento\Framework\Exception\NoSuchEntityException;
24
use Magento\Framework\Registry;
25
use Magento\Framework\Controller\Result\ForwardFactory;
26
use Magento\Framework\View\Result\Page;
27
use Magento\Framework\View\Result\PageFactory;
28
29
/**
30
 * Class Index
31
 * @package LizardMedia\AllProductsListing\Controller\Index
32
 */
33
class Index extends Action
34
{
35
    /**
36
     * @var ListingPageProcessorInterface
37
     */
38
    private $listingPageProcessor;
39
40
    /**
41
    * @var RootCategoryProviderInterface
42
     */
43
    private $rootCategoryProvider;
44
45
    /**
46
     * @var Resolver
47
     */
48
    private $layerResolver;
49
50
    /**
51
     * @var Session
52
     */
53
    private $catalogSession;
54
55
    /**
56
     * @var ForwardFactory
57
     */
58
    private $resultForwardFactory;
59
60
    /**
61
     * @var PageFactory
62
     */
63
    private $resultPageFactory;
64
65
    /**
66
     * @var Registry
67
     */
68
    private $registry;
69
70
    /**
71
     * Index constructor.
72
     * @param ListingPageProcessorInterface $listingPageProcessor
73
     * @param RootCategoryProviderInterface $rootCategoryProvider
74
     * @param Resolver $layerResolver
75
     * @param Session $catalogSession
76
     * @param Context $context
77
     * @param ForwardFactory $resultForwardFactory
78
     * @param Registry $registry
79
     * @param PageFactory $resultPageFactory
80
     */
81
    public function __construct(
82
        ListingPageProcessorInterface $listingPageProcessor,
83
        RootCategoryProviderInterface $rootCategoryProvider,
84
        Resolver $layerResolver,
85
        Session $catalogSession,
86
        Context $context,
87
        ForwardFactory $resultForwardFactory,
88
        Registry $registry,
89
        PageFactory $resultPageFactory
90
    ) {
91
        parent::__construct($context);
92
        $this->listingPageProcessor = $listingPageProcessor;
93
        $this->rootCategoryProvider = $rootCategoryProvider;
94
        $this->layerResolver = $layerResolver;
95
        $this->catalogSession = $catalogSession;
96
        $this->resultForwardFactory = $resultForwardFactory;
97
        $this->resultPageFactory = $resultPageFactory;
98
        $this->registry = $registry;
99
    }
100
101
    /**
102
     * @return CategoryInterface
103
     * @throws NoSuchEntityException
104
     */
105
    private function initRootCategory() : CategoryInterface
106
    {
107
        $category = $this->rootCategoryProvider->getRootCategory();
108
        $this->catalogSession->setLastVisitedCategoryId($category->getId());
109
        $this->registry->register('current_category', $category);
110
        $this->_eventManager->dispatch(
111
            'catalog_controller_category_init_after',
112
            ['category' => $category, 'controller_action' => $this]
113
        );
114
115
        return $category;
116
    }
117
118
    /**
119
     * @return ResponseInterface|ResultInterface|Page
120
     */
121
    public function execute()
122
    {
123
        try {
124
            $category = $this->initRootCategory();
125
            $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
126
        } catch (\Exception $exception) {
127
            return $this->resultForwardFactory->create()->forward('noroute');
128
        }
129
130
        $this->catalogSession->setLastViewedCategoryId($category->getId());
131
        $page = $this->resultPageFactory->create();
132
        $this->listingPageProcessor->process($category, $page);
133
134
        return $page;
135
    }
136
}
137