From 6559f09f5ea939660792b77a49c6049d3ce0f213 Mon Sep 17 00:00:00 2001 From: Jeroen van Leusden Date: Mon, 17 Sep 2018 13:05:58 +0200 Subject: [PATCH] [WD2-282] Add logger handler --- src/Logger/Handler.php | 21 +++++++ src/Logger/Log.php | 13 ++++ src/Model/ImportProfile.php | 42 ++++++++----- src/RowModifier/AbstractRowModifier.php | 17 +++--- src/RowModifier/AttributeOptionCreator.php | 28 ++++++--- src/RowModifier/ConfigurableBuilder.php | 26 ++++---- .../ExternalCategoryManagement.php | 16 +++-- src/RowModifier/ImageDownloader.php | 20 +++--- src/RowModifier/ItemMapper.php | 34 ++++++----- src/RowModifier/NonUniqueSourceIterator.php | 11 +++- src/RowModifier/ProductCategoryMargin.php | 8 ++- src/RowModifier/ProductDisabler.php | 17 +++--- src/RowModifier/ProductMapper.php | 14 +++-- src/RowModifier/ProductUrlKey.php | 23 ++++--- src/RowModifier/SourceIterator.php | 9 ++- src/RowModifier/TemplateFieldParser.php | 5 +- src/Streamer/FileCsv.php | 50 ++++++--------- src/Streamer/HttpCsv.php | 61 ++++++++----------- src/docs/examples/basic/ExampleProfile.php | 19 ++++-- src/etc/di.xml | 17 +++++- 20 files changed, 266 insertions(+), 185 deletions(-) create mode 100644 src/Logger/Handler.php create mode 100644 src/Logger/Log.php diff --git a/src/Logger/Handler.php b/src/Logger/Handler.php new file mode 100644 index 0000000..a5e16af --- /dev/null +++ b/src/Logger/Handler.php @@ -0,0 +1,21 @@ +objectManagerFactory = $objectManagerFactory; $this->stopwatch = $stopwatch; $this->consoleOutput = $consoleOutput; + $this->log = $log; } @@ -78,19 +80,24 @@ public function run() $errors = $importer->processImport($items); $stopwatchEvent = $this->stopwatch->stop('importinstance'); - $this->consoleOutput->writeln((string) new Phrase( + $output = (string) new Phrase( '%1 items imported in %2 sec, %3 items / sec (%4mb used)', [ count($items), round($stopwatchEvent->getDuration() / 1000, 1), round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1), round($stopwatchEvent->getMemory() / 1024 / 1024, 1) - ])); + ]); + + $this->consoleOutput->writeln($output); + $this->log->addInfo($output); $this->consoleOutput->writeln("$errors"); + $this->log->addError($errors); return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } catch (\Exception $e) { $this->consoleOutput->writeln($e->getMessage()); + $this->log->addCritical($e->getMessage()); return \Magento\Framework\Console\Cli::RETURN_FAILURE; } @@ -99,12 +106,13 @@ public function run() /** * Get all items that need to be imported * - * @return \[] + * @return array */ private function getItemsMeasured() { $this->stopwatch->start('profileinstance'); $this->consoleOutput->writeln('Getting item data'); + $this->log->addInfo('Getting item data'); $items = $this->getItems(); $stopwatchEvent = $this->stopwatch->stop('profileinstance'); @@ -112,13 +120,17 @@ private function getItemsMeasured() return $items; } - $this->consoleOutput->writeln((string)new Phrase('%1 items processed in %2 sec, %3 items / sec (%4mb used)', + $output = (string) new Phrase('%1 items processed in %2 sec, %3 items / sec (%4mb used)', [ count($items), round($stopwatchEvent->getDuration() / 1000, 1), round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1), round($stopwatchEvent->getMemory() / 1024 / 1024, 1) - ])); + ]); + + $this->consoleOutput->writeln($output); + $this->log->addInfo($output); + return $items; } diff --git a/src/RowModifier/AbstractRowModifier.php b/src/RowModifier/AbstractRowModifier.php index 06b0fd9..51da293 100644 --- a/src/RowModifier/AbstractRowModifier.php +++ b/src/RowModifier/AbstractRowModifier.php @@ -6,6 +6,7 @@ namespace Ho\Import\RowModifier; +use Ho\Import\Logger\Log; use Symfony\Component\Console\Output\ConsoleOutput; /** @@ -15,7 +16,6 @@ */ abstract class AbstractRowModifier { - /** * Log items to the console. * @@ -23,7 +23,10 @@ abstract class AbstractRowModifier */ protected $consoleOutput; - + /** + * @var Log + */ + protected $log; /** * Item array with all items to import @@ -33,17 +36,15 @@ abstract class AbstractRowModifier protected $items; /** - * AbstractRowModifier constructor. - * * @param ConsoleOutput $consoleOutput + * @param Log $log */ - public function __construct( - ConsoleOutput $consoleOutput - ) { + public function __construct(ConsoleOutput $consoleOutput, Log $log) + { $this->consoleOutput = $consoleOutput; + $this->log = $log; } - /** * Set the data array for fields to import * diff --git a/src/RowModifier/AttributeOptionCreator.php b/src/RowModifier/AttributeOptionCreator.php index 8cd39fc..fa176f9 100644 --- a/src/RowModifier/AttributeOptionCreator.php +++ b/src/RowModifier/AttributeOptionCreator.php @@ -1,11 +1,12 @@ optionManagement = $optionManagement; $this->optionFactory = $optionFactory; $this->attributes = $attributes; } - /** * Automatically create attribute options. * + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\StateException + * * @return void */ public function process() { $attributes = implode(', ', $this->attributes); $this->consoleOutput->writeln("Creating attribute options for: {$attributes}"); + $this->log->addInfo('Creating attribute options for:'. $attributes); + foreach ($this->attributes as $attribute) { $this->createForAttribute($attribute); } } - /** * Create attribute options * * @param string $attributeCode + * + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\StateException + * * @return void */ public function createForAttribute(string $attributeCode) @@ -88,7 +96,7 @@ public function createForAttribute(string $attributeCode) $items = $this->optionManagement->getItems($attributeCode); foreach ($items as $item) { - if (in_array($item->getLabel(), $uniqueOptions)) { + if (\in_array($item->getLabel(), $uniqueOptions)) { unset($uniqueOptions[$item->getLabel()]); } } @@ -118,10 +126,12 @@ protected function getNonExistingAttributes(string $attribute) continue; } - if (! is_string($item[$attribute])) { + if (! \is_string($item[$attribute])) { $this->consoleOutput->writeln( "AttributeOptionCreator: Invalid value for {$attribute} {$identifier}" ); + $this->log->addError("AttributeOptionCreator: Invalid value for {$attribute} {$identifier}"); + $item[$attribute] = ''; continue; } diff --git a/src/RowModifier/ConfigurableBuilder.php b/src/RowModifier/ConfigurableBuilder.php index 9c8172f..a979f82 100644 --- a/src/RowModifier/ConfigurableBuilder.php +++ b/src/RowModifier/ConfigurableBuilder.php @@ -1,19 +1,17 @@ lineFormatterMulti = $lineFormatterMulti; $this->configurableSku = $configurableSku; $this->attributes = $attributes; @@ -96,7 +93,8 @@ public function __construct( */ public function process() { - $this->consoleOutput->writeln("Creating configurable products... "); + $this->consoleOutput->writeln("Creating configurable products..."); + $this->log->addInfo('Creating configurable products...'); $skuCallback = $this->configurableSku; $attrCallback = $this->attributes; @@ -133,9 +131,11 @@ public function process() $configurables[$configurableSku]['configurable_variations'][] = $variation; } + unset($item); $count = count($configurables); $this->consoleOutput->writeln("{$count} potential configurables created"); + $this->log->addInfo("{$count} potential configurables created"); $configurables = $this->splitOnValue($configurables); if ($this->enableFilterConfigurable) { @@ -154,6 +154,7 @@ public function process() $count = count($configurables); $this->consoleOutput->writeln("Created {$count} configurables"); + $this->log->addInfo("Created {$count} configurables"); $this->items += $configurables; } @@ -174,7 +175,7 @@ private function initConfigurable($item, $configurableSku) //@todo implement the configurable product mapper by using ItemMapper foreach ($this->configurableValues as $key => $value) { - if (is_callable($value)) { + if (\is_callable($value)) { $value = $value($configurable); } $configurable[$key] = $value; @@ -254,6 +255,7 @@ private function splitOnValue($configurables) $count = count($newConfigurables) - count($configurables); if ($count > 0) { $this->consoleOutput->writeln("Created {$count} extra configurables while splitting"); + $this->log->addInfo("Created {$count} extra configurables while splitting"); } return $newConfigurables; @@ -273,7 +275,7 @@ private function setSimpleValues($configurables) $item =& $this->items[$simpleData['sku']]; //@todo implement the simple product mapper by using ItemMapper foreach ($this->simpleValues as $key => $value) { - if (is_callable($value)) { + if (\is_callable($value)) { $value = $value($item); } $item[$key] = $value; diff --git a/src/RowModifier/ExternalCategoryManagement.php b/src/RowModifier/ExternalCategoryManagement.php index 2a09c2a..418b7b5 100644 --- a/src/RowModifier/ExternalCategoryManagement.php +++ b/src/RowModifier/ExternalCategoryManagement.php @@ -1,21 +1,17 @@ categoryCollectionFactory = $categoryCollectionFactory; $this->productCollectionFactory = $productCollectionFactory; $this->externalCategoryPathFilter = $externalCategoryPathFilter; @@ -73,6 +70,7 @@ public function __construct( public function process() { $this->consoleOutput->writeln("Adding existing product category associations"); + $this->log->addInfo('Adding existing product category associations'); $this->initCategoryMapping(); $this->initCategoryProductMapping(); diff --git a/src/RowModifier/ImageDownloader.php b/src/RowModifier/ImageDownloader.php index f5a9505..3505bb8 100644 --- a/src/RowModifier/ImageDownloader.php +++ b/src/RowModifier/ImageDownloader.php @@ -1,6 +1,6 @@ directoryList = $directoryList; - $this->httpClient = new HttpClient(); -// $this->progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->consoleOutput); - $this->stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); + $this->httpClient = new HttpClient(); } /** diff --git a/src/RowModifier/ItemMapper.php b/src/RowModifier/ItemMapper.php index 25250de..37e7db4 100644 --- a/src/RowModifier/ItemMapper.php +++ b/src/RowModifier/ItemMapper.php @@ -1,11 +1,12 @@ mapping = $mapping; } @@ -67,6 +66,7 @@ public function validateItem(array $item, $sku) public function process() { $this->consoleOutput->writeln("Mapping item information"); + $this->log->addInfo('Mapping item information'); foreach ($this->items as $identifier => $item) { try { @@ -81,27 +81,31 @@ public function process() return null; } - if (is_array($value) || is_object($value)) { + if (\is_array($value) || \is_object($value)) { $val = print_r($filteredItem, true); $this->consoleOutput->writeln( "Value is a object or array for $identifier: {$val}" ); + $this->log->addInfo("Value is a object or array for $identifier: {$val}"); + return null; } return $value; }, $filteredItem); } else { - $this->consoleOutput->writeln( - sprintf("Error validating, skipping %s: %s", - $identifier, implode(",", $itemsValidated)) - ); + $output = sprintf('Error validating, skipping %s: %s', $identifier, implode(',', $itemsValidated)); + + $this->consoleOutput->writeln(sprintf('%s', $output)); + $this->log->addError($output); } } catch (\Exception $e) { $this->consoleOutput->writeln( "ItemMapper: {$e->getMessage()} (removing product {$identifier})" ); + $this->log->addError("ItemMapper: {$e->getMessage()} (removing product {$identifier})"); + unset($this->items[$identifier]); } } @@ -114,7 +118,7 @@ public function mapItem(array $item) : array { $product = []; foreach ($this->mapping as $key => $value) { - if (is_callable($value)) { + if (\is_callable($value)) { $value = $value($item); } diff --git a/src/RowModifier/NonUniqueSourceIterator.php b/src/RowModifier/NonUniqueSourceIterator.php index 2a12a91..ecde90e 100644 --- a/src/RowModifier/NonUniqueSourceIterator.php +++ b/src/RowModifier/NonUniqueSourceIterator.php @@ -5,6 +5,7 @@ */ namespace Ho\Import\RowModifier; +use Ho\Import\Logger\Log; use Symfony\Component\Console\Output\ConsoleOutput; /** @@ -52,14 +53,17 @@ class NonUniqueSourceIterator extends AbstractRowModifier * First argument of the \Closure will be the $item * @param \Iterator $iterator \Ho\Import\Streamer\FileXml or \Ho\Import\Streamer\HttpXml * @param string $nodeName Array key where the data will be stored in the parent + * @param Log $log */ public function __construct( ConsoleOutput $consoleOutput, \Closure $identifier, \Iterator $iterator, - $nodeName + $nodeName, + Log $log ) { - parent::__construct($consoleOutput); + parent::__construct($consoleOutput, $log); + $this->identifier = $identifier; $this->iterator = $iterator; $this->nodeName = $nodeName; @@ -73,6 +77,7 @@ public function __construct( public function process() { $this->consoleOutput->writeln("NonUniqueSourceIterator: Adding information from stream"); + $this->log->addInfo('NonUniqueSourceIterator: Adding information from stream'); $identifier = $this->identifier; foreach ($this->iterator as $item) { @@ -84,6 +89,8 @@ public function process() $this->consoleOutput->writeln( "NonUniqueSourceIterator: Item not found for {$id}" ); + $this->log->addInfo("NonUniqueSourceIterator: Item not found for {$id}"); + continue; } $this->items[$id][$this->nodeName][] = $item; diff --git a/src/RowModifier/ProductCategoryMargin.php b/src/RowModifier/ProductCategoryMargin.php index b5f0e5a..563c793 100644 --- a/src/RowModifier/ProductCategoryMargin.php +++ b/src/RowModifier/ProductCategoryMargin.php @@ -7,6 +7,7 @@ namespace Ho\Import\RowModifier; use Ho\Import\Helper\LineFormatterMulti; +use Ho\Import\Logger\Log; use Magento\Catalog\Model\Category; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; use Symfony\Component\Console\Output\ConsoleOutput; @@ -35,18 +36,19 @@ class ProductCategoryMargin extends AbstractRowModifier private $lineFormatterMulti; /** - * ExternalCategoryManagement constructor. - * * @param ConsoleOutput $consoleOutput + * @param Log $log * @param CategoryCollectionFactory $categoryCollectionFactory * @param LineFormatterMulti $lineFormatterMulti */ public function __construct( ConsoleOutput $consoleOutput, + Log $log, CategoryCollectionFactory $categoryCollectionFactory, LineFormatterMulti $lineFormatterMulti ) { - parent::__construct($consoleOutput); + parent::__construct($consoleOutput, $log); + $this->categoryCollectionFactory = $categoryCollectionFactory; $this->lineFormatterMulti = $lineFormatterMulti; } diff --git a/src/RowModifier/ProductDisabler.php b/src/RowModifier/ProductDisabler.php index 51de4b0..cd0baac 100644 --- a/src/RowModifier/ProductDisabler.php +++ b/src/RowModifier/ProductDisabler.php @@ -1,17 +1,16 @@ profile = $profile; - $this->connection = $resource->getConnection(); + parent::__construct($consoleOutput, $log); + + $this->profile = $profile; + $this->connection = $resource->getConnection(); $this->consoleOutput = $consoleOutput; $this->force = $force; } @@ -85,6 +85,7 @@ public function process() $count = count($itemsToDisable); $this->consoleOutput->writeln("ProductDisabler: Disabling {$count} products"); + $this->log->addInfo("ProductDisabler: Disabling {$count} products"); $this->items += $itemsToDisable; } diff --git a/src/RowModifier/ProductMapper.php b/src/RowModifier/ProductMapper.php index 08445a1..15f72ef 100644 --- a/src/RowModifier/ProductMapper.php +++ b/src/RowModifier/ProductMapper.php @@ -1,6 +1,6 @@ consoleOutput->writeln("Mapping product information"); + $this->log->addInfo('Mapping product information'); foreach ($this->getSourceItems() as $item) { try { @@ -49,14 +50,15 @@ public function process() return $item; }, $filteredItem); } else { - $this->consoleOutput->writeln( - sprintf("Error validating, skipping %s: %s", - $identifier, implode(",", $errors)) - ); + $output = sprintf('Error validating, skipping %s: %s', $identifier, implode(',', $errors)); + + $this->consoleOutput->writeln(sprintf('%s', $output)); + $this->log->addInfo($output); } } catch (\Exception $e) { $this->consoleOutput->writeln("{$e->getMessage()}"); + $this->log->addError($e->getMessage()); } } } @@ -68,7 +70,7 @@ public function mapItem(array $item) : array { $product = []; foreach ($this->getMappingDefinition() as $key => $value) { - if (is_callable($value)) { + if (\is_callable($value)) { $value = $value($item); } diff --git a/src/RowModifier/ProductUrlKey.php b/src/RowModifier/ProductUrlKey.php index d4d087e..d3ad9c4 100644 --- a/src/RowModifier/ProductUrlKey.php +++ b/src/RowModifier/ProductUrlKey.php @@ -1,10 +1,12 @@ urlKeyFormatter = $urlKeyFormatter; - $this->urlFinderInterface = $urlFinderInterface; + parent::__construct($consoleOutput, $log); + + $this->urlKeyFormatter = $urlKeyFormatter; + $this->urlFinderInterface = $urlFinderInterface; $this->productCollectionFactory = $productCollectionFactory; - $this->scopeConfig = $scopeConfig; + $this->scopeConfig = $scopeConfig; + $this->consoleOutput = $consoleOutput; } /** @@ -101,15 +105,18 @@ public function __construct( public function process() { $this->consoleOutput->writeln("Checking url_keys and make sure they are unique"); + $this->log->addInfo('Checking url_keys and make sure they are unique'); $this->initProductToSku(); foreach ($this->items as $identifier => &$item) { //check for existence. if (empty($item['url_key'])) { $this->consoleOutput->writeln("ProductUrlKey: url_key not found {$identifier}"); + $this->log->addInfo('ProductUrlKey: url_key not found '.$identifier); } $item['url_key'] = $this->getUrlKey($item['url_key'], $identifier); } + $this->reset(); } @@ -167,9 +174,11 @@ public function getUrlKey($urlKey, $identifier) } continue; } + $this->consoleOutput->writeln( "Can not find available URL-key for {$identifier}, you might run into trouble" ); + $this->log->addError("Can not find available URL-key for {$identifier}, you might run into trouble"); } /** diff --git a/src/RowModifier/SourceIterator.php b/src/RowModifier/SourceIterator.php index 6804520..9c2d6df 100644 --- a/src/RowModifier/SourceIterator.php +++ b/src/RowModifier/SourceIterator.php @@ -6,6 +6,7 @@ namespace Ho\Import\RowModifier; use Closure; +use Ho\Import\Logger\Log; use Iterator; use Symfony\Component\Console\Output\ConsoleOutput; @@ -58,15 +59,18 @@ class SourceIterator extends AbstractRowModifier * to identify the parent. Can return a String or Array. * First argument of the \Closure will be the item * @param Iterator $iterator \Ho\Import\Streamer\FileXml or \Ho\Import\Streamer\HttpXml + * @param Log $log * @param string $mode self::MODE_ADD or self::MODE_CREATE */ public function __construct( ConsoleOutput $consoleOutput, Closure $identifier, Iterator $iterator, + Log $log, $mode = self::MODE_CREATE ) { - parent::__construct($consoleOutput); + parent::__construct($consoleOutput, $log); + $this->identifier = $identifier; $this->iterator = $iterator; $this->mode = $mode; @@ -80,6 +84,7 @@ public function __construct( public function process() { $this->consoleOutput->writeln("SourceIterator: Adding information from stream"); + $this->log->addInfo('SourceIterator: Adding information from stream'); $identifier = $this->identifier; foreach ($this->iterator as $item) { @@ -100,6 +105,8 @@ public function process() $this->consoleOutput->writeln( "SourceIterator: Item not found for {$id}" ); + $this->log->addInfo("SourceIterator: Item not found for {$id}"); + continue; } $this->items[$id] += $item; diff --git a/src/RowModifier/TemplateFieldParser.php b/src/RowModifier/TemplateFieldParser.php index 698508f..acd39b1 100644 --- a/src/RowModifier/TemplateFieldParser.php +++ b/src/RowModifier/TemplateFieldParser.php @@ -7,6 +7,7 @@ namespace Ho\Import\RowModifier; +use Ho\Import\Logger\Log; use Magento\Framework\Filter\Template; use Magento\Framework\Message\ManagerInterface; use Symfony\Component\Console\Output\ConsoleOutput; @@ -26,15 +27,17 @@ class TemplateFieldParser extends AbstractRowModifier * @param ConsoleOutput $consoleOutput * @param Template $filterTemplate * @param ManagerInterface $messageManager + * @param Log $log * @param array $templateFields */ public function __construct( ConsoleOutput $consoleOutput, Template $filterTemplate, ManagerInterface $messageManager, + Log $log, $templateFields = [] ) { - parent::__construct($consoleOutput); + parent::__construct($consoleOutput, $log); $this->filterTemplate = $filterTemplate; $this->messageManager = $messageManager; diff --git a/src/Streamer/FileCsv.php b/src/Streamer/FileCsv.php index 5dff9de..8d41154 100644 --- a/src/Streamer/FileCsv.php +++ b/src/Streamer/FileCsv.php @@ -1,11 +1,12 @@ consoleOutput = $consoleOutput; $this->directoryList = $directoryList; $this->requestFile = $requestFile; + $this->log = $log; $this->headers = $headers; - $this->csvOptions = $csvOptions; - $this->limit = $limit; } /** - * @return \Generator * @throws FileSystemException * @throws \League\Csv\Exception + * + * @return \Generator */ public function getIterator() { $this->consoleOutput->writeln( "Streamer\FileCsv: Getting data from requestFile {$this->requestFile}" ); + $this->log->addInfo('Streamer\FileCsv: Getting data from requestFile '.$this->requestFile); $requestFile = $this->getRequestFile(); if (! file_exists($requestFile)) { - throw new FileSystemException(__("requestFile %1 not found", $requestFile)); + $this->log->addCritical(sprintf('requestFile %s not found', $requestFile)); + throw new FileSystemException(__('requestFile %1 not found', $requestFile)); } $requestFile = fopen($requestFile, 'r'); $csvReader = \League\Csv\Reader::createFromStream($requestFile); - if (empty($this->headers)) - { + if (empty($this->headers)) { $csvReader->setHeaderOffset(0); } foreach ($csvReader->getIterator() as $row) { @@ -114,7 +100,7 @@ public function getIterator() /** * @return string */ - private function getRequestFile():string + private function getRequestFile(): string { return $this->requestFile[0] == '/' ? $this->requestFile diff --git a/src/Streamer/HttpCsv.php b/src/Streamer/HttpCsv.php index 3198a34..f9936fc 100644 --- a/src/Streamer/HttpCsv.php +++ b/src/Streamer/HttpCsv.php @@ -7,6 +7,7 @@ namespace Ho\Import\Streamer; use Bakame\Psr7\Factory\StreamWrapper; +use Ho\Import\Logger\Log; use Psr\Cache\CacheItemPoolInterface as CachePool; use Symfony\Component\Console\Output\ConsoleOutput; @@ -28,7 +29,6 @@ */ class HttpCsv { - /** * @var CachePool */ @@ -50,26 +50,10 @@ class HttpCsv private $requestMethod; /** - * @var [] + * @var array */ private $requestOptions; -// -// /** -// * @var string -// */ -// private $xmlParser; -// -// /** -// * @var [] -// */ -// private $xmlOptions; -// -// /** -// * @var int -// */ -// private $limit; -// /** * @var int */ @@ -81,37 +65,37 @@ class HttpCsv public $headers; /** - * Xml constructor. - * + * @var Log + */ + private $log; + + /** * @param CachePool $cacheItemPool * @param ConsoleOutput $consoleOutput - * * @param string $requestUrl + * @param Log $log * @param string $requestMethod * @param array $requestOptions - * @param string $xmlParser - * @param array $xmlOptions - * @param int $limit * @param int $ttl - * + * @param array $headers */ public function __construct( CachePool $cacheItemPool, ConsoleOutput $consoleOutput, string $requestUrl, + Log $log, string $requestMethod = 'GET', array $requestOptions = [], -// int $limit = PHP_INT_MAX, - int $ttl = (12 * 3600), - $headers = [] + int $ttl = 12 * 3600, + array $headers = [] ) { - $this->requestUrl = $requestUrl; - $this->requestMethod = $requestMethod; - $this->requestOptions = $requestOptions; -// $this->limit = $limit; $this->cacheItemPool = $cacheItemPool; $this->consoleOutput = $consoleOutput; - $this->ttl = $ttl; + $this->requestUrl = $requestUrl; + $this->log = $log; + $this->requestMethod = $requestMethod; + $this->requestOptions = $requestOptions; + $this->ttl = $ttl; $this->headers = $headers; } @@ -124,15 +108,18 @@ public function getHeaders() } /** - * Get the source iterator - * @return \Generator * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \League\Csv\Exception + * + * @return \Generator */ public function getIterator() { $this->consoleOutput->write( "Streamer\HttpCsv: Getting data from URL {$this->requestUrl}" ); + $this->log->addInfo('Streamer\HttpCsv: Getting data from URL '.$this->requestUrl); + $stack = \GuzzleHttp\HandlerStack::create(); $stack->push(new \Kevinrob\GuzzleCache\CacheMiddleware( new \Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy( @@ -151,10 +138,10 @@ public function getIterator() ); $csvReader = \League\Csv\Reader::createFromStream(StreamWrapper::getResource($response->getBody())); - if (empty($this->headers)) - { + if (empty($this->headers)) { $csvReader->setHeaderOffset(0); } + foreach ($csvReader->getIterator() as $row) { yield (empty($this->headers) ? $row : array_combine($this->headers, $row)); } diff --git a/src/docs/examples/basic/ExampleProfile.php b/src/docs/examples/basic/ExampleProfile.php index 8d9f513..fc5c598 100644 --- a/src/docs/examples/basic/ExampleProfile.php +++ b/src/docs/examples/basic/ExampleProfile.php @@ -1,10 +1,15 @@ xmlStreamerFactory = $xmlStreamerFactory; + parent::__construct($objectManagerFactory, $stopwatch, $consoleOutput, $log); + + $this->xmlStreamerFactory = $xmlStreamerFactory; $this->sourceIteratorFactory = $sourceIteratorFactory; - $this->itemMapperFactory = $itemMapperFactory; + $this->itemMapperFactory = $itemMapperFactory; } /** diff --git a/src/etc/di.xml b/src/etc/di.xml index 8d0bc88..2c09586 100644 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -13,6 +13,20 @@ + + + Magento\Framework\Filesystem\Driver\File + + + + + import_logging + + Ho\Import\Logger\Handler + + + + @@ -20,6 +34,5 @@ - +