PluginCommandManager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
c 1
b 0
f 0
dl 0
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A executePluginCommands() 0 24 3
A isCommandSupported() 0 16 4
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Command
7
 * @package   Payever\Plugins
8
 * @author    payever GmbH <[email protected]>
9
 * @author    Hennadii.Shymanskyi <[email protected]>
10
 * @copyright 2017-2021 payever GmbH
11
 * @license   MIT <https://opensource.org/licenses/MIT>
12
 * @link      https://docs.payever.org/shopsystems/api/getting-started
13
 */
14
15
namespace Payever\ExternalIntegration\Plugins\Command;
16
17
use Payever\ExternalIntegration\Plugins\Base\PluginsApiClientInterface;
18
use Payever\ExternalIntegration\Plugins\Enum\PluginCommandNameEnum;
19
use Payever\ExternalIntegration\Plugins\Http\MessageEntity\PluginCommandEntity;
20
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\CommandsResponseEntity;
21
use Psr\Log\LoggerInterface;
22
23
class PluginCommandManager
24
{
25
    /** @var PluginsApiClientInterface */
26
    private $pluginsApiClient;
27
28
    /** @var PluginCommandExecutorInterface */
29
    private $commandExecutor;
30
31
    /** @var LoggerInterface */
32
    private $logger;
33
34
    /**
35
     * @param PluginsApiClientInterface $pluginsApiClient
36
     * @param PluginCommandExecutorInterface $commandExecutor
37
     * @param LoggerInterface $logger
38
     */
39
    public function __construct(
40
        PluginsApiClientInterface $pluginsApiClient,
41
        PluginCommandExecutorInterface $commandExecutor,
42
        LoggerInterface $logger
43
    ) {
44
        $this->pluginsApiClient = $pluginsApiClient;
45
        $this->commandExecutor = $commandExecutor;
46
        $this->logger = $logger;
47
    }
48
49
    /**
50
     * Gets up to date commands and executes them
51
     *
52
     * @param int|null $lastCommandTimestamp
53
     *
54
     * @throws \Exception - bubbles up anything thrown by API or CommandExecutor
55
     * @SuppressWarnings(PHPMD.ElseExpression)
56
     */
57
    public function executePluginCommands($lastCommandTimestamp = null)
58
    {
59
        $commandsResponse = $this->pluginsApiClient->getCommands($lastCommandTimestamp);
60
        /** @var CommandsResponseEntity $responseEntity */
61
        $responseEntity = $commandsResponse->getResponseEntity();
62
        $commandsList = $responseEntity->getCommands();
63
64
        foreach ($commandsList as $commandEntity) {
65
            if ($this->isCommandSupported($commandEntity)) {
66
                $this->logger->info(
67
                    sprintf(
68
                        'Executing plugin command %s with value %s',
69
                        $commandEntity->getName(),
70
                        $commandEntity->getValue()
71
                    )
72
                );
73
                $this->commandExecutor->executeCommand($commandEntity);
74
                $this->pluginsApiClient->acknowledgePluginCommand($commandEntity->getId());
75
            } else {
76
                $this->logger->info(
77
                    sprintf(
78
                        'Plugin command %s with value %s is not supported',
79
                        $commandEntity->getName(),
80
                        $commandEntity->getValue()
81
                    )
82
                );
83
            }
84
        }
85
    }
86
87
    /**
88
     * @param PluginCommandEntity $commandEntity
89
     *
90
     * @return bool
91
     */
92
    private function isCommandSupported(PluginCommandEntity $commandEntity)
93
    {
94
        $infoProvider = $this->pluginsApiClient->getRegistryInfoProvider();
95
96
        if (!in_array($commandEntity->getName(), $infoProvider->getSupportedCommands())) {
97
            return false;
98
        }
99
100
        if (
101
            $commandEntity->getName() === PluginCommandNameEnum::NOTIFY_NEW_PLUGIN_VERSION
102
            && version_compare($commandEntity->getValue(), $infoProvider->getPluginVersion(), '<=')
103
        ) {
104
            return false;
105
        }
106
107
        return true;
108
    }
109
}
110