1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 5.4 and 7 |
4
|
|
|
* |
5
|
|
|
* @package Payever\Plugins |
6
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
7
|
|
|
* @copyright 2017-2019 payever GmbH |
8
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Payever\ExternalIntegration\Plugins\Command; |
12
|
|
|
|
13
|
|
|
use Payever\ExternalIntegration\Plugins\Base\PluginRegistryInfoProviderInterface; |
14
|
|
|
use Payever\ExternalIntegration\Plugins\Base\PluginsApiClientInterface; |
15
|
|
|
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\CommandsResponseEntity; |
16
|
|
|
|
17
|
|
|
class PluginCommandManager |
18
|
|
|
{ |
19
|
|
|
/** @var int|null */ |
20
|
|
|
private $lastCommandTimestamp; |
21
|
|
|
|
22
|
|
|
/** @var PluginsApiClientInterface */ |
23
|
|
|
private $pluginsApiClient; |
24
|
|
|
|
25
|
|
|
/** @var PluginCommandExecutorInterface */ |
26
|
|
|
private $commandExecutor; |
27
|
|
|
|
28
|
|
|
/** @var PluginRegistryInfoProviderInterface */ |
29
|
|
|
private $registryInfoProvider; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
PluginsApiClientInterface $pluginsApiClient, |
33
|
|
|
PluginCommandExecutorInterface $commandExecutor, |
34
|
|
|
PluginRegistryInfoProviderInterface $registryInfoProvider, |
35
|
|
|
$lastCommandTimestamp = null |
36
|
|
|
) { |
37
|
|
|
$this->pluginsApiClient = $pluginsApiClient; |
38
|
|
|
$this->commandExecutor = $commandExecutor; |
39
|
|
|
$this->registryInfoProvider = $registryInfoProvider; |
40
|
|
|
$this->lastCommandTimestamp = $lastCommandTimestamp; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets up to date commands and executes them |
45
|
|
|
* |
46
|
|
|
* @throws \Exception - bubbles up anything thrown by API or CommandExecutor |
47
|
|
|
*/ |
48
|
|
|
public function executePluginCommands() |
49
|
|
|
{ |
50
|
|
|
$commandsResponse = $this->pluginsApiClient->getCommands($this->lastCommandTimestamp); |
51
|
|
|
/** @var CommandsResponseEntity $commandsResponseEntity */ |
52
|
|
|
$commandsResponseEntity = $commandsResponse->getResponseEntity(); |
53
|
|
|
$commandsList = $commandsResponseEntity->getCommands(); |
54
|
|
|
|
55
|
|
|
foreach ($commandsList as $commandEntity) { |
56
|
|
|
if ($this->isCommandSupported($commandEntity->getName())) { |
57
|
|
|
$this->commandExecutor->executeCommand($commandEntity->getName(), $commandEntity->getValue()); |
58
|
|
|
$this->pluginsApiClient->acknowledgePluginCommand($commandEntity->getId()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $commandName |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
private function isCommandSupported($commandName) |
68
|
|
|
{ |
69
|
|
|
return in_array($commandName, $this->registryInfoProvider->getSupportedCommands()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|