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

ArrayResourceStack::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
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 Puli\Repository\Api\ChangeStream\ResourceStack;
15
use Puli\Repository\Api\Resource\PuliResource;
16
use Puli\Repository\Api\ResourceCollection;
17
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
18
use RuntimeException;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * Resource stack stored as an array.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Titouan Galopin <[email protected]>
27
 */
28
class ArrayResourceStack implements 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
     * @inheritdoc
47
     */
48 11
    public function getCurrentVersion()
49
    {
50 11
        if (0 === count($this->stack)) {
51 2
            throw new RuntimeException('Could not retrieve the current version of an empty stack.');
52
        }
53
54 9
        return end($this->stack);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 9
    public function getFirstVersion()
61
    {
62 9
        if (0 === count($this->stack)) {
63
            throw new RuntimeException('Could not retrieve the first version of an empty stack.');
64
        }
65
66 9
        return reset($this->stack);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 11
    public function getVersion($versionNumber)
73
    {
74 11
        if (0 === count($this->stack)) {
75 1
            throw new RuntimeException(sprintf(
76 1
                'Could not retrieve the version %s of an empty stack.',
77
                $versionNumber
78 1
            ));
79
        }
80
81 10
        Assert::oneOf($versionNumber, $this->getAvailableVersions(), 'Could not retrieve the version %s (stack: %s).');
82
83 9
        return $this->stack[$versionNumber];
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 11
    public function getAvailableVersions()
90
    {
91 11
        return array_keys($this->stack);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function toArray()
98
    {
99
        return $this->stack;
100
    }
101
}
102