Failed Conditions
Push — master ( ebb1ea...d49c03 )
by Bernhard
06:05
created

PathMappingCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.5%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 16
c 5
b 2
f 0
lcom 1
cbo 3
dl 0
loc 186
ccs 31
cts 40
cp 0.775
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A remove() 0 4 1
A get() 0 4 1
A contains() 0 4 1
A listByRepositoryPath() 0 4 1
A getPackageNames() 0 4 1
A add() 0 5 1
A set() 0 5 1
A listByPackageName() 0 8 2
A getRepositoryPaths() 0 8 2
A toArray() 0 8 2
A isEmpty() 0 4 1
A lazySortPrimaryKeys() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[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 Puli\Manager\Repository\Mapping;
13
14
use OutOfBoundsException;
15
use Puli\Manager\Api\Repository\PathMapping;
16
use Puli\Manager\Util\TwoDimensionalHashMap;
17
18
/**
19
 * A collection of path mappings.
20
 *
21
 * Each mapping has a composite key:
22
 *
23
 *  * The repository path of the mapping.
24
 *  * The package that defines the mapping.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class PathMappingCollection
31
{
32
    /**
33
     * @var TwoDimensionalHashMap
34
     */
35
    private $map;
36
37
    /**
38
     * @var bool
39
     */
40
    private $primaryKeysSorted = false;
41
42
    /**
43
     * Creates the store.
44
     */
45 63
    public function __construct()
46
    {
47 63
        $this->map = new TwoDimensionalHashMap();
48 63
    }
49
50
    /**
51
     * Adds a path mapping.
52
     *
53
     * @param PathMapping $mapping The path mapping.
54
     */
55 57
    public function add(PathMapping $mapping)
56
    {
57 57
        $this->map->set($mapping->getRepositoryPath(), $mapping->getContainingPackage()->getName(), $mapping);
58 57
        $this->primaryKeysSorted = false;
59 57
    }
60
61
    /**
62
     * Sets a path mapping for a specific repository path.
63
     *
64
     * @param string      $repositoryPath The repository path.
65
     * @param PathMapping $mapping        The path mapping.
66
     */
67 54
    public function set($repositoryPath, PathMapping $mapping)
68
    {
69 54
        $this->map->set($repositoryPath, $mapping->getContainingPackage()->getName(), $mapping);
70 54
        $this->primaryKeysSorted = false;
71 54
    }
72
73
    /**
74
     * Removes a path mapping.
75
     *
76
     * This method ignores non-existing path mappings.
77
     *
78
     * @param string $repositoryPath The repository path of the mapping.
79
     * @param string $packageName    The package containing the mapping.
80
     */
81 15
    public function remove($repositoryPath, $packageName)
82
    {
83 15
        $this->map->remove($repositoryPath, $packageName);
84 15
    }
85
86
    /**
87
     * Returns a path mapping.
88
     *
89
     * @param string $repositoryPath The repository path of the mapping.
90
     * @param string $packageName    The package containing the mapping.
91
     *
92
     * @return PathMapping The path mapping.
93
     *
94
     * @throws OutOfBoundsException If no path mapping was set for the
95
     *                              given repository path/package.
96
     */
97 27
    public function get($repositoryPath, $packageName)
98
    {
99 27
        return $this->map->get($repositoryPath, $packageName);
100
    }
101
102
    /**
103
     * Returns whether a path mapping was set for the given repository
104
     * path/package.
105
     *
106
     * @param string      $repositoryPath The repository path of the mapping.
107
     * @param string|null $packageName    The package containing the mapping.
108
     *
109
     * @return bool Returns `true` if a path mapping was set for the given
110
     *              repository path/package.
111
     */
112 19
    public function contains($repositoryPath, $packageName = null)
113
    {
114 19
        return $this->map->contains($repositoryPath, $packageName);
115
    }
116
117
    /**
118
     * Returns all path mappings set for the given repository path.
119
     *
120
     * @param string $repositoryPath The repository path of the mapping.
121
     *
122
     * @return PathMapping[] The path mappings.
123
     *
124
     * @throws OutOfBoundsException If no path mapping was set for the
125
     *                              given repository path.
126
     */
127
    public function listByRepositoryPath($repositoryPath)
128
    {
129
        return $this->map->listByPrimaryKey($repositoryPath);
130
    }
131
132
    /**
133
     * Returns all path mappings set for the given package name.
134
     *
135
     * @param string $packageName The package name.
136
     *
137
     * @return PathMapping[] The path mappings.
138
     *
139
     * @throws OutOfBoundsException If no path mapping was set for the
140
     *                              given package name.
141
     */
142 13
    public function listByPackageName($packageName)
143
    {
144 13
        if ($this->primaryKeysSorted) {
145
            $this->lazySortPrimaryKeys();
146
        }
147
148 13
        return $this->map->listBySecondaryKey($packageName);
149
    }
150
151
    /**
152
     * Returns the names of the packages defining mappings with the given
153
     * repository path.
154
     *
155
     * @param string|null $repositoryPath The repository path of the mapping.
156
     *
157
     * @return string[] The package names.
0 ignored issues
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
     *
159
     * @throws OutOfBoundsException If no path mapping was set for the
160
     *                              given repository path.
161
     */
162 14
    public function getPackageNames($repositoryPath = null)
163
    {
164 14
        return $this->map->getSecondaryKeys($repositoryPath);
165
    }
166
167
    /**
168
     * Returns the repository paths of all path mappings.
169
     *
170
     * @return string[] The repository paths of the stored mappings.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
171
     */
172 63
    public function getRepositoryPaths()
173
    {
174 63
        if ($this->primaryKeysSorted) {
175
            $this->lazySortPrimaryKeys();
176
        }
177
178 63
        return $this->map->getPrimaryKeys();
179
    }
180
181
    /**
182
     * Returns the contents of the collection as array.
183
     *
184
     * @return PathMapping[][] An array containing all path mappings indexed
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
185
     *                         first by repository, then by package name.
186
     */
187 33
    public function toArray()
188
    {
189 33
        if ($this->primaryKeysSorted) {
190
            $this->lazySortPrimaryKeys();
191
        }
192
193 33
        return $this->map->toArray();
194
    }
195
196
    /**
197
     * Returns whether the collection is empty.
198
     *
199
     * @return bool Returns `true` if the collection is empty and `false`
200
     *              otherwise.
201
     */
202 2
    public function isEmpty()
203
    {
204 2
        return $this->map->isEmpty();
205
    }
206
207
    /**
208
     * Sorts the map primary keys, if necessary.
209
     */
210
    private function lazySortPrimaryKeys()
211
    {
212
        $this->map->sortPrimaryKeys();
213
        $this->primaryKeysSorted = true;
214
    }
215
}
216