mageprince /
magento2-FAQ
| 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\Observer; |
||
| 23 | |||
| 24 | use Magento\Framework\App\Config\Storage\WriterInterface; |
||
|
0 ignored issues
–
show
|
|||
| 25 | use Magento\Framework\App\RequestInterface; |
||
|
0 ignored issues
–
show
The type
Magento\Framework\App\RequestInterface 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. 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\Event\Observer as EventObserver; |
||
|
0 ignored issues
–
show
The type
Magento\Framework\Event\Observer 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. 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\Event\ObserverInterface; |
||
|
0 ignored issues
–
show
The type
Magento\Framework\Event\ObserverInterface 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. 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\Store\Model\StoreManagerInterface; |
||
|
0 ignored issues
–
show
The type
Magento\Store\Model\StoreManagerInterface 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. 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\UrlRewrite\Model\UrlRewriteFactory; |
||
|
0 ignored issues
–
show
The type
Magento\UrlRewrite\Model\UrlRewriteFactory 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 30 | |||
| 31 | class ConfigChange implements ObserverInterface |
||
| 32 | { |
||
| 33 | private const REQUEST_PATH = 'faq'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var RequestInterface |
||
| 37 | */ |
||
| 38 | protected $request; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var WriterInterface |
||
| 42 | */ |
||
| 43 | protected $configWriter; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var UrlRewriteFactory |
||
| 47 | */ |
||
| 48 | protected $urlRewriteFactory; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var StoreManagerInterface |
||
| 52 | */ |
||
| 53 | protected $storeManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Psr\Log\LoggerInterface |
||
|
0 ignored issues
–
show
The type
Psr\Log\LoggerInterface 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 57 | */ |
||
| 58 | protected $logger; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * ConfigChange constructor. |
||
| 62 | * |
||
| 63 | * @param RequestInterface $request |
||
| 64 | * @param WriterInterface $configWriter |
||
| 65 | * @param UrlRewriteFactory $urlRewriteFactory |
||
| 66 | * @param StoreManagerInterface $storeManager |
||
| 67 | * @param \Psr\Log\LoggerInterface $logger |
||
| 68 | */ |
||
| 69 | public function __construct( |
||
| 70 | RequestInterface $request, |
||
| 71 | WriterInterface $configWriter, |
||
| 72 | UrlRewriteFactory $urlRewriteFactory, |
||
| 73 | StoreManagerInterface $storeManager, |
||
| 74 | \Psr\Log\LoggerInterface $logger |
||
| 75 | ) { |
||
| 76 | $this->request = $request; |
||
| 77 | $this->configWriter = $configWriter; |
||
| 78 | $this->urlRewriteFactory = $urlRewriteFactory; |
||
| 79 | $this->storeManager = $storeManager; |
||
| 80 | $this->logger = $logger; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Save FAQ url |
||
| 85 | * |
||
| 86 | * @param EventObserver $observer |
||
| 87 | * @return $this|void |
||
| 88 | */ |
||
| 89 | public function execute(EventObserver $observer) |
||
| 90 | { |
||
| 91 | try { |
||
| 92 | $faqParams = $this->request->getParam('groups'); |
||
| 93 | if (!empty($faqParams['seo']['fields']['faq_url'])) { |
||
| 94 | $faqUrlVal = $faqParams['seo']['fields']['faq_url']; |
||
| 95 | $urlKey = str_replace(' ', '-', $faqUrlVal['value']); |
||
| 96 | $filterUrlKey = preg_replace('/[^A-Za-z0-9\-]/', '', $urlKey); |
||
| 97 | $this->configWriter->save('faqtab/seo/faq_url', $filterUrlKey); |
||
| 98 | $stores = $this->storeManager->getStores(); |
||
| 99 | foreach ($stores as $store) { |
||
| 100 | $urlRewriteModel = $this->urlRewriteFactory->create(); |
||
| 101 | $rewriteCollection = $urlRewriteModel->getCollection() |
||
| 102 | ->addFieldToFilter('request_path', self::REQUEST_PATH) |
||
| 103 | ->addFieldToFilter('store_id', $store->getId()) |
||
| 104 | ->getFirstItem(); |
||
| 105 | $urlRewriteModel->load($rewriteCollection->getId()); |
||
| 106 | if ($filterUrlKey == self::REQUEST_PATH) { |
||
| 107 | if ($urlRewriteModel->getId()) { |
||
| 108 | $urlRewriteModel->delete(); |
||
| 109 | } |
||
| 110 | } else { |
||
| 111 | $urlRewriteModel->setStoreId($store->getId()); |
||
| 112 | $urlRewriteModel->setTargetPath($filterUrlKey); |
||
| 113 | $urlRewriteModel->setRequestPath(self::REQUEST_PATH); |
||
| 114 | $urlRewriteModel->setredirectType(301); |
||
| 115 | $urlRewriteModel->save(); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } catch (\Exception $e) { |
||
| 120 | $this->logger->error('FAQ url save error:' . $e->getMessage()); |
||
| 121 | } |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | } |
||
| 125 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths