StorageManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 30
cp 0
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstName() 0 7 2
A addStorage() 0 3 1
A getNames() 0 3 1
A getStorage() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Service;
13
14
/**
15
 * A service to easily access different storage services.
16
 *
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
final class StorageManager
20
{
21
    /**
22
     * @var StorageService[]
23
     */
24
    private $storages = [];
25
26
    public function addStorage(string $name, StorageService $storage): void
27
    {
28
        $this->storages[$name] = $storage;
29
    }
30
31
    /**
32
     * @param string|string[]|null $name
33
     */
34
    public function getStorage($name = null): ?StorageService
35
    {
36
        if (empty($name)) {
37
            return $this->getStorage('default');
38
        }
39
40
        if (isset($this->storages[$name])) {
41
            return $this->storages[$name];
42
        }
43
44
        if ('default' === $name) {
45
            $name = $this->getFirstName();
46
            if (isset($this->storages[$name])) {
47
                return $this->storages[$name];
48
            }
49
        }
50
51
        return null;
52
    }
53
54
    public function getFirstName(): ?string
55
    {
56
        foreach ($this->storages as $name => $config) {
57
            return $name;
58
        }
59
60
        return null;
61
    }
62
63
    public function getNames(): array
64
    {
65
        return \array_keys($this->storages);
66
    }
67
}
68