ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Amasty\AdminActionsLog\Api\Restoring\EntityRestoreHandlerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
abstract class AbstractHandler implements EntityRestoreHandlerInterface
{
public const STORAGE_CODE_PREFIX = 'action_type';
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var ObjectDataStorageInterface
*/
protected $dataStorage;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager
) {
$this->objectManager = $objectManager;
$this->dataStorage = $dataStorage;
$this->storeManager = $storeManager;
}
protected function getModelObject(LogEntryInterface $logEntry, LogDetailInterface $logDetail)
{
$elementId = $logEntry->getElementId();
$modelName = $logDetail->getModel();
return $this->objectManager->create($modelName)->load($elementId);
}
protected function setRestoreActionFlag($object): void
{
$storageKey = spl_object_id($object) . '.' . self::STORAGE_CODE_PREFIX;
$this->dataStorage->set($storageKey, []);
}
/**
* Set current default store
*
* @param string|int|\Magento\Store\Api\Data\StoreInterface $store
* @return void
*/
protected function setCurrentStore($store = Store::DEFAULT_STORE_ID): void
{
$this->storeManager->setCurrentStore($store);
}
}
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Amasty\AdminActionsLog\Model\LogEntry\LogEntry;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
class Category extends AbstractHandler
{
/**
* @var array
*/
private $requiredKeys = [
'entity_id',
'store_id',
'row_id',
'created_in',
'updated_in',
'parent_id'
];
/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager,
CategoryRepositoryInterface $categoryRepository
) {
parent::__construct(
$objectManager,
$dataStorage,
$storeManager
);
$this->categoryRepository = $categoryRepository;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
try {
$storeId = $logEntry->getStoreId() ?? Store::DEFAULT_STORE_ID;
$this->setCurrentStore($storeId);
$categoryId = $logEntry->getData(LogEntry::ELEMENT_ID);
$category = $this->categoryRepository->get($categoryId, $storeId);
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(__('Unable to restore changes. Error: %1', $e->getMessage()));
}
if ($storeId !== Store::DEFAULT_STORE_ID) {
$this->processUseDefault($category);
}
foreach ($logDetails as $detail) {
$oldValue = $detail->getOldValue();
$elementKey = $detail->getName();
$category->setData($elementKey, $oldValue);
}
if (!$category->hasData('store_id')) {
$storeId = $logEntry->getStoreId() ?? Store::DEFAULT_STORE_ID;
$category->setData('store_id', $storeId);
}
$this->setRestoreActionFlag($category);
$this->categoryRepository->save($category);
}
// Process category data for correct checkbox behaviour.
private function processUseDefault(CategoryInterface $category): void
{
try {
$defCategory = $this->categoryRepository->get($category->getId(), Store::DEFAULT_STORE_ID);
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(__('Unable to restore changes. Error: %1', $e->getMessage()));
}
$useDefaultMap = [];
foreach ($defCategory->getData() as $key => $value) {
if ($category->getData($key) === $value) {
if (!in_array($key, $this->requiredKeys)) {
$category->setData($key);
}
$useDefaultMap[$key] = 1;
} else {
$useDefaultMap[$key] = 0;
}
}
$category->setData('use_default', $useDefaultMap);
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Magento\Store\Model\Store;
class Common extends AbstractHandler
{
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
if (empty($logDetails)) {
return;
}
$element = $this->getModelObject($logEntry, current($logDetails));
/** @var LogDetailInterface $logDetail */
foreach ($logDetails as $logDetail) {
$oldValue = $logDetail->getOldValue();
$elementKey = $logDetail->getName();
$element->setData($elementKey, $oldValue);
}
if (!$element->hasData('store_id')) {
$storeId = $logEntry->getStoreId() === null ? Store::DEFAULT_STORE_ID : $logEntry->getStoreId();
$element->setData('store_id', $storeId);
}
$this->setRestoreActionFlag($element);
$element->save();
}
}
@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\LogEntryRepositoryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Amasty\AdminActionsLog\Logging\Util\DetailsBuilder;
use Amasty\AdminActionsLog\Model\LogEntry\AdminLogEntryFactory;
use Amasty\AdminActionsLog\Model\LogEntry\LogEntry;
use Amasty\AdminActionsLog\Model\OptionSource\LogEntryTypes;
use Magento\Config\Model\ResourceModel\Config as ResourceConfig;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
class Config extends AbstractHandler
{
/**
* @var ResourceConfig
*/
private $scopeConfig;
/**
* @var ScopeConfigInterface
*/
private $resourceConfig;
/**
* @var AdminLogEntryFactory
*/
private $logEntryFactory;
/**
* @var DetailsBuilder
*/
private $detailsBuilder;
/**
* @var LogEntryRepositoryInterface
*/
private $logEntryRepository;
public function __construct(
ResourceConfig $resourceConfig,
ScopeConfigInterface $scopeConfig,
AdminLogEntryFactory $logEntryFactory,
DetailsBuilder $detailsBuilder,
LogEntryRepositoryInterface $logEntryRepository,
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager
) {
parent::__construct(
$objectManager,
$dataStorage,
$storeManager
);
$this->resourceConfig = $resourceConfig;
$this->scopeConfig = $scopeConfig;
$this->logEntryFactory = $logEntryFactory;
$this->detailsBuilder = $detailsBuilder;
$this->logEntryRepository = $logEntryRepository;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
$beforeData = $afterData = [];
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
$additionalData = $logEntry->getAdditionalData();
// $logEntry->getStoreId() can return website id or store view id.
// So we need to know scope to load correct beforeData
if ($additionalData && isset($additionalData['scope'])) {
$scope = $additionalData['scope'];
}
/** @var LogDetailInterface $logDetail */
foreach ($logDetails as $logDetail) {
$oldValue = $logDetail->getOldValue();
$elementKey = $logDetail->getName();
$beforeData[$elementKey] = $this->scopeConfig->getValue(
$elementKey,
$scope,
(int)$logEntry->getStoreId()
);
$afterData[$elementKey] = $oldValue;
$this->resourceConfig->saveConfig(
$elementKey,
$oldValue,
$scope,
(int)$logEntry->getStoreId()
);
}
if ($detailsList = $this->detailsBuilder->build(Value::class, $beforeData, $afterData)) {
$newLogEntry = $this->logEntryFactory->create(
[
LogEntry::TYPE => LogEntryTypes::TYPE_RESTORE
]
);
$newLogEntry->setCategory($logEntry->getCategory())
->setCategoryName($logEntry->getCategoryName())
->setParameterName($logEntry->getParameterName())
->setElementId($logEntry->getElementId())
->setLogDetails($detailsList)
->setStoreId((int)$logEntry->getStoreId());
$this->logEntryRepository->save($newLogEntry);
}
}
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
class OrderAddress extends Common
{
/**
* @var ManagerInterface
*/
private $eventManager;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager,
ManagerInterface $eventManager
) {
parent::__construct(
$objectManager,
$dataStorage,
$storeManager
);
$this->eventManager = $eventManager;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
if (!empty($logDetails)) {
parent::restore($logEntry, $logDetails);
/** @var \Magento\Sales\Model\Order\Address $address */
$address = $this->getModelObject($logEntry, current($logDetails));
$this->eventManager->dispatch(
'admin_sales_order_address_update',
[
'order_id' => $address->getParentId()
]
);
}
}
}
@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\LogEntryRepositoryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Amasty\AdminActionsLog\Logging\Entity\SaveHandler\Catalog\Product as SaveHandlerProduct;
use Amasty\AdminActionsLog\Logging\Util\DetailsBuilder;
use Amasty\AdminActionsLog\Model\LogEntry\AdminLogEntryFactory;
use Amasty\AdminActionsLog\Model\LogEntry\LogEntry;
use Amasty\AdminActionsLog\Model\OptionSource\LogEntryTypes;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
class Product extends Common
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var DetailsBuilder
*/
private $detailsBuilder;
/**
* @var AdminLogEntryFactory
*/
private $logEntryFactory;
/**
* @var LogEntryRepositoryInterface
*/
private $logEntryRepository;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager,
ProductRepositoryInterface $productRepository,
DetailsBuilder $detailsBuilder,
AdminLogEntryFactory $logEntryFactory,
LogEntryRepositoryInterface $logEntryRepository
) {
parent::__construct($objectManager, $dataStorage, $storeManager);
$this->productRepository = $productRepository;
$this->detailsBuilder = $detailsBuilder;
$this->logEntryFactory = $logEntryFactory;
$this->logEntryRepository = $logEntryRepository;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
try {
$storeId = $logEntry->getStoreId() ?? Store::DEFAULT_STORE_ID;
$this->setCurrentStore($storeId);
$productId = $logEntry->getData(LogEntry::ELEMENT_ID);
$product = $this->productRepository->getById($productId, false, $storeId);
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(__('Unable to restore changes. Error: %1', $e->getMessage()));
}
if (!$product->hasData('store_id')) {
$storeId = $logEntry->getStoreId() ?? Store::DEFAULT_STORE_ID;
$product->setData('store_id', $storeId);
}
$beforeData = $afterData = [];
foreach ($logDetails as $detail) {
$oldValue = $detail->getOldValue();
$elementKey = $detail->getName();
$afterData[$elementKey] = $oldValue;
$beforeData[$elementKey] = $product->getData($elementKey);
$product->setData($elementKey, $oldValue);
$product->addAttributeUpdate($elementKey, $oldValue, $storeId);
}
$detailsList = $this->detailsBuilder->build(get_class($product), $beforeData, $afterData);
if (!empty($detailsList)) {
$logEntryData = [
LogEntry::TYPE => LogEntryTypes::TYPE_RESTORE,
LogEntry::ITEM => $product->getName(),
LogEntry::CATEGORY => SaveHandlerProduct::CATEGORY,
LogEntry::CATEGORY_NAME => __('Catalog Product'),
LogEntry::ELEMENT_ID => (int)$product->getId(),
LogEntry::STORE_ID => (int)$product->getStoreId()
];
$logEntry = $this->logEntryFactory->create($logEntryData);
$logEntry->setLogDetails($detailsList);
$this->logEntryRepository->save($logEntry);
}
}
}
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
class ProductOption extends Common
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var ProductCustomOptionRepositoryInterface
*/
private $productCustomOptionRepository;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager,
ProductRepositoryInterface $productRepository,
ProductCustomOptionRepositoryInterface $productCustomOptionRepository
) {
parent::__construct(
$objectManager,
$dataStorage,
$storeManager
);
$this->productRepository = $productRepository;
$this->productCustomOptionRepository = $productCustomOptionRepository;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
if (empty($logDetails) || !is_array($logEntry->getAdditionalData())) {
return;
}
$additionalData = $logEntry->getAdditionalData();
$productId = $additionalData['productId'] ?? null;
$optionId = $additionalData['optionId'] ?? null;
if (!$productId || !$optionId) {
return;
}
$product = $this->productRepository->getById($productId, false, (int)$logEntry->getStoreId());
$option = $this->productCustomOptionRepository->getProductOptions($product)
[$optionId] ?? null;
if (!$option) {
throw new NoSuchEntityException(__('Product Option with specified ID "%1" not found.', $optionId));
}
/** @var LogDetailInterface $logDetail */
foreach ($logDetails as $logDetail) {
$oldValue = $logDetail->getOldValue();
$elementKey = $logDetail->getName();
$option->setData($elementKey, $oldValue);
}
$option->setProductSku($product->getSku());
$this->setRestoreActionFlag($option);
// Workaround for \Magento\Catalog\Model\Product\Option\Repository::save()
$this->setCurrentStore((int)$logEntry->getStoreId());
$this->productCustomOptionRepository->save($option);
}
}
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity\RestoreHandler;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
class ProductOptionValue extends Common
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var ProductCustomOptionRepositoryInterface
*/
private $productCustomOptionRepository;
public function __construct(
ObjectManagerInterface $objectManager,
ObjectDataStorageInterface $dataStorage,
StoreManagerInterface $storeManager,
ProductRepositoryInterface $productRepository,
ProductCustomOptionRepositoryInterface $productCustomOptionRepository
) {
parent::__construct(
$objectManager,
$dataStorage,
$storeManager
);
$this->productRepository = $productRepository;
$this->productCustomOptionRepository = $productCustomOptionRepository;
}
public function restore(LogEntryInterface $logEntry, array $logDetails): void
{
if (empty($logDetails) || !is_array($logEntry->getAdditionalData())) {
return;
}
$additionalData = $logEntry->getAdditionalData();
$productId = $additionalData['productId'] ?? null;
$optionId = $additionalData['optionId'] ?? null;
$valueId = $additionalData['valueId'] ?? null;
if (!$productId || !$optionId || !$valueId) {
return;
}
$product = $this->productRepository->getById($productId, false, (int)$logEntry->getStoreId());
$option = $this->productCustomOptionRepository->getProductOptions($product)
[$optionId] ?? null;
if (!$option) {
throw new NoSuchEntityException(__('Product Option with specified ID "%1" not found.', $optionId));
}
$values = $option->getValues() ?? [];
if (!isset($values[$valueId])) {
throw new NoSuchEntityException(__('Product Option Value with specified ID "%1" not found.', $valueId));
}
/** @var LogDetailInterface $logDetail */
foreach ($logDetails as $logDetail) {
$oldValue = $logDetail->getOldValue();
$elementKey = $logDetail->getName();
$values[$valueId]->setData($elementKey, $oldValue);
}
$option->setProductSku($product->getSku());
$option->setValues($values);
$this->setRestoreActionFlag($option);
// Workaround for \Magento\Catalog\Model\Product\Option\Repository::save()
$this->setCurrentStore((int)$logEntry->getStoreId());
$this->productCustomOptionRepository->save($option);
}
}
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\Entity;
use Amasty\AdminActionsLog\Api\Restoring\EntityRestoreHandlerInterface;
use Amasty\AdminActionsLog\Logging\Util\ClassNameNormalizer;
use Magento\Framework\ObjectManagerInterface;
class RestoreHandlerProvider
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var ClassNameNormalizer
*/
private $classNameNormalizer;
/**
* @var RestoreHandler\Common
*/
private $commonRestoreHandler;
/**
* @var array
*/
private $entityTypes;
public function __construct(
ObjectManagerInterface $objectManager,
ClassNameNormalizer $classNameNormalizer,
RestoreHandler\Common $commonRestoreHandler,
array $entityTypes = []
) {
foreach ($entityTypes as $entityTypeClass => &$affectedObjectClasses) {
if (!is_subclass_of($entityTypeClass, EntityRestoreHandlerInterface::class)) {
throw new \LogicException(
sprintf(
'EntityRestoreHandler "%s" must implement %s',
$entityTypeClass,
EntityRestoreHandlerInterface::class
)
);
}
$affectedObjectClasses = array_map(function ($affectedClass) {
return trim($affectedClass, '\\');
}, $affectedObjectClasses);
}
$this->objectManager = $objectManager;
$this->classNameNormalizer = $classNameNormalizer;
$this->commonRestoreHandler = $commonRestoreHandler;
$this->entityTypes = $entityTypes;
}
public function get(string $objectClass): EntityRestoreHandlerInterface
{
$objectClass = $this->classNameNormalizer->execute($objectClass);
foreach ($this->entityTypes as $entityTypeClass => $affectedObjectClasses) {
if (in_array($objectClass, $affectedObjectClasses)) {
return $this->objectManager->get($entityTypeClass);
}
}
return $this->commonRestoreHandler;
}
}
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring;
use Amasty\AdminActionsLog\Api\Data\LogDetailInterface;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Restoring\Entity\RestoreHandlerProvider;
class RestoreProcessor
{
/**
* @var RestoreHandlerProvider
*/
private $restoreHandlerProvider;
public function __construct(
RestoreHandlerProvider $restoreHandlerProvider
) {
$this->restoreHandlerProvider = $restoreHandlerProvider;
}
public function restoreChanges(LogEntryInterface $logEntry): bool
{
$groupedLogDetails = $this->prepareLogDetails($logEntry);
foreach ($groupedLogDetails as $modelName => $logDetails) {
$this->restoreHandlerProvider->get($modelName)->restore($logEntry, $logDetails);
}
return true;
}
private function prepareLogDetails(LogEntryInterface $logEntry): array
{
$groupedLogDetails = [];
/** @var LogDetailInterface $logDetail */
foreach ($logEntry->getLogDetails() as $logDetail) {
$groupedLogDetails[$logDetail->getModel()][] = $logDetail;
}
return $groupedLogDetails;
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Magento\Framework\App\ObjectManager;
class RestoreValidator
{
/**
* @var RestoreValidatorProvider
*/
private $restoreValidatorProvider;
public function __construct(
?array $notRestorableCategories = null, // @deprecated
RestoreValidatorProvider $restoreValidatorProvider = null
) {
// OM for backward compatibility
$this->restoreValidatorProvider = $restoreValidatorProvider
?? ObjectManager::getInstance()->get(RestoreValidatorProvider::class);
}
public function isValid(LogEntryInterface $logEntry): bool
{
$isValid = true;
foreach ($this->restoreValidatorProvider->getValidators() as $validator) {
if (!$validator->isValid($logEntry)) {
$isValid = false;
break;
}
}
return $isValid;
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\RestoreValidator;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Amasty\AdminActionsLog\Model\OptionSource\LogEntryTypes;
class LogEntryTypeValidator implements RestoreValidatorInterface
{
/**
* @var array
*/
private $notRestorableCategories;
public function __construct(
array $notRestorableCategories
) {
$this->notRestorableCategories = $notRestorableCategories;
}
public function isValid(LogEntryInterface $logEntry): bool
{
return $logEntry->getType() == LogEntryTypes::TYPE_EDIT
&& !in_array($logEntry->getCategory(), $this->notRestorableCategories);
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\RestoreValidator;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
interface RestoreValidatorInterface
{
public function isValid(LogEntryInterface $logEntry): bool;
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring\RestoreValidator;
use Amasty\AdminActionsLog\Api\Data\LogEntryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
class ScopeValidator implements RestoreValidatorInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}
public function isValid(LogEntryInterface $logEntry): bool
{
try {
$this->storeManager->getStore((int)$logEntry->getStoreId());
return true;
} catch (NoSuchEntityException $exception) {
return false;
}
}
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Admin Actions Log for Magento 2
*/
namespace Amasty\AdminActionsLog\Restoring;
use Amasty\AdminActionsLog\Restoring\RestoreValidator\RestoreValidatorInterface;
class RestoreValidatorProvider
{
/**
* @var RestoreValidatorInterface[]
*/
private $validators;
/**
* @param array $validatorTypes ['validatorName' => ValidatorClass ]
*/
public function __construct(
array $validatorTypes = []
) {
$this->initializeValidatorTypes($validatorTypes);
}
public function getValidators(): array
{
return $this->validators;
}
private function initializeValidatorTypes(array $validatorTypes): void
{
foreach ($validatorTypes as $type => $validator) {
if (!$validator instanceof RestoreValidatorInterface) {
throw new \LogicException(
sprintf('Validator type must implement %s', RestoreValidatorInterface::class)
);
}
$this->validators[$type] = $validator;
}
}
}