Connector.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Framework\Exception\NotFoundException;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * A connector to external services.
  11. *
  12. * Aggregates and executes commands which perform requests to external services.
  13. */
  14. class Connector
  15. {
  16. /**
  17. * A list of possible commands.
  18. *
  19. * An associative array in format: 'command_name' => 'command_class_name'.
  20. *
  21. * The list may be configured in each module via '/etc/di.xml'.
  22. *
  23. * @var string[]
  24. */
  25. private $commands;
  26. /**
  27. * @var ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * @param array $commands
  32. * @param ObjectManagerInterface $objectManager
  33. */
  34. public function __construct(
  35. array $commands,
  36. ObjectManagerInterface $objectManager
  37. ) {
  38. $this->commands = $commands;
  39. $this->objectManager = $objectManager;
  40. }
  41. /**
  42. * Executes a command in accordance with the given name.
  43. *
  44. * @param string $commandName
  45. * @return bool
  46. * @throws NotFoundException if the command is not found.
  47. */
  48. public function execute($commandName)
  49. {
  50. if (!array_key_exists($commandName, $this->commands)) {
  51. throw new NotFoundException(__('Command was not found.'));
  52. }
  53. /** @var \Magento\Analytics\Model\Connector\CommandInterface $command */
  54. $command = $this->objectManager->create($this->commands[$commandName]);
  55. return $command->execute();
  56. }
  57. }