Completed
Push — master ( c92ef6...3f980f )
by
unknown
13:33
created

RemoteRegistry   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 101
rs 10
c 1
b 0
f 0
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A registerRemote() 0 27 5
A getDefaultRemote() 0 6 2
A getRemote() 0 8 3
A hasRemote() 0 3 2
A hasDefaultRemote() 0 3 1
A __construct() 0 3 1
A getListableRemotes() 0 5 4
A getAllRemotes() 0 5 3
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
use Psr\Container\ContainerInterface;
21
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
22
23
/**
24
 * Registry of remote connectors.
25
 *
26
 * @internal This class is a specific implementation and is not part of the Public TYPO3 API.
27
 */
28
class RemoteRegistry
29
{
30
    /**
31
     * @var ContainerInterface
32
     */
33
    protected $container;
34
35
    /**
36
     * @var ExtensionDownloaderRemoteInterface[]
37
     */
38
    protected $remotes = [];
39
40
    /**
41
     * @var ExtensionDownloaderRemoteInterface
42
     */
43
    protected $defaultRemote;
44
45
    public function __construct(ContainerInterface $container)
46
    {
47
        $this->container = $container;
48
    }
49
50
    public function registerRemote(string $id, array $configuration): void
51
    {
52
        $remote = $this->container->get($id);
53
54
        if (!$remote instanceof ExtensionDownloaderRemoteInterface) {
55
            throw new ExtensionManagerException(
56
                'Extension remote ' . $id . ' must implement ' . ExtensionDownloaderRemoteInterface::class,
57
                1602173538
58
            );
59
        }
60
61
        $identifier = $remote->getIdentifier();
62
63
        $this->remotes[$identifier] = array_merge(
64
            $configuration,
65
            [
66
                'service' => $remote,
67
                'listable' => $remote instanceof ListableRemoteInterface
68
            ]
69
        );
70
71
        if ($this->remotes[$identifier]['default']
72
            && $this->remotes[$identifier]['enabled']
73
            && $this->remotes[$identifier]['listable']
74
        ) {
75
            // This overrides a previously set default remote
76
            $this->defaultRemote = $remote;
77
        }
78
    }
79
80
    public function hasRemote(string $identifier): bool
81
    {
82
        return isset($this->remotes[$identifier]) && $this->remotes[$identifier]['enabled'];
83
    }
84
85
    public function getRemote(string $identifier): ExtensionDownloaderRemoteInterface
86
    {
87
        if (isset($this->remotes[$identifier]) && $this->remotes[$identifier]['enabled']) {
88
            return $this->remotes[$identifier]['service'];
89
        }
90
        throw new RemoteNotRegisteredException(
91
            'The requested remote ' . $identifier . ' is not registered in this system or not enabled',
92
            1601566451
93
        );
94
    }
95
96
    public function hasDefaultRemote(): bool
97
    {
98
        return isset($this->defaultRemote);
99
    }
100
101
    public function getDefaultRemote(): ExtensionDownloaderRemoteInterface
102
    {
103
        if ($this->defaultRemote !== null) {
104
            return $this->defaultRemote;
105
        }
106
        throw new RemoteNotRegisteredException('No default remote registered in this system', 1602226715);
107
    }
108
109
    /**
110
     * @return ListableRemoteInterface[]
111
     */
112
    public function getListableRemotes(): iterable
113
    {
114
        foreach ($this->remotes as $remote) {
115
            if ($remote['listable'] && $remote['enabled']) {
116
                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...
117
            }
118
        }
119
    }
120
121
    /**
122
     * @return ExtensionDownloaderRemoteInterface[]
123
     */
124
    public function getAllRemotes(): iterable
125
    {
126
        foreach ($this->remotes as $remote) {
127
            if ($remote['enabled']) {
128
                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...
129
            }
130
        }
131
    }
132
}
133