Completed
Pull Request — 1.0 (#58)
by Titouan
23:08 queued 20:24
created

ArrayResourceStack::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 0
Metric Value
c 1
b 0
f 0
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 Puli\Repository\Api\ChangeStream\ResourceStack;
15
use RuntimeException;
16
use Webmozart\Assert\Assert;
17
18
/**
19
 * Resource stack stored as an array.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Titouan Galopin <[email protected]>
24
 */
25
class ArrayResourceStack implements ResourceStack
26
{
27
    /**
28
     * @var array
29
     */
30
    private $stack;
31
32
    /**
33
     * @param array $stack
34
     */
35 20
    public function __construct($stack)
36
    {
37 20
        Assert::isArray($stack, 'Built resource stack must be an array.');
38
39 20
        $this->stack = $stack;
40 20
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 11
    public function getCurrentVersion()
46
    {
47 11
        if (0 === count($this->stack)) {
48 2
            throw new RuntimeException('Could not retrieve the current version of an empty stack.');
49
        }
50
51 9
        return end($this->stack);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 9
    public function getFirstVersion()
58
    {
59 9
        if (0 === count($this->stack)) {
60
            throw new RuntimeException('Could not retrieve the first version of an empty stack.');
61
        }
62
63 9
        return reset($this->stack);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 11
    public function getVersion($versionNumber)
70
    {
71 11
        if (0 === count($this->stack)) {
72 1
            throw new RuntimeException(sprintf(
73 1
                'Could not retrieve the version %s of an empty stack.',
74
                $versionNumber
75
            ));
76
        }
77
78 10
        Assert::oneOf($versionNumber, $this->getAvailableVersions(), 'Could not retrieve the version %s (stack: %s).');
79
80 9
        return $this->stack[$versionNumber];
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 11
    public function getAvailableVersions()
87
    {
88 11
        return array_keys($this->stack);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function toArray()
95
    {
96
        return $this->stack;
97
    }
98
}
99