119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_GlobalPrivacyControl
|
|
* @copyright Copyright (c) 2025 Plumrocket Inc.
|
|
* @license https://plumrocket.com/license End-user License Agreement
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plumrocket\GlobalPrivacyControl\Service;
|
|
|
|
use Magento\Framework\App\DeploymentConfig;
|
|
use Magento\Framework\App\Filesystem\DirectoryList as DirectoryListApi;
|
|
use Magento\Framework\Config\ConfigOptionsListConstants;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Filesystem\DirectoryList;
|
|
use Magento\Framework\Filesystem\Driver\File as FileDriver;
|
|
use Plumrocket\GlobalPrivacyControl\Helper\Config;
|
|
|
|
class WellKnownFileManager
|
|
{
|
|
private const FILE_NAME = 'gpc.json';
|
|
|
|
/**
|
|
* @var \Magento\Framework\App\DeploymentConfig
|
|
*/
|
|
private $deploymentConfig;
|
|
|
|
/**
|
|
* @var \Magento\Framework\Filesystem\DirectoryList
|
|
*/
|
|
private $directoryList;
|
|
|
|
/**
|
|
* @var \Magento\Framework\Filesystem\Driver\File
|
|
*/
|
|
private $fileDriver;
|
|
|
|
/**
|
|
* @var \Plumrocket\GlobalPrivacyControl\Helper\Config
|
|
*/
|
|
private $config;
|
|
|
|
/**
|
|
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
|
|
* @param \Magento\Framework\Filesystem\DirectoryList $directoryList
|
|
* @param \Magento\Framework\Filesystem\Driver\File $fileDriver
|
|
* @param \Plumrocket\GlobalPrivacyControl\Helper\Config $config
|
|
*/
|
|
public function __construct(
|
|
DeploymentConfig $deploymentConfig,
|
|
DirectoryList $directoryList,
|
|
FileDriver $fileDriver,
|
|
Config $config
|
|
) {
|
|
$this->deploymentConfig = $deploymentConfig;
|
|
$this->directoryList = $directoryList;
|
|
$this->fileDriver = $fileDriver;
|
|
$this->config = $config;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws \Magento\Framework\Exception\FileSystemException
|
|
* @throws \Magento\Framework\Exception\RuntimeException
|
|
*/
|
|
public function create(): void
|
|
{
|
|
$filePath = $this->getFilePath();
|
|
$directoryPath = dirname($filePath);
|
|
|
|
if (! $this->fileDriver->isExists($directoryPath)) {
|
|
$this->fileDriver->createDirectory($directoryPath);
|
|
}
|
|
|
|
$jsonData = json_encode(
|
|
[
|
|
'gpc' => $this->config->isModuleEnabled(),
|
|
'version' => '1.0',
|
|
'created' => $this->config->getWellKnownLastUpdateDate()
|
|
],
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
|
|
);
|
|
|
|
$this->fileDriver->filePutContents($filePath, $jsonData);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @throws \Magento\Framework\Exception\FileSystemException
|
|
* @throws \Magento\Framework\Exception\RuntimeException
|
|
*/
|
|
public function getContent(): string
|
|
{
|
|
$filePath = $this->getFilePath();
|
|
if (! $this->fileDriver->isExists($filePath)) {
|
|
throw new LocalizedException(
|
|
__("The .well-known file was not found.")
|
|
);
|
|
}
|
|
|
|
return $this->fileDriver->fileGetContents($filePath);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @throws \Magento\Framework\Exception\FileSystemException
|
|
* @throws \Magento\Framework\Exception\RuntimeException
|
|
*/
|
|
private function getFilePath(): string
|
|
{
|
|
$rootPath = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB)
|
|
? $this->directoryList->getPath(DirectoryListApi::PUB)
|
|
: $this->directoryList->getRoot();
|
|
|
|
return $rootPath . DIRECTORY_SEPARATOR . '.well-known' . DIRECTORY_SEPARATOR . self::FILE_NAME;
|
|
}
|
|
}
|