Skip to content

Commit

Permalink
Update category component to allow setting image.
Browse files Browse the repository at this point in the history
Catch YAML parse exceptions.
  • Loading branch information
r-stanley committed Feb 10, 2017
1 parent 132bd10 commit 4870aa3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Model/Component/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Model/Component/YamlComponentAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 4870aa3

Please sign in to comment.