Passed
Push — master ( 00c123...9095f7 )
by Mage
12:06 queued 09:24
created

Ajax::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * MagePrince
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the mageprince.com license that is
8
 * available through the world-wide-web at this URL:
9
 * https://mageprince.com/end-user-license-agreement
10
 *
11
 * DISCLAIMER
12
 *
13
 * Do not edit or add to this file if you wish to upgrade this extension to newer
14
 * version in the future.
15
 *
16
 * @category    MagePrince
17
 * @package     Mageprince_Faq
18
 * @copyright   Copyright (c) MagePrince (https://mageprince.com/)
19
 * @license     https://mageprince.com/end-user-license-agreement
20
 */
21
22
namespace Mageprince\Faq\Controller\Index;
23
24
use Magento\Framework\App\Action;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action 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...
25
use Magento\Framework\App\Cache\Type\Block as TypeBlock;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Cache\Type\Block 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...
26
use Magento\Framework\App\CacheInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CacheInterface 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...
27
use Magento\Framework\Controller\Result\JsonFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\JsonFactory 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...
28
use Magento\Framework\Controller\ResultInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\ResultInterface 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...
29
use Magento\Framework\View\Result\PageFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\View\Result\PageFactory 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...
30
use Mageprince\Faq\Block\Index\Index as FaqBlock;
31
use Mageprince\Faq\Helper\Data;
32
use Mageprince\Faq\Model\Config\DefaultConfig;
33
34
class Ajax extends Action\Action
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Action 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...
35
{
36
    /**
37
     * Cache key
38
     */
39
    private const FAQ_GROUP_CACHE_KEY = 'mageprince_faq_group_';
40
41
    /**
42
     * @var PageFactory
43
     */
44
    protected $resultPageFactory;
45
46
    /**
47
     * @var JsonFactory
48
     */
49
    protected $resultJsonFactory;
50
51
    /**
52
     * @var Data
53
     */
54
    protected $helper;
55
56
    /**
57
     * @var CacheInterface
58
     */
59
    protected $cache;
60
61
    /**
62
     * Ajax constructor.
63
     * @param Action\Context $context
64
     * @param Data $helper
65
     * @param PageFactory $resultPageFactory
66
     * @param JsonFactory $resultJsonFactory
67
     * @param CacheInterface $cache
68
     */
69
    public function __construct(
70
        Action\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context 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...
71
        Data $helper,
72
        PageFactory $resultPageFactory,
73
        JsonFactory $resultJsonFactory,
74
        CacheInterface $cache
75
    ) {
76
        $this->helper = $helper;
77
        $this->resultPageFactory = $resultPageFactory;
78
        $this->resultJsonFactory = $resultJsonFactory;
79
        parent::__construct($context);
80
        $this->cache = $cache;
81
    }
82
83
    /**
84
     * Ajax request
85
     *
86
     * @return ResultInterface|void
87
     */
88
    public function execute()
89
    {
90
        if ($this->getRequest()->isXmlHttpRequest()) {
91
            $groupId = $this->getRequest()->getParam('groupId');
92
            $faqHtml = $this->getFaqHtml($groupId);
93
            $resultJson = $this->resultJsonFactory->create();
94
            $resultJson->setData(['faq' => $faqHtml]);
95
            return $resultJson;
96
        }
97
    }
98
99
    /**
100
     * Get faq html from cache
101
     *
102
     * @param int $groupId
103
     * @return string
104
     */
105
    protected function getFaqHtml($groupId)
106
    {
107
        $faqHtml = $this->cache->load($this->getCacheKey($groupId));
108
        if (false === $faqHtml) {
109
            $resultPage = $this->resultPageFactory->create();
110
            $faqHtml = $resultPage->getLayout()
111
                ->createBlock(FaqBlock::class)
112
                ->setTemplate(DefaultConfig::FAQ_AJAX_TEMPLATE_FILE)
113
                ->setGroupId($groupId)
114
                ->toHtml();
115
116
            $this->cache->save(
117
                $faqHtml,
118
                $this->getCacheKey($groupId),
119
                [
120
                    TypeBlock::TYPE_IDENTIFIER
121
                ]
122
            );
123
        }
124
        return $faqHtml;
125
    }
126
127
    /**
128
     * Retrieve cache key
129
     *
130
     * @param int $groupId
131
     * @return string
132
     */
133
    protected function getCacheKey($groupId)
134
    {
135
        return self::FAQ_GROUP_CACHE_KEY . $groupId;
136
    }
137
}
138