Failed Conditions
Push — 1.0 ( 9f5a0b...fe7a2f )
by Bernhard
30:36 queued 17:00
created

src/Api/ChangeStream/VersionList.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the puli/repository 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\Repository\Api\ChangeStream;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use BadMethodCallException;
17
use Countable;
18
use IteratorAggregate;
19
use OutOfBoundsException;
20
use Puli\Repository\Api\Resource\PuliResource;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * Contains different versions of a resource.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Titouan Galopin <[email protected]>
29
 * @author Bernhard Schussek <[email protected]>
30
 */
31
class VersionList implements IteratorAggregate, ArrayAccess, Countable
32
{
33
    /**
34
     * @var string
35
     */
36
    private $path;
37
38
    /**
39
     * @var array
40
     */
41
    private $versions;
42
43
    /**
44
     * Creates a new version list.
45
     *
46
     * @param string         $path     The Puli path.
47
     * @param PuliResource[] $versions The versions of the resource, starting
48
     *                                 with the first.
49
     */
50 125
    public function __construct($path, array $versions)
51
    {
52 125
        Assert::stringNotEmpty($path, 'The Puli path must be a non-empty string. Got: %s');
53 123
        Assert::allIsInstanceOf($versions, 'Puli\Repository\Api\Resource\PuliResource');
54 122
        Assert::greaterThanEq(count($versions), 1, 'Expected at least one version.');
55
56 121
        $this->path = $path;
57 121
        $this->versions = array_values($versions);
58 121
    }
59
60
    /**
61
     * Returns the path of the versioned resources.
62
     *
63
     * @return string The Puli path.
64
     */
65 1
    public function getPath()
66
    {
67 1
        return $this->path;
68
    }
69
70
    /**
71
     * Returns the current version of the resource.
72
     *
73
     * @return PuliResource The current version.
74
     */
75 19
    public function getCurrent()
76
    {
77 19
        return $this->get($this->getCurrentVersion());
78
    }
79
80
    /**
81
     * Returns the current version number.
82
     *
83
     * @return int The current version number.
84
     */
85 20
    public function getCurrentVersion()
86
    {
87 20
        return count($this->versions) - 1;
88
    }
89
90
    /**
91
     * Returns the first version of the resource.
92
     *
93
     * @return PuliResource The first version.
94
     */
95 28
    public function getFirst()
96
    {
97 28
        return $this->get($this->getFirstVersion());
98
    }
99
100
    /**
101
     * Returns the first version number.
102
     *
103
     * @return int The first version number.
104
     */
105 29
    public function getFirstVersion()
106
    {
107 29
        return 0;
108
    }
109
110
    /**
111
     * Returns whether a specific version exists.
112
     *
113
     * @param int $version The version number starting at 0.
114
     *
115
     * @return bool Whether the version exists.
116
     */
117 1
    public function contains($version)
118
    {
119 1
        return isset($this->versions[$version]);
120
    }
121
122
    /**
123
     * Returns a specific version of the resource.
124
     *
125
     * @param int $version The version number starting at 0.
126
     *
127
     * @return PuliResource The resource.
128
     *
129
     * @throws OutOfBoundsException If the version number does not exist.
130
     */
131 97
    public function get($version)
132
    {
133 97
        if (!isset($this->versions[$version])) {
134 1
            throw new OutOfBoundsException(sprintf(
135 1
                'The version %s of path %s does not exist.',
136
                $version,
137 1
                $this->path
138
            ));
139
        }
140
141 96
        return $this->versions[$version];
142
    }
143
144
    /**
145
     * Returns all version numbers.
146
     *
147
     * @return int[] The version numbers.
0 ignored issues
show
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...
148
     */
149 92
    public function getVersions()
150
    {
151 92
        return array_keys($this->versions);
152
    }
153
154
    /**
155
     * Returns the list as array indexed by version numbers.
156
     *
157
     * @return PuliResource[] The resource versions indexed by their version numbers.
158
     */
159 1
    public function toArray()
160
    {
161 1
        return $this->versions;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 1
    public function getIterator()
168
    {
169 1
        return new ArrayIterator($this->versions);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function offsetExists($offset)
176
    {
177 1
        return $this->contains($offset);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function offsetGet($offset)
184
    {
185 1
        return $this->get($offset);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function offsetSet($offset, $value)
192
    {
193
        throw new BadMethodCallException('List entries may not be changed.');
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function offsetUnset($offset)
200
    {
201
        throw new BadMethodCallException('List entries may not be removed.');
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 12
    public function count()
208
    {
209 12
        return count($this->versions);
210
    }
211
}
212