Passed
Push — master ( a8ff8f...cbaed7 )
by payever
02:47
created

PluginCommandManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 1
b 0
f 0
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A executePluginCommands() 0 11 3
A isCommandSupported() 0 3 1
A __construct() 0 10 1
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