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

ArrayResourceStack   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 74
ccs 19
cts 22
cp 0.8636
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCurrentVersion() 0 8 2
A getFirstVersion() 0 8 2
A getVersion() 0 13 2
A getAvailableVersions() 0 4 1
A toArray() 0 4 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\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