Completed
Push — 1.0 ( 0e2cc5...14cdbe )
by Bernhard
02:42
created

ResourceStack::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 OutOfBoundsException;
15
use Puli\Repository\Api\Resource\PuliResource;
16
use Webmozart\Assert\Assert;
17
18
/**
19
 * Represents a versioned 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 24
    public function __construct(array $stack)
39
    {
40 24
        Assert::allIsInstanceOf($stack, 'Puli\Repository\Api\Resource\PuliResource');
41 23
        Assert::greaterThanEq(count($stack), 1, 'A ResourceStack cannot be empty');
42
43 22
        $this->stack = array_values($stack);
44 22
    }
45
46
    /**
47
     * Get the current version resource.
48
     *
49
     * @return PuliResource
50
     */
51 9
    public function getCurrent()
52
    {
53 9
        return $this->get($this->getCurrentVersion());
54
    }
55
56
    /**
57
     * Get the current version number.
58
     *
59
     * @return int
60
     */
61 10
    public function getCurrentVersion()
62
    {
63 10
        return count($this->stack) - 1;
64
    }
65
66
    /**
67
     * Get the current version resource.
68
     *
69
     * @return PuliResource
70
     */
71 9
    public function getFirst()
72
    {
73 9
        return $this->get($this->getFirstVersion());
74
    }
75
76
    /**
77
     * Get the first version number.
78
     *
79
     * @return int
80
     */
81 10
    public function getFirstVersion()
82
    {
83 10
        return 0;
84
    }
85
86
    /**
87
     * Get a specific version resource from the stack by its version number.
88
     *
89
     * @param int $version The version number (first is 0).
90
     *
91
     * @return PuliResource
92
     */
93 14
    public function get($version)
94
    {
95 14
        if (!isset($this->stack[$version])) {
96 1
            throw new OutOfBoundsException('Could not retrieve the version %s (stack: %s).');
97
        }
98
99 13
        return $this->stack[$version];
100
    }
101
102
    /**
103
     * Get an array of the available versions of this resource.
104
     *
105
     * @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...
106
     */
107 9
    public function getVersions()
108
    {
109 9
        return array_keys($this->stack);
110
    }
111
112
    /**
113
     * Returns the stack contents as array.
114
     *
115
     * @return PuliResource[] The resources in the stack.
116
     */
117
    public function toArray()
118
    {
119
        return $this->stack;
120
    }
121
}
122