121 lines
3.5 KiB
PHP
121 lines
3.5 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\ViewModel\Integration;
|
|
|
|
use Magento\Framework\Api\SearchCriteriaBuilder;
|
|
use Magento\Framework\Serialize\SerializerInterface;
|
|
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
|
use Plumrocket\CookieConsent\Model\CategoryRepository;
|
|
use Plumrocket\CookieConsentPro\Model\Integration\GlobalPrivacyControl as GlobalPrivacyControlIntegrationConfig;
|
|
|
|
/**
|
|
* @since 1.0.1
|
|
*/
|
|
class GlobalPrivacyControl implements ArgumentInterface
|
|
{
|
|
/**
|
|
* @var \Magento\Framework\Api\SearchCriteriaBuilder
|
|
*/
|
|
private $searchCriteriaBuilder;
|
|
|
|
/**
|
|
* @var \Magento\Framework\Serialize\SerializerInterface
|
|
*/
|
|
private $serializer;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\Model\CategoryRepository
|
|
*/
|
|
private $cookieCategoryRepository;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsentPro\Model\Integration\GlobalPrivacyControl
|
|
*/
|
|
private $integrationConfig;
|
|
|
|
/**
|
|
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
|
|
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
|
* @param \Plumrocket\CookieConsent\Model\CategoryRepository $cookieCategoryRepository
|
|
* @param GlobalPrivacyControlIntegrationConfig $integrationConfig
|
|
*/
|
|
public function __construct(
|
|
SearchCriteriaBuilder $searchCriteriaBuilder,
|
|
SerializerInterface $serializer,
|
|
CategoryRepository $cookieCategoryRepository,
|
|
GlobalPrivacyControlIntegrationConfig $integrationConfig
|
|
) {
|
|
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
|
|
$this->serializer = $serializer;
|
|
$this->cookieCategoryRepository = $cookieCategoryRepository;
|
|
$this->integrationConfig = $integrationConfig;
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function shouldDeclineAllCategories(): bool
|
|
{
|
|
return $this->integrationConfig->shouldDeclineAllCategories();
|
|
}
|
|
|
|
/**
|
|
* Get prepared list of allowed cookie categories
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getAllowedCategories(): array
|
|
{
|
|
$allowedCategories = [];
|
|
$categoriesToDecline = $this->integrationConfig->getCategoriesToDecline();
|
|
$categories = $this->cookieCategoryRepository
|
|
->getList($this->searchCriteriaBuilder->create())
|
|
->getItems();
|
|
|
|
foreach ($categories as $category) {
|
|
if (! $category->isEnabled()) {
|
|
continue;
|
|
}
|
|
|
|
if (! $category->isEssential() && in_array($category->getKey(), $categoriesToDecline, true)) {
|
|
continue;
|
|
}
|
|
|
|
$allowedCategories[] = $category->getKey();
|
|
}
|
|
|
|
return $allowedCategories;
|
|
}
|
|
|
|
/**
|
|
* Get prepared list of allowed cookie categories in JSON format
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getAllowedCategoriesJson(): string
|
|
{
|
|
return $this->serializer->serialize(
|
|
$this->getAllowedCategories()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get prepared list cookie categories to decline in JSON format
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getCategoriesToDeclineJson()
|
|
{
|
|
return $this->serializer->serialize(
|
|
$this->integrationConfig->getCategoriesToDecline()
|
|
);
|
|
}
|
|
}
|