/src/Plugins/AbstractPlugin.php

https://github.com/techdivision/import · PHP · 322 lines · 123 code · 37 blank · 162 comment · 11 complexity · cbf009d56daa0ac0b37e74e9b2b2d9e0 MD5 · raw file

  1. <?php
  2. /**
  3. * TechDivision\Import\Plugins\AbstractPlugin
  4. *
  5. * PHP version 7
  6. *
  7. * @author Tim Wagner <t.wagner@techdivision.com>
  8. * @copyright 2016 TechDivision GmbH <info@techdivision.com>
  9. * @license https://opensource.org/licenses/MIT
  10. * @link https://github.com/techdivision/import
  11. * @link http://www.techdivision.com
  12. */
  13. namespace TechDivision\Import\Plugins;
  14. use TechDivision\Import\Utils\LoggerKeys;
  15. use TechDivision\Import\ApplicationInterface;
  16. use TechDivision\Import\Configuration\PluginConfigurationInterface;
  17. use TechDivision\Import\Adapter\ImportAdapterInterface;
  18. use TechDivision\Import\Utils\RegistryKeys;
  19. use TechDivision\Import\Loggers\SwiftMailer\TransportMailerFactoryInterface;
  20. /**
  21. * Abstract plugin implementation.
  22. *
  23. * @author Tim Wagner <t.wagner@techdivision.com>
  24. * @copyright 2016 TechDivision GmbH <info@techdivision.com>
  25. * @license https://opensource.org/licenses/MIT
  26. * @link https://github.com/techdivision/import
  27. * @link http://www.techdivision.com
  28. */
  29. abstract class AbstractPlugin implements PluginInterface
  30. {
  31. /**
  32. * The application instance.
  33. *
  34. * @var \TechDivision\Import\ApplicationInterface
  35. */
  36. protected $application;
  37. /**
  38. * The plugin configuration instance.
  39. *
  40. * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
  41. */
  42. protected $pluginConfiguration;
  43. /**
  44. * The import adapter instance.
  45. *
  46. * @var \TechDivision\Import\Adapter\ImportAdapterInterface
  47. */
  48. protected $importAdapter;
  49. /**
  50. * Initializes the plugin with the application instance.
  51. *
  52. * @param \TechDivision\Import\ApplicationInterface $application The application instance
  53. */
  54. public function __construct(ApplicationInterface $application)
  55. {
  56. $this->application = $application;
  57. }
  58. /**
  59. * Set's the plugin configuration instance.
  60. *
  61. * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
  62. *
  63. * @return void
  64. */
  65. public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
  66. {
  67. $this->pluginConfiguration = $pluginConfiguration;
  68. }
  69. /**
  70. * Return's the plugin configuration instance.
  71. *
  72. * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
  73. */
  74. public function getPluginConfiguration()
  75. {
  76. return $this->pluginConfiguration;
  77. }
  78. /**
  79. * Return's the unique serial for this import process.
  80. *
  81. * @return string The unique serial
  82. */
  83. public function getSerial()
  84. {
  85. return $this->getApplication()->getSerial();
  86. }
  87. /**
  88. * Set's the import adapter instance.
  89. *
  90. * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance
  91. *
  92. * @return void
  93. */
  94. public function setImportAdapter(ImportAdapterInterface $importAdapter)
  95. {
  96. $this->importAdapter = $importAdapter;
  97. }
  98. /**
  99. * Return's the import adapter instance.
  100. *
  101. * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance
  102. */
  103. public function getImportAdapter()
  104. {
  105. return $this->importAdapter;
  106. }
  107. /**
  108. * Return's the plugin's execution context configuration.
  109. *
  110. * @return \TechDivision\Import\Configuration\ExecutionContextInterface The execution context configuration to use
  111. */
  112. public function getExecutionContext()
  113. {
  114. return $this->getPluginConfiguration()->getExecutionContext();
  115. }
  116. /**
  117. * Return's the target directory for the artefact export.
  118. *
  119. * @return string The target directory for the artefact export
  120. */
  121. public function getTargetDir()
  122. {
  123. // load the status from the registry processor
  124. $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
  125. // query whether or not a target directory (mandatory) has been configured
  126. if (isset($status[RegistryKeys::TARGET_DIRECTORY])) {
  127. return $status[RegistryKeys::TARGET_DIRECTORY];
  128. }
  129. // throw an exception if the root category is NOT available
  130. throw new \Exception(sprintf('Can\'t find a target directory in status data for import %s', $this->getSerial()));
  131. }
  132. /**
  133. * Return's the application instance.
  134. *
  135. * @return \TechDivision\Import\ApplicationInterface The application instance
  136. */
  137. public function getApplication()
  138. {
  139. return $this->application;
  140. }
  141. /**
  142. * Return's the RegistryProcessor instance to handle the running threads.
  143. *
  144. * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
  145. */
  146. protected function getRegistryProcessor()
  147. {
  148. return $this->getApplication()->getRegistryProcessor();
  149. }
  150. /**
  151. * Return's the import processor instance.
  152. *
  153. * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
  154. */
  155. protected function getImportProcessor()
  156. {
  157. return $this->getApplication()->getImportProcessor();
  158. }
  159. /**
  160. * Return's the logger with the passed name, by default the system logger.
  161. *
  162. * @param string $name The name of the requested system logger
  163. *
  164. * @return \Psr\Log\LoggerInterface The logger instance
  165. * @throws \Exception Is thrown, if the requested logger is NOT available
  166. */
  167. protected function getSystemLogger($name = LoggerKeys::SYSTEM)
  168. {
  169. return $this->getApplication()->getSystemLogger($name);
  170. }
  171. /**
  172. * Query whether or not the system logger with the passed name is available.
  173. *
  174. * @param string $name The name of the requested system logger
  175. *
  176. * @return boolean TRUE if the logger with the passed name exists, else FALSE
  177. */
  178. protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
  179. {
  180. return $this->getApplication()->hasSystemLogger($name);
  181. }
  182. /**
  183. * Return's the array with the system logger instances.
  184. *
  185. * @return array The logger instance
  186. */
  187. protected function getSystemLoggers()
  188. {
  189. return $this->getApplication()->getSystemLoggers();
  190. }
  191. /**
  192. * Remove's the passed line from the file with the passed name.
  193. *
  194. * @param string $line The line to be removed
  195. * @param string $filename The name of the file the line has to be removed
  196. *
  197. * @return void
  198. * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
  199. */
  200. protected function removeLineFromFile($line, $filename)
  201. {
  202. $this->getApplication()->removeLineFromFile($line, $filename);
  203. }
  204. /**
  205. * Return's the system configuration.
  206. *
  207. * @return \TechDivision\Import\Configuration\ConfigurationInterface The system configuration
  208. */
  209. protected function getConfiguration()
  210. {
  211. return $this->getApplication()->getConfiguration();
  212. }
  213. /**
  214. * Return's the PID filename to use.
  215. *
  216. * @return string The PID filename
  217. */
  218. protected function getPidFilename()
  219. {
  220. return $this->getConfiguration()->getPidFilename();
  221. }
  222. /**
  223. * Return's the source directory that has to be watched for new files.
  224. *
  225. * @return string The source directory
  226. */
  227. protected function getSourceDir()
  228. {
  229. return $this->getConfiguration()->getSourceDir();
  230. }
  231. /**
  232. * Removes the passed directory recursively.
  233. *
  234. * @param string $src Name of the directory to remove
  235. *
  236. * @return void
  237. * @throws \Exception Is thrown, if the directory can not be removed
  238. */
  239. protected function removeDir($src)
  240. {
  241. // open the directory
  242. $dir = opendir($src);
  243. // remove files/folders recursively
  244. while (false !== ($file = readdir($dir))) {
  245. if (($file !== '.') && ($file !== '..')) {
  246. $full = $src . '/' . $file;
  247. if (is_dir($full)) {
  248. $this->removeDir($full);
  249. } else {
  250. if (!unlink($full)) {
  251. throw new \Exception(sprintf('Can\'t remove file %s', $full));
  252. }
  253. }
  254. }
  255. }
  256. // close handle and remove directory itself
  257. closedir($dir);
  258. if (!rmdir($src)) {
  259. throw new \Exception(sprintf('Can\'t remove directory %s', $src));
  260. }
  261. }
  262. /**
  263. * Return's the configured swift mailer instance.
  264. *
  265. * @return \Swift_Mailer|null The mailer instance
  266. */
  267. protected function getSwiftMailer()
  268. {
  269. // the swift mailer configuration
  270. if ($swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer()) {
  271. // create the swift mailer (factory) instance
  272. $possibleSwiftMailer = $this->getApplication()->getContainer()->get($swiftMailerConfiguration->getId());
  273. // query whether or not we've a factory or the instance
  274. /** @var \Swift_Mailer $swiftMailer */
  275. if ($possibleSwiftMailer instanceof TransportMailerFactoryInterface) {
  276. return $possibleSwiftMailer->factory($swiftMailerConfiguration->getTransport());
  277. }
  278. if ($possibleSwiftMailer instanceof \Swift_Mailer) {
  279. return $possibleSwiftMailer;
  280. }
  281. }
  282. // throw an exception if the configuration contains an invalid value
  283. throw new \Exception('Can\'t create SwiftMailer from configuration');
  284. }
  285. }