Passed
Push — master ( e4ea65...5bc50c )
by Christian
15:36 queued 10s
created

ExtensionListingLoader::addStoreInformation()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 39
rs 8.6186
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Framework\Store\Services;
4
5
use Shopware\Core\Framework\Context;
6
use Shopware\Core\Framework\Store\Exception\StoreApiException;
7
use Shopware\Core\Framework\Store\Struct\ExtensionCollection;
8
use Shopware\Core\Framework\Store\Struct\ExtensionStruct;
9
10
/**
11
 * @internal
12
 */
13
class ExtensionListingLoader
14
{
15
    /**
16
     * @var StoreClient
17
     */
18
    private $client;
19
20
    public function __construct(StoreClient $client)
21
    {
22
        $this->client = $client;
23
    }
24
25
    public function load(ExtensionCollection $localCollection, Context $context): ExtensionCollection
26
    {
27
        $this->addStoreInformation($localCollection, $context);
28
29
        return $this->sortCollection($localCollection);
30
    }
31
32
    private function addStoreInformation(ExtensionCollection $localCollection, Context $context): void
33
    {
34
        try {
35
            $storeExtensions = $this->client->listMyExtensions($localCollection, $context);
36
        } catch (StoreApiException $e) {
37
            return;
38
        }
39
40
        foreach ($storeExtensions->getElements() as $storeExtension) {
41
            if ($localCollection->has($storeExtension->getName())) {
42
                /** @var ExtensionStruct $localExtension */
43
                $localExtension = $localCollection->get($storeExtension->getName());
44
                $localExtension->setId($storeExtension->getId());
45
                $localExtension->setIsTheme($storeExtension->isTheme());
46
                $localExtension->setStoreExtension($storeExtension);
47
48
                $localExtension->setStoreLicense($storeExtension->getStoreLicense());
49
                $localExtension->setNotices($storeExtension->getNotices());
50
51
                if ($storeExtension->getDescription()) {
52
                    $localExtension->setDescription($storeExtension->getDescription());
53
                }
54
55
                if ($storeExtension->getShortDescription()) {
56
                    $localExtension->setShortDescription($storeExtension->getShortDescription());
57
                }
58
59
                $localExtension->setIcon($storeExtension->getIcon());
60
                $localExtension->setLabel($storeExtension->getLabel());
61
62
                if ($storeExtension->getLatestVersion()) {
63
                    $localExtension->setLatestVersion($storeExtension->getLatestVersion());
64
                    $localExtension->setUpdateSource($storeExtension->getUpdateSource());
65
                }
66
67
                continue;
68
            }
69
70
            $localCollection->set($storeExtension->getName(), $storeExtension);
71
        }
72
    }
73
74
    private function sortCollection(ExtensionCollection $collection): ExtensionCollection
75
    {
76
        $collection->sort(function (ExtensionStruct $a, ExtensionStruct $b) {
77
            return strcmp($a->getLabel(), $b->getLabel());
78
        });
79
80
        $sortedCollection = new ExtensionCollection();
81
82
        // Sorted order: active, installed, all others
83
        foreach ($collection->getElements() as $extension) {
84
            if ($extension->getActive()) {
85
                $sortedCollection->set($extension->getName(), $extension);
86
                $collection->remove($extension->getName());
87
            }
88
        }
89
90
        foreach ($collection->getElements() as $extension) {
91
            if ($extension->getInstalledAt()) {
92
                $sortedCollection->set($extension->getName(), $extension);
93
                $collection->remove($extension->getName());
94
            }
95
        }
96
97
        foreach ($collection->getElements() as $extension) {
98
            $sortedCollection->set($extension->getName(), $extension);
99
        }
100
101
        return $sortedCollection;
102
    }
103
}
104