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