From 9463f366d6ec46e1ba92f9332bafa4ff8403dbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wac=C5=82awczyk?= Date: Tue, 9 Jul 2024 12:46:06 +0200 Subject: [PATCH] #94738 use serializer, catch thrown exception when image is missing --- Model/Menu/Node.php | 11 ++++++----- Model/Menu/Node/Image/File.php | 6 +----- Model/ResourceModel/Menu/Node.php | 18 ++++++++++++++++-- Service/Menu/SaveRequestProcessor.php | 6 +++++- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Model/Menu/Node.php b/Model/Menu/Node.php index 6da4c34d..4291d991 100644 --- a/Model/Menu/Node.php +++ b/Model/Menu/Node.php @@ -351,11 +351,12 @@ public function setSelectedItemId($selectedItemId) public function getCustomerGroups() { $customerGroups = $this->_getData(NodeInterface::CUSTOMER_GROUPS); - if ($customerGroups !== null) { - $customerGroups = explode(',', $customerGroups); - if (is_array($customerGroups) && !empty($customerGroups)) { - return $customerGroups; - } + if ($customerGroups == null) { + return []; + } + $customerGroups = explode(',', $customerGroups); + if (is_array($customerGroups) && !empty($customerGroups)) { + return $customerGroups; } return []; diff --git a/Model/Menu/Node/Image/File.php b/Model/Menu/Node/Image/File.php index 4a1b81bf..0115aaf3 100644 --- a/Model/Menu/Node/Image/File.php +++ b/Model/Menu/Node/Image/File.php @@ -123,11 +123,7 @@ public function getImageSize(string $file): array $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); $fileFullPath = $mediaDirectory->getAbsolutePath(self::PATH . $file); - try { - return getimagesize($fileFullPath); - } catch (Exception $e) { - return []; - } + return getimagesize($fileFullPath); } private function getAbsolutePath(): string diff --git a/Model/ResourceModel/Menu/Node.php b/Model/ResourceModel/Menu/Node.php index b8f481cb..8f0609fd 100644 --- a/Model/ResourceModel/Menu/Node.php +++ b/Model/ResourceModel/Menu/Node.php @@ -4,23 +4,37 @@ namespace Snowdog\Menu\Model\ResourceModel\Menu; +use Magento\Framework\Model\AbstractModel; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\Framework\Model\ResourceModel\Db\Context; +use Magento\Framework\Serialize\SerializerInterface; class Node extends AbstractDb { + protected $serializer; + + public function __construct( + Context $context, + SerializerInterface $serializer, + $connectionName = null + ) { + $this->serializer = $serializer; + parent::__construct($context, $connectionName); + } + protected function _construct() { $this->_init('snowmenu_node', 'node_id'); } - protected function _afterSave(\Magento\Framework\Model\AbstractModel $object) + protected function _afterSave(AbstractModel $object) { $connection = $this->getConnection(); $connection->delete('snowmenu_customer', ['node_id = ?' => $object->getNodeId()]); $nodeCustomerGroups = $object->getData('customer_groups'); if ($nodeCustomerGroups && is_string($nodeCustomerGroups)) { - $nodeCustomerGroups = json_decode($nodeCustomerGroups); + $nodeCustomerGroups = $this->serializer->unserialize($nodeCustomerGroups); } $insertData = []; foreach ($nodeCustomerGroups ?? [] as $customerGroup) { diff --git a/Service/Menu/SaveRequestProcessor.php b/Service/Menu/SaveRequestProcessor.php index 5ac76126..00bf894f 100644 --- a/Service/Menu/SaveRequestProcessor.php +++ b/Service/Menu/SaveRequestProcessor.php @@ -226,7 +226,11 @@ private function processImageParameters(array $nodeData, NodeInterface &$nodeObj if (empty($nodeData[NodeInterface::IMAGE_WIDTH]) || empty($nodeData[NodeInterface::IMAGE_HEIGHT]) ) { - $imageSize = $this->nodeImageFile->getImageSize($nodeData[NodeInterface::IMAGE]); + try { + $imageSize = $this->nodeImageFile->getImageSize($nodeData[NodeInterface::IMAGE]); + } catch (\Exception $e) { + $imageSize = null; + } if (!empty($imageSize)) { $nodeObject