Completed
Pull Request — 1.0 (#58)
by Titouan
04:38 queued 02:05
created

ResourceStack::getAvailableVersions()   A

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\ChangeStream;
13
14
use Puli\Repository\Api\Resource\PuliResource;
15
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
16
use RuntimeException;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * Represents a versionned stack of resources for a given path.
21
 *
22
 * You can access different versions of a resource using a ChangeStream
23
 * that will return you resource stacks.
24
 *
25
 * @since  1.0
26
 *
27
 * @author Titouan Galopin <[email protected]>
28
 */
29
class ResourceStack extends ArrayResourceCollection
30
{
31
    /**
32
     * Get the last version from the stack.
33
     *
34
     * @return PuliResource
35
     */
36 11 View Code Duplication
    public function getCurrentVersion()
0 ignored issues
show
Duplication introduced by
This method 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...
37
    {
38 11
        $stack = $this->toArray();
39
40 11
        if (!is_array($stack) || 0 === count($stack)) {
41 2
            throw new RuntimeException('Impossible to find the current version of an empty stack.');
42
        }
43
44 9
        return end($stack);
45
    }
46
47
    /**
48
     * Get the first version from the stack.
49
     *
50
     * @return PuliResource
51
     */
52 9 View Code Duplication
    public function getFirstVersion()
0 ignored issues
show
Duplication introduced by
This method 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...
53
    {
54 9
        $stack = $this->toArray();
55
56 9
        if (!is_array($stack) || 0 === count($stack)) {
57
            throw new RuntimeException('Impossible to find the first version of an empty stack.');
58
        }
59
60 9
        return reset($stack);
61
    }
62
63
    /**
64
     * Get a specific version from the stack.
65
     *
66
     * @param int $versionNumber The version number (first is 0).
67
     *
68
     * @return PuliResource
69
     */
70 11
    public function getVersion($versionNumber)
71
    {
72 11
        $stack = $this->toArray();
73
74 11
        if (!is_array($stack) || 0 === count($stack)) {
75 1
            throw new RuntimeException(sprintf('Impossible to find the version %s of an empty stack.', $versionNumber));
76
        }
77
78 10
        Assert::oneOf($versionNumber, $this->getAvailableVersions(), 'Impossible to find version %s (stack: %s).');
79
80 9
        return $stack[$versionNumber];
81
    }
82
83
    /**
84
     * Get an array of the available versions of this resource.
85
     *
86
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
87
     */
88 11
    public function getAvailableVersions()
89
    {
90 11
        return $this->keys();
91
    }
92
}
93