Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

+ (add) Importer logic #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/code/community/Kirchbergerknorr/Importer/Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @category Kirchbergerknorr
* @package Kirchbergerknorr/Importer/Helper
* @subpackage Config
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2016 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Kirchbergerknorr_Importer_Helper_Config
{
const PATH = 'kirchbergerknorr/importer/';

// TODO: implement multiple config possibility
/**
* get importer config
*
* @param string $key config key
* @param int $type (NOT IMPLEMENTED YET) from which importer config
* @return mixed config value
*/
public function get($key, $type = 0)
{
return Mage::getStoreConfig(self::PATH . $key);
}
}
132 changes: 132 additions & 0 deletions app/code/community/Kirchbergerknorr/Importer/Helper/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* @category Kirchbergerknorr
* @package Kirchbergerknorr/Importer/Helper
* @subpackage Logger
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2016 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

// TODO: validate if interface is needed
// TODO: get calling method
/**
* Class Kirchbergerknorr_PcoInterface_Helper_Logger for logging
*/
class Kirchbergerknorr_Importer_Helper_Logger
{
// TODO: config
/**
* @var string log destination path
*/
private static $destination = 'importer/import';

/**
* @var bool set if debug is enabled for interface
*/
protected static $debug = null;

/**
* @var string suffix for logging file, will be created in constructor
*/
protected $suffix = '';

/**
* Kirchbergerknorr_Importer_Helper_Logger constructor.
*
* @param string[] $arguments for file suffix
*/
public function __construct($arguments = array())
{
$path = array();
if (isset($arguments['type'])) {
$path[] = $arguments['type'];
}
if (isset($arguments['name'])) {
$path[] = $arguments['name'];
}

$this->suffix = implode('_', $path);
}

/**
* Log given message
*
* @param string $message message to log
* @param string $type (optional) logging type, for different file location
*
* @return Kirchbergerknorr_Importer_Helper_Logger
*/
public function log($message, $type = '')
{
// TODO: Db logging and file logging
if($message) {
// Log for admin Backend
echo $message . "\n";
Mage::log($message . "\n", null, static::$destination . $this->getSuffix($type) . '.log', true);
}
return $this;
}

/**
* Debug Log given message if debug is enabled in config
*
* @param string|mixed $message message to log
* @param string $type (optional) logging type, for different file location
*
* @return Kirchbergerknorr_Importer_Helper_Logger for chaining
*/
public function debug($message, $type = '')
{
if(self::isDebug() && $message) {
Mage::log(((is_string($message)) ? $message : var_export($message, true)) . "\n", null, static::$destination . $this->getSuffix($type) . '_debug.log', true);
}
return $this;
}

// TODO: think about a own exception log
/**
* @param Exception $exception exception to log
*
* @return Kirchbergerknorr_Importer_Helper_Logger for chaining
*/
public function exception($exception)
{
Mage::logException($exception);
return $this;
}

/**
* is debug enabled
*
* @return bool true if enabled, else false
*/
public static function isDebug()
{
if (self::$debug === null) {
// TODO: create config
self::$debug = true;
// self::$debug = (bool) Mage::getStoreConfig('kirchbergerknorr/importer/debug_enabled');
}
return self::$debug;
}

/**
* get file suffix
*
* @param string $type (optional) additional suffix for file
*
* @return string file suffix
*/
protected function getSuffix($type = '')
{
$suffix = array();
if ($this->suffix) {
$suffix[] = strtolower($this->suffix);
}
if ($type) {
$suffix[] = strtolower($type);
}
return (($suffix) ? '_' . implode('_', $suffix) : '');
}
}
153 changes: 153 additions & 0 deletions app/code/community/Kirchbergerknorr/Importer/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* @category Kirchbergerknorr
* @package Kirchbergerknorr/Importer/Model
* @subpackage Config
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2016 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Kirchbergerknorr_Importer_Model_Config
{
const IMPORTER_NAMESPACE = 'kirchbergerknorr_importer';

/**
* @var mixed[] config data
*/
protected $config;

/**
* @var Kirchbergerknorr_Importer_Model_Factory helper factory
*/
protected $factory;

/**
* Kirchbergerknorr_Importer_Model_Config constructor.
*
* @param mixed[] $config importer config
*/
public function __construct($config)
{
$this->config = $config;
$this->factory = Mage::getModel('kirchbergerknorr_importer/factory');
}

// TODO: refactor if get as simple get- value is needed, else remove this
/**
* get raw values from config without magic
*
* @param string $key config key
*
* @return mixed raw value
*/
public function getValue($key)
{
if (($value = $this->value($key))) {
return $value;
}
return null;
}

/**
* get value from config for given key - uses magic
*
* @param string $key config key
* @param mixed[] $arguments (optional) additional parameters for magic object creation
*
* @return mixed value from config
*/
public function get($key, $arguments = array())
{
return $this->config($key, $arguments);
}

/**
* get dynamic value from config - replaces YYYY, MM, DD with date
*
* @param string $key config key
*
* @return mixed value, replaced with date if it is a string
*/
public function dynamic($key)
{
$value = $this->get($key);
if ($value && is_string($value)) {
$date = new DateTime('now');
// TODO: optimize - currently all occurrences are replaced
$value = str_replace(
array('YYYY', 'MM', 'DD'),
array($date->format('Y'), $date->format('m'), $date->format('d')),
$value
);
}
return $value;
}

/**
* magic config return values depending on type
*
* @param string $key key in config which is wanted
* @param mixed[] $arguments (optional) additional arguments for objects
* @param string[] $identifiers (optional) identifiers for array handling -> which key is needed to create a object, else it returns only a array
*
* @return mixed magic values
*/
protected function config($key, $arguments = array(), $identifiers = array('model'))
{
if ($value = $this->value($key)) {
switch (gettype($value)) {
case 'array':
// exclude mapping, to have a free namespace
if ($key !== 'mapping') {
foreach ($identifiers as $identifier) {
if (!empty($value[$identifier]) && is_string($value[$identifier])) {;
$arguments = (!empty($value['arguments'])) ? array_merge($arguments, $value['arguments']) : $arguments;
$type = end(explode('/', $key));
return $this->factory->get($type, $value[$identifier], $arguments);
}
}
}
return $value;
break;
case 'string':
// TODO: think about it, possible that it tries to crerate to much models, but config is only read one time
if ($model = $this->factory->get($key, $value, $arguments)) {
return $model;
}
return $value;
break;
case 'object':
// TODO: implement stuff, if needed
break;
case 'NULL':
// TODO: implement, if needed
break;
default:
return $value;
break;
}
return $value;
}
return null;
}

/**
* get config value from key - can handle sub-keys splitted with '/'
*
* @param string $key key to get
*
* @return mixed|null value if found
*/
protected function value($key)
{
$config = $this->config;
foreach (explode('/', $key) as $key) {
if (isset($config[$key])) {
$config = $config[$key];
} else {
return null;
}
}
return $config;
}
}
68 changes: 68 additions & 0 deletions app/code/community/Kirchbergerknorr/Importer/Model/Decorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @category Kirchbergerknorr
* @package Kirchbergerknorr/Importer/Model
* @subpackage Decorator
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2016 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
abstract class Kirchbergerknorr_Importer_Model_Decorator implements Kirchbergerknorr_Importer_Model_Decorator_Interface
{
/**
* @var mixed data to decorate
*/
protected $data;

/**
* @var string name for writer
*/
protected $name;

// TODO: use TRAITS
/**
* Kirchbergerknorr_Importer_Model_Decorator constructor.
*
* @param mixed[] $arguments constructor arguments
* @param string[] $keys (optional) additional keys to check in arguments
*
* @throws Kirchbergerknorr_Importer_Model_Decorator_Exception_InvalidArgumentException if empty name in arguments
*
* @typedef Arguments
* @var string name name for which decorator is used
*/
public function __construct($arguments, $keys = array())
{
if (empty($arguments['name'])) {
throw new Kirchbergerknorr_Importer_Model_Decorator_Exception_InvalidArgumentException('Mapping Name Missing');
}
foreach (array_merge($keys, array('name')) as $config) {
if (isset($arguments[$config])) {
$this->$config = $arguments[$config];
}
}
}

/**
* get name for which the decorator is for
*
* @return string name for writer
*/
public function getName()
{
return $this->name;
}

/**
* set decorator decorate
*
* @param mixed $data data to decorate
*
* @return Kirchbergerknorr_Importer_Model_Decorator for chaining
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* @category Kirchbergerknorr
* @package Kirchbergerknorr/Importer/Model/Decorator/Exception
* @subpackage InvalidArgumentException
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2016 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Kirchbergerknorr_Importer_Model_Decorator_Exception_InvalidArgumentException extends InvalidArgumentException
{}
Loading