Passed
Push — master ( b9d710...1e0284 )
by
unknown
16:15
created

RemoteRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Extensionmanager\Remote;
19
20
/**
21
 * Registry of remote connectors.
22
 *
23
 * @internal This class is a specific implementation and is not part of the Public TYPO3 API.
24
 */
25
class RemoteRegistry
26
{
27
    /**
28
     * @var ExtensionDownloaderRemoteInterface[]
29
     */
30
    protected $remotes = [];
31
32
    /**
33
     * @var ExtensionDownloaderRemoteInterface
34
     */
35
    protected $defaultRemote;
36
37
    public function registerRemote(ExtensionDownloaderRemoteInterface $remote, array $configuration): void
38
    {
39
        $identifier = $remote->getIdentifier();
40
41
        $this->remotes[$identifier] = array_merge(
42
            $configuration,
43
            [
44
                'service' => $remote,
45
                'listable' => $remote instanceof ListableRemoteInterface
46
            ]
47
        );
48
49
        if ($this->remotes[$identifier]['default']
50
            && $this->remotes[$identifier]['enabled']
51
            && $this->remotes[$identifier]['listable']
52
        ) {
53
            // This overrides a previously set default remote
54
            $this->defaultRemote = $remote;
55
        }
56
    }
57
58
    public function hasRemote(string $identifier): bool
59
    {
60
        return isset($this->remotes[$identifier]) && $this->remotes[$identifier]['enabled'];
61
    }
62
63
    public function getRemote(string $identifier): ExtensionDownloaderRemoteInterface
64
    {
65
        if (isset($this->remotes[$identifier]) && $this->remotes[$identifier]['enabled']) {
66
            return $this->remotes[$identifier]['service'];
67
        }
68
        throw new RemoteNotRegisteredException(
69
            'The requested remote ' . $identifier . ' is not registered in this system or not enabled',
70
            1601566451
71
        );
72
    }
73
74
    public function hasDefaultRemote(): bool
75
    {
76
        return isset($this->defaultRemote);
77
    }
78
79
    public function getDefaultRemote(): ExtensionDownloaderRemoteInterface
80
    {
81
        if ($this->defaultRemote !== null) {
82
            return $this->defaultRemote;
83
        }
84
        throw new RemoteNotRegisteredException('No default remote registered in this system', 1602226715);
85
    }
86
87
    /**
88
     * @return ListableRemoteInterface[]
89
     */
90
    public function getListableRemotes(): iterable
91
    {
92
        foreach ($this->remotes as $remote) {
93
            if ($remote['listable'] && $remote['enabled']) {
94
                yield $remote['service'];
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $remote['service'] returns the type Generator which is incompatible with the documented return type TYPO3\CMS\Extensionmanag...stableRemoteInterface[].
Loading history...
95
            }
96
        }
97
    }
98
99
    /**
100
     * @return ExtensionDownloaderRemoteInterface[]
101
     */
102
    public function getAllRemotes(): iterable
103
    {
104
        foreach ($this->remotes as $remote) {
105
            if ($remote['enabled']) {
106
                yield $remote['service'];
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $remote['service'] returns the type Generator which is incompatible with the documented return type TYPO3\CMS\Extensionmanag...loaderRemoteInterface[].
Loading history...
107
            }
108
        }
109
    }
110
}
111