From 4870aa3c93820631bcc7da9fc15f3a97c8508bff Mon Sep 17 00:00:00 2001 From: Robert Stanley Date: Fri, 10 Feb 2017 14:58:39 +0000 Subject: [PATCH] Update category component to allow setting image. Catch YAML parse exceptions. --- Model/Component/Categories.php | 15 ++++++++++++++- Model/Component/YamlComponentAbstract.php | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Model/Component/Categories.php b/Model/Component/Categories.php index da6c24f..459e2b3 100644 --- a/Model/Component/Categories.php +++ b/Model/Component/Categories.php @@ -3,6 +3,7 @@ namespace CtiDigital\Configurator\Model\Component; use CtiDigital\Configurator\Model\Exception\ComponentException; +use Magento\Framework\Webapi\Exception; use Symfony\Component\Yaml\Yaml; class Categories extends YamlComponentAbstract @@ -24,10 +25,12 @@ public function __construct( \CtiDigital\Configurator\Model\LoggingInterface $log, \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Catalog\Model\CategoryFactory $category, - \Magento\Store\Model\GroupFactory $groupFactory + \Magento\Store\Model\GroupFactory $groupFactory, + \Magento\Framework\App\Filesystem\DirectoryList $dirList ) { $this->category = $category; $this->groupFactory = $groupFactory; + $this->dirList = $dirList; parent::__construct($log, $objectManager); } @@ -118,10 +121,20 @@ public function createOrUpdateCategory( break; case 'category': break; + case 'image': + $img = basename($value); + $catMediaDir = $this->dirList->getPath('media') . '/' . 'catalog' . '/' . 'category' . '/'; + if (! @copy($value, $catMediaDir . $img)) { + $this->log->logError('Failed to find image: ' . $value, 1); + break; + } + $category->setImage($img); + break; default: $category->setCustomAttribute($attribute, $value); } } + // Set the category to be active if (!(isset($categoryValues['is_active']))) { $category->setIsActive(true); diff --git a/Model/Component/YamlComponentAbstract.php b/Model/Component/YamlComponentAbstract.php index bc6ee6e..ef15c0c 100644 --- a/Model/Component/YamlComponentAbstract.php +++ b/Model/Component/YamlComponentAbstract.php @@ -2,7 +2,10 @@ namespace CtiDigital\Configurator\Model\Component; +use Magento\Framework\Webapi\Exception; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; +use CtiDigital\Configurator\Model\Exception\ComponentException; /** * Class YamlComponentAbstract @@ -46,6 +49,10 @@ protected function parseData($source = null) $parser = new Yaml(); return $parser->parse(file_get_contents($source)); + } catch (ParseException $e) { + throw new ComponentException( + sprintf('The %s component failed to parse. Error: %s.', $source, $e->getMessage()) + ); } catch (ComponentException $e) { $this->log->logError($e->getMessage()); }