Files

111 lines
3.3 KiB
PHP

<?php
/**
* @package Plumrocket_CookieConsentPro
* @copyright Copyright (c) 2025 Plumrocket Inc.
* @license https://plumrocket.com/license End-user License Agreement
*/
declare(strict_types=1);
namespace Plumrocket\CookieConsentPro\Setup\Patch\Data;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Plumrocket\CookieConsentPro\Model\Integration\MicrosoftClarity as MicrosoftClarityConfig;
/**
* @since 1.1.0
*/
class MigrateMicrosoftClarityConfigToV2 implements DataPatchInterface
{
private const V2_CONSENT_TYPES = [
'item1' => [
'consent_type' => 'ad_Storage',
'status' => 'denied',
'cookie_category' => null,
],
'item2' => [
'consent_type' => 'analytics_Storage',
'status' => 'denied',
'cookie_category' => null,
],
];
/**
* @var \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory
*/
private $configDataCollectionFactory;
/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
private $configWriter;
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $jsonSerializer;
/**
* @param \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configDataCollectionFactory
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
* @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer
*/
public function __construct(
CollectionFactory $configDataCollectionFactory,
WriterInterface $configWriter,
Json $jsonSerializer
) {
$this->configDataCollectionFactory = $configDataCollectionFactory;
$this->configWriter = $configWriter;
$this->jsonSerializer = $jsonSerializer;
}
/**
* @inheritDoc
*/
public function apply()
{
$legacyCategoryConfigs = $this->configDataCollectionFactory->create();
$legacyCategoryConfigs->addFieldToFilter('path', 'pr_cookie/integrations/microsoft_clarity/category');
/** @var \Magento\Framework\App\Config\Value $legacyCategoryConfig */
foreach ($legacyCategoryConfigs as $legacyCategoryConfig) {
$categoryKey = (string) $legacyCategoryConfig->getValue();
if (! $categoryKey) {
continue;
}
$configDataItemValue = self::V2_CONSENT_TYPES;
foreach ($configDataItemValue as $key => $consentType) {
$configDataItemValue[$key]['cookie_category'] = $categoryKey;
}
$this->configWriter->save(
MicrosoftClarityConfig::XML_PATH_CONSENT_TYPES,
$this->jsonSerializer->serialize($configDataItemValue),
$legacyCategoryConfig->getScope(),
(int) $legacyCategoryConfig->getScopeId()
);
}
}
/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
}