Completed
Push — master ( 356e08...d08624 )
by Tobias
09:26
created

StorageManager::getFirstName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 6
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
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 View Code Duplication
final class StorageManager
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * @var StorageService[]
23
     */
24
    private $storages = [];
25
26
    /**
27
     * @param string         $name
28
     * @param StorageService $storage
29
     */
30 4
    public function addStorage($name, StorageService $storage)
31
    {
32 4
        $this->storages[$name] = $storage;
33 4
    }
34
35
    /**
36
     * @param string $name
37
     *
38
     * @return null|StorageService
39
     */
40 4
    public function getStorage($name = null)
41
    {
42 4
        if (empty($name)) {
43
            return $this->getStorage('default');
44
        }
45
46 4
        if (isset($this->storages[$name])) {
47 4
            return $this->storages[$name];
48
        }
49
50
        if ('default' === $name) {
51
            $name = $this->getFirstName();
52
            if (isset($this->storages[$name])) {
53
                return $this->storages[$name];
54
            }
55
        }
56
    }
57
58
    /**
59
     * @return string|null
60
     */
61
    public function getFirstName()
62
    {
63
        foreach ($this->storages as $name => $config) {
64
            return $name;
65
        }
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getNames()
72
    {
73
        return array_keys($this->storages);
74
    }
75
}
76