Completed
Pull Request — 1.0 (#58)
by Titouan
02:34
created

ResourceStack::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

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...
98
     */
99 11
    public function getAvailableVersions()
100
    {
101 11
        return array_keys($this->stack);
102
    }
103
104
    /**
105
     * Returns the stack contents as array.
106
     *
107
     * @return PuliResource[] The resources in the stack.
108
     */
109
    public function toArray()
110
    {
111
        return $this->stack;
112
    }
113
}
114