Completed
Push — master ( 031ec6...098ccd )
by
unknown
15:29
created

DownloadQueue   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtensionQueue() 0 3 1
A addExtensionToQueue() 0 18 6
A removeExtensionFromQueue() 0 8 6
A isQueueEmpty() 0 3 1
A addExtensionToInstallQueue() 0 3 1
A resetExtensionQueue() 0 12 3
A getExtensionInstallStorage() 0 3 1
A resetExtensionInstallStorage() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Extensionmanager\Domain\Model;
17
18
use TYPO3\CMS\Core\SingletonInterface;
19
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
20
21
/**
22
 * Download Queue - storage for extensions to be downloaded
23
 * @internal This class is a specific domain model implementation and is not part of the Public TYPO3 API.
24
 */
25
class DownloadQueue implements SingletonInterface
26
{
27
    /**
28
     * Storage for extensions to be downloaded
29
     *
30
     * @var array<string, array<string>>
31
     */
32
    protected $extensionStorage = [];
33
34
    /**
35
     * Storage for extensions to be installed
36
     *
37
     * @var array
38
     */
39
    protected $extensionInstallStorage = [];
40
41
    /**
42
     * Adds an extension to the download queue.
43
     * If the extension was already requested in a different version
44
     * an exception is thrown.
45
     *
46
     * @param Extension $extension
47
     * @param string $stack
48
     * @throws ExtensionManagerException
49
     */
50
    public function addExtensionToQueue(Extension $extension, $stack = 'download')
51
    {
52
        if (!is_string($stack) || !in_array($stack, ['download', 'update'])) {
0 ignored issues
show
introduced by
The condition is_string($stack) is always true.
Loading history...
53
            throw new ExtensionManagerException('Stack has to be either "download" or "update"', 1342432103);
54
        }
55
        if (!isset($this->extensionStorage[$stack])) {
56
            $this->extensionStorage[$stack] = [];
57
        }
58
        if (array_key_exists($extension->getExtensionKey(), $this->extensionStorage[$stack])) {
59
            if ($this->extensionStorage[$stack][$extension->getExtensionKey()] !== $extension) {
0 ignored issues
show
introduced by
The condition $this->extensionStorage[...onKey()] !== $extension is always true.
Loading history...
60
                throw new ExtensionManagerException(
61
                    $extension->getExtensionKey() . ' was requested to be downloaded in different versions (' . $extension->getVersion()
62
                        . ' and ' . $this->extensionStorage[$stack][$extension->getExtensionKey()]->getVersion() . ').',
63
                    1342432101
64
                );
65
            }
66
        }
67
        $this->extensionStorage[$stack][$extension->getExtensionKey()] = $extension;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getExtensionQueue()
74
    {
75
        return $this->extensionStorage;
76
    }
77
78
    /**
79
     * Remove an extension from download queue
80
     *
81
     * @param Extension $extension
82
     * @param string $stack Stack to remove extension from (download, update or install)
83
     * @throws ExtensionManagerException
84
     */
85
    public function removeExtensionFromQueue(Extension $extension, $stack = 'download')
86
    {
87
        if (!is_string($stack) || !in_array($stack, ['download', 'update'])) {
0 ignored issues
show
introduced by
The condition is_string($stack) is always true.
Loading history...
88
            throw new ExtensionManagerException('Stack has to be either "download" or "update"', 1342432104);
89
        }
90
        if (array_key_exists($stack, $this->extensionStorage) && is_array($this->extensionStorage[$stack])) {
91
            if (array_key_exists($extension->getExtensionKey(), $this->extensionStorage[$stack])) {
92
                unset($this->extensionStorage[$stack][$extension->getExtensionKey()]);
93
            }
94
        }
95
    }
96
97
    /**
98
     * Adds an extension to the install queue for later installation
99
     *
100
     * @param Extension $extension
101
     */
102
    public function addExtensionToInstallQueue($extension)
103
    {
104
        $this->extensionInstallStorage[$extension->getExtensionKey()] = $extension;
105
    }
106
107
    /**
108
     * Gets the extension installation queue
109
     *
110
     * @return array
111
     */
112
    public function getExtensionInstallStorage()
113
    {
114
        return $this->extensionInstallStorage;
115
    }
116
117
    /**
118
     * Return whether the queue contains extensions or not
119
     *
120
     * @param string $stack either "download" or "update"
121
     * @return bool
122
     */
123
    public function isQueueEmpty($stack)
124
    {
125
        return empty($this->extensionStorage[$stack]);
126
    }
127
128
    /**
129
     * Resets the extension queue and returns old extensions
130
     *
131
     * @param string|null $stack if null, all stacks are reset
132
     * @return array
133
     */
134
    public function resetExtensionQueue($stack = null)
135
    {
136
        $storage = [];
137
        if ($stack === null) {
138
            $storage = $this->extensionStorage;
139
            $this->extensionStorage = [];
140
        } elseif (isset($this->extensionStorage[$stack])) {
141
            $storage = $this->extensionStorage[$stack];
142
            $this->extensionStorage[$stack] = [];
143
        }
144
145
        return $storage;
146
    }
147
148
    /**
149
     * Resets the install queue and returns the old extensions
150
     * @return array
151
     */
152
    public function resetExtensionInstallStorage()
153
    {
154
        $storage = $this->extensionInstallStorage;
155
        $this->extensionInstallStorage = [];
156
157
        return $storage;
158
    }
159
}
160