RepositoryRegistry::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * Copyright (C) 2017 by TEQneers GmbH & Co. KG
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
/**
25
 * Git Stream Wrapper for PHP
26
 *
27
 * @category   TQ
28
 * @package    TQ_VCS
29
 * @subpackage VCS
30
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
31
 */
32
33
namespace TQ\Vcs\StreamWrapper;
34
use TQ\Vcs\Repository\RepositoryInterface;
35
36
/**
37
 * Manages multiples repositories by keys
38
 *
39
 * @author     Stefan Gehrig <gehrigteqneers.de>
40
 * @category   TQ
41
 * @package    TQ_VCS
42
 * @subpackage VCS
43
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
44
 */
45
class RepositoryRegistry implements \Countable
46
{
47
    /**
48
     * The repository map
49
     *
50
     * @var array
51
     */
52
    protected $map  = array();
53
54
    /**
55
     * Adds a single repository
56
     *
57
     * @param   string               $key        The key
58
     * @param   RepositoryInterface           $repository The repository
59
     * @return  RepositoryRegistry
60
     */
61 14
    public function addRepository($key, RepositoryInterface $repository)
62
    {
63 14
        $this->map[$key]    = $repository;
64 14
        return $this;
65
    }
66
67
    /**
68
     * Adds multiple repositories
69
     *
70
     * @param   array      $repositories    The repositories (key => repository)
71
     * @return  RepositoryRegistry
72
     */
73 13
    public function addRepositories(array $repositories)
74
    {
75 13
        foreach ($repositories as $key => $repository) {
76 13
            $this->addRepository($key, $repository);
77
        }
78 13
        return $this;
79
    }
80
81
    /**
82
     * Returns true if the repository is registered in the map
83
     *
84
     * @param   string      $key        The key
85
     * @return  boolean
86
     */
87 12
    public function hasRepository($key)
88
    {
89 12
        return isset($this->map[$key]);
90
    }
91
92
    /**
93
     * Returns the repository if it is registered in the map, throws exception otherwise
94
     *
95
     * @param   string      $key        The key
96
     * @return  RepositoryInterface
97
     * @throws  \OutOfBoundsException   If the key does not exist
98
     */
99 11
    public function getRepository($key)
100
    {
101 11
        $repository = $this->tryGetRepository($key);
102 11
        if ($repository === null) {
103 1
            throw new \OutOfBoundsException($key.' does not exist in the registry');
104
        }
105 10
        return $repository;
106
    }
107
108
    /**
109
     * Returns the repository if it is registered in the map, NULL otherwise
110
     *
111
     * @param   string      $key        The key
112
     * @return  RepositoryInterface|null
113
     */
114 12
    public function tryGetRepository($key)
115
    {
116 12
        if ($this->hasRepository($key)) {
117 10
            return $this->map[$key];
118
        } else {
119 2
            return null;
120
        }
121
122
    }
123
124
    /**
125
     * Count elements of an object
126
     *
127
     * @link    http://php.net/manual/en/countable.count.php
128
     * @return  integer     The custom count as an integer
129
     */
130 2
    public function count()
131
    {
132 2
        return count($this->map);
133
    }
134
}
135