PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Dataflow/Model/Convert/Action/Abstract.php

https://github.com/FiveDigital/magento2
PHP | 257 lines | 127 code | 27 blank | 103 comment | 16 complexity | d3e47c5487bc7634205eb3de35ba3fc4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Dataflow
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Convert action abstract
  28. *
  29. * Instances of this class are used as actions in profile
  30. *
  31. * @category Mage
  32. * @package Mage_Dataflow
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. */
  35. abstract class Mage_Dataflow_Model_Convert_Action_Abstract
  36. implements Mage_Dataflow_Model_Convert_Action_Interface
  37. {
  38. /**
  39. * Action parameters
  40. *
  41. * Hold information about action container
  42. *
  43. * @var array
  44. */
  45. protected $_params;
  46. /**
  47. * Reference to profile this action belongs to
  48. *
  49. * @var Mage_Dataflow_Model_Convert_Profile_Abstract
  50. */
  51. protected $_profile;
  52. protected $_actions = array();
  53. /**
  54. * Action's container
  55. *
  56. * @var Mage_Dataflow_Model_Convert_Container_Abstract
  57. */
  58. protected $_container;
  59. protected $_actionDefaultClass = 'Mage_Dataflow_Model_Convert_Action';
  60. /**
  61. * Get action parameter
  62. *
  63. * @param string $key
  64. * @param mixed $default
  65. * @return mixed
  66. */
  67. public function getParam($key, $default=null)
  68. {
  69. if (!isset($this->_params[$key])) {
  70. return $default;
  71. }
  72. return $this->_params[$key];
  73. }
  74. /**
  75. * Set action parameter
  76. *
  77. * @param string $key
  78. * @param mixed $value
  79. * @return Mage_Dataflow_Model_Convert_Action_Abstract
  80. */
  81. public function setParam($key, $value=null)
  82. {
  83. if (is_array($key) && is_null($value)) {
  84. $this->_params = $key;
  85. } else {
  86. $this->_params[$key] = $value;
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Get all action parameters
  92. *
  93. * @return array
  94. */
  95. public function getParams()
  96. {
  97. return $this->_params;
  98. }
  99. /**
  100. * Set all action parameters
  101. *
  102. * @param array $params
  103. * @return Mage_Dataflow_Model_Convert_Action_Abstract
  104. */
  105. public function setParams($params)
  106. {
  107. $this->_params = $params;
  108. return $this;
  109. }
  110. /**
  111. * Get profile instance the action belongs to
  112. *
  113. * @return Mage_Dataflow_Model_Convert_Profile_Abstract
  114. */
  115. public function getProfile()
  116. {
  117. return $this->_profile;
  118. }
  119. /**
  120. * Set profile instance the action belongs to
  121. *
  122. * @param Mage_Dataflow_Model_Convert_Profile_Abstract $profile
  123. * @return Mage_Dataflow_Model_Convert_Action_Abstract
  124. */
  125. public function setProfile(Mage_Dataflow_Model_Convert_Profile_Interface $profile)
  126. {
  127. $this->_profile = $profile;
  128. return $this;
  129. }
  130. public function addAction(Mage_Dataflow_Model_Convert_Action_Interface $action=null)
  131. {
  132. if (is_null($action)) {
  133. $action = new $this->_actionDefaultClass();
  134. }
  135. $this->_actions[] = $action;
  136. $action->setProfile($this->getProfile());
  137. return $action;
  138. }
  139. /**
  140. * Set action's container
  141. *
  142. * @param Mage_Dataflow_Model_Convert_Container_Interface $container
  143. * @return Mage_Dataflow_Model_Convert_Action_Abstract
  144. */
  145. public function setContainer(Mage_Dataflow_Model_Convert_Container_Interface $container)
  146. {
  147. $this->_container = $container;
  148. $this->_container->setProfile($this->getProfile());
  149. $this->_container->setAction($this);
  150. return $this;
  151. }
  152. /**
  153. * Get action's container
  154. *
  155. * @param string $name
  156. * @return Mage_Dataflow_Model_Convert_Container_Abstract
  157. */
  158. public function getContainer($name=null)
  159. {
  160. if (!is_null($name)) {
  161. return $this->getProfile()->getContainer($name);
  162. }
  163. if (!$this->_container) {
  164. $class = $this->getParam('class');
  165. $this->setContainer(new $class());
  166. }
  167. return $this->_container;
  168. }
  169. public function importXml(Varien_Simplexml_Element $actionNode)
  170. {
  171. foreach ($actionNode->attributes() as $key=>$value) {
  172. $this->setParam($key, (string)$value);
  173. }
  174. if ($actionNode['use']) {
  175. $container = $this->getProfile()->getContainer((string)$actionNode['use']);
  176. } else {
  177. $this->setParam('class', $this->getClassNameByType((string)$actionNode['type']));
  178. $container = $action->getContainer();
  179. }
  180. $this->setContainer($container);
  181. if ($this->getParam('name')) {
  182. $this->getProfile()->addContainer($this->getParam('name'), $container);
  183. }
  184. foreach ($actionNode->var as $varNode) {
  185. $container->setVar((string)$varNode['name'], (string)$varNode);
  186. }
  187. foreach ($actionNode->action as $actionSubnode) {
  188. $action = $this->addAction();
  189. $action->importXml($actionSubnode);
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Run current action
  195. *
  196. * @return Mage_Dataflow_Model_Convert_Action_Abstract
  197. */
  198. public function run(array $args=array())
  199. {
  200. if ($method = $this->getParam('method')) {
  201. if (!method_exists($this->getContainer(), $method)) {
  202. $this->getContainer()->addException(
  203. 'Unable to run action method: ' . $method,
  204. Mage_Dataflow_Model_Convert_Exception::FATAL
  205. );
  206. }
  207. $this->getContainer()->addException('Starting '.get_class($this->getContainer()).' :: '.$method);
  208. if ($this->getParam('from')) {
  209. $this->getContainer()->setData($this->getContainer($this->getParam('from'))->getData());
  210. }
  211. $this->getContainer()->$method($args);
  212. if ($this->getParam('to')) {
  213. $this->getContainer($this->getParam('to'))->setData($this->getContainer()->getData());
  214. }
  215. } else {
  216. $this->addException('No method specified', Mage_Dataflow_Model_Convert_Exception::FATAL);
  217. }
  218. return $this;
  219. }
  220. public function runActions(array $args=array())
  221. {
  222. if (empty($this->_actions)) {
  223. return $this;
  224. }
  225. foreach ($this->_actions as $action) {
  226. $action->run($args);
  227. }
  228. return $this;
  229. }
  230. }