1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File: Index.php |
4
|
|
|
* |
5
|
|
|
* @author Maciej Sławik <[email protected]> |
6
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace LizardMedia\GoogleAnalyticsVerifier\Controller\File; |
11
|
|
|
|
12
|
|
|
use LizardMedia\GoogleAnalyticsVerifier\Api\ConfigProviderInterface; |
13
|
|
|
use LizardMedia\GoogleAnalyticsVerifier\Exception\VerificationFileNotFoundException; |
14
|
|
|
use Magento\Framework\App\Action\Action; |
15
|
|
|
use Magento\Framework\App\Action\Context; |
16
|
|
|
use Magento\Framework\Controller\Result\Raw; |
17
|
|
|
use Magento\Framework\Controller\Result\RawFactory; |
18
|
|
|
use Magento\Framework\Controller\Result\Redirect; |
19
|
|
|
use Magento\Framework\Controller\ResultFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Index |
23
|
|
|
* @package LizardMedia\GoogleAnalyticsVerifier\Controller\File |
24
|
|
|
*/ |
25
|
|
|
class Index extends Action |
26
|
|
|
{ |
27
|
|
|
const URL_PATH = 'lm_google_analytics_verifier/file/index/'; |
28
|
|
|
const FILENAME_PARAM = 'request_path'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RawFactory |
32
|
|
|
*/ |
33
|
|
|
protected $rawResultFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConfigProviderInterface |
37
|
|
|
*/ |
38
|
|
|
private $configProvider; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Index constructor. |
42
|
|
|
* @param Context $context |
43
|
|
|
* @param RawFactory $rawResultFactory |
44
|
|
|
* @param ConfigProviderInterface $configProvider |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
Context $context, |
48
|
|
|
RawFactory $rawResultFactory, |
49
|
|
|
ConfigProviderInterface $configProvider |
50
|
|
|
) { |
51
|
|
|
parent::__construct($context); |
52
|
|
|
$this->rawResultFactory = $rawResultFactory; |
53
|
|
|
$this->configProvider = $configProvider; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Raw|Redirect |
58
|
|
|
*/ |
59
|
|
|
public function execute() |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$verificationFileContent = $this->getVerificationFileContent(); |
63
|
|
|
/** @var Raw $result */ |
64
|
|
|
$result = $this->rawResultFactory->create(); |
65
|
|
|
$result->setContents($verificationFileContent); |
66
|
|
|
return $result; |
67
|
|
|
} catch (VerificationFileNotFoundException $e) { |
68
|
|
|
$this->messageManager->addErrorMessage($e->getMessage()); |
69
|
|
|
/** @var Redirect $result */ |
70
|
|
|
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); |
71
|
|
|
$result->setUrl('/'); |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
* @throws VerificationFileNotFoundException |
79
|
|
|
*/ |
80
|
|
|
private function getVerificationFileContent(): string |
81
|
|
|
{ |
82
|
|
|
$requestedFile = urldecode($this->getRequest()->getParam('request_path')); |
83
|
|
|
$rewrites = $this->configProvider->getRewritesDataArray(); |
84
|
|
|
|
85
|
|
|
foreach ($rewrites as $rewrite) { |
86
|
|
|
if ($requestedFile === $rewrite->getFileName()) { |
87
|
|
|
return $rewrite->getFileContent(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new VerificationFileNotFoundException(__('Requested verification file was not found')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|