UpdatablePluginsCollector::getResult()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 28
rs 9.6666
1
<?php
2
3
namespace Koality\ShopwarePlugin\Collector;
4
5
use Koality\ShopwarePlugin\Formatter\Result;
6
use Shopware\Core\Framework\Api\Context\SystemSource;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\Plugin;
11
use Shopware\Core\Framework\Store\Services\StoreClient;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
15
/**
16
 * Class UpdateablePluginsCollector
17
 *
18
 * @package Koality\ShopwarePlugin\Collector
19
 *
20
 * @author Nils Langner <[email protected]>
21
 * created 2020-12-29
22
 */
23
class UpdatablePluginsCollector implements Collector
24
{
25
    /**
26
     * @var array
27
     */
28
    private $pluginConfig = [];
29
30
    /**
31
     * @var EntityRepositoryInterface
32
     */
33
    private $pluginRepository;
34
35
    /**
36
     * @var Context
37
     */
38
    private $context;
39
40
    /**
41
     * @var StoreClient
42
     */
43
    private $storeClient;
44
45
    /**
46
     * @var Request
47
     */
48
    private $request;
49
50
    /**
51
     * CountOrdersCollector constructor.
52
     *
53
     * @param array $pluginConfig
54
     * @param EntityRepositoryInterface $pluginRepository
55
     * @param Context $context
56
     * @param StoreClient $storeClient
57
     * @param Request $request
58
     */
59
    public function __construct(array $pluginConfig, EntityRepositoryInterface $pluginRepository, Context $context, StoreClient $storeClient, Request $request)
60
    {
61
        $this->pluginConfig = $pluginConfig;
62
        $this->pluginRepository = $pluginRepository;
63
        $this->context = $context;
64
        $this->storeClient = $storeClient;
65
        $this->request = $request;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function getResult(): Result
72
    {
73
        $updatablePlugins = $this->getUpdatablePlugins();
74
75
        $updatablePluginsCount = count($updatablePlugins);
76
77
        if (array_key_exists('pluginsUpdatable', $this->pluginConfig)) {
78
            $maxUpdatablePluginsCount = $this->pluginConfig['pluginsUpdatable'];
79
        } else {
80
            $maxUpdatablePluginsCount = 0;
81
        }
82
83
        if ($maxUpdatablePluginsCount < $updatablePluginsCount) {
84
            $pluginResult = new Result(Result::STATUS_FAIL, Result::KEY_PLUGINS_UPDATABLE, 'Too many plugins need to be updated.');
85
        } else {
86
            $pluginResult = new Result(Result::STATUS_PASS, Result::KEY_PLUGINS_UPDATABLE, 'Not too many plugins need to be updated.');
87
        }
88
89
        $pluginResult->addAttribute('plugins', $updatablePlugins);
90
91
        $pluginResult->setLimit($maxUpdatablePluginsCount);
92
        $pluginResult->setObservedValue($updatablePluginsCount);
93
        $pluginResult->setObservedValueUnit('plugins');
94
        $pluginResult->setLimitType(Result::LIMIT_TYPE_MAX);
95
        $pluginResult->setType(Result::TYPE_TIME_SERIES_NUMERIC);
96
        $pluginResult->setObservedValuePrecision(0);
97
98
        return $pluginResult;
99
    }
100
101
    /**
102
     * Return a list of plugins that can be updated.
103
     *
104
     * @return array
105
     */
106
    private function getUpdatablePlugins()
107
    {
108
        /** @var Plugin[] $plugins */
109
        $plugins = $this->pluginRepository->search(new Criteria(), $this->context);
110
        $pluginCollection = new Plugin\PluginCollection($plugins);
111
112
        $updateList = $this->storeClient->getUpdatesList(
113
            $pluginCollection,
114
            $this->request->getHost(),
115
            new Context(new SystemSource())
116
        );
117
118
        $updatablePlugins = [];
119
120
        foreach ($updateList as $updateElement) {
121
            $pluginVars = $updateElement->getVars();
122
            $updatablePlugins[] = [
123
                'name' => $pluginVars['name'],
124
                'label' => $pluginVars['label']
125
            ];
126
        }
127
128
        return $updatablePlugins;
129
    }
130
}
131