![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/core/Model/ResourceModel/Config/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Core\Model\ResourceModel\Config; use Extmag\Core\Model\Config; use Extmag\Core\Model\ConfigFactory; use Extmag\Core\Model\ConfigRepository; use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; use Magento\Framework\Data\Collection\EntityFactoryInterface; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\Event\ManagerInterface; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; use Psr\Log\LoggerInterface; class Collection extends AbstractCollection { protected $_idFieldName = 'entity_id'; private $items = []; /** * @var ConfigFactory */ private $configFactory; /** * @var ConfigRepository */ private $configRepository; public function __construct( EntityFactoryInterface $entityFactory, LoggerInterface $logger, FetchStrategyInterface $fetchStrategy, ManagerInterface $eventManager, ConfigFactory $configFactory, ConfigRepository $configRepository, AdapterInterface $connection = null, AbstractDb $resource = null ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); $this->configFactory = $configFactory; $this->configRepository = $configRepository; } /** * Define resource model * * @return void */ protected function _construct() { $this->_init('Extmag\Core\Model\Config', 'Extmag\Core\Model\ResourceModel\Config'); } private function getAll() { if (empty($this->items)) { $items = $this->getItems(); if (empty($this->items) && !empty($items)) { /** * @var Config $item */ foreach ($items as $item) { $this->items[$item->getModule()][$item->getPath()] = [ 'value' => $item->getValue(), 'id' => $item->getId() ]; } } } } public function getValue($module, $path) { $this->getAll(); return $this->items[$module][$path]['value'] ?? null; } public function getRowId($module, $path) { $this->getAll(); return $this->items[$module][$path]['id'] ?? null; } protected function getValues() { $this->getAll(); return $this->items; } public function deleteByPath($module, $path, $isChangeCur = true) { $this->getAll(); if (isset($this->items[$module][$path]['id'])) { $model = $this->configRepository->getById($this->items[$module][$path]['id']); $this->configRepository->deleteById($model->getId()); if ($isChangeCur === true) { unset($this->items[$module][$path]); } } } public function setValue($module, $path, $value, $isChangeCur = true) { $this->getAll(); /** * @var Config $model */ if (isset($this->items[$module][$path]['value'])) { $model = $this->configRepository->getById($this->items[$module][$path]['id']); } else { $model = $this->configFactory->create(); $model->setModule($module); $model->setPath($path); } $model->setValue($value); $model = $this->configRepository->save($model); if ($isChangeCur === true) { $this->items[$module][$path]['value'] = $value; if (!isset($this->items[$module][$path]['id'])) { $this->items[$module][$path]['id'] = $model->getId(); } } return $value; } }