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

ResourceStack::getFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 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 28
    public function __construct($stack)
39
    {
40 28
        Assert::isArray($stack, 'Built resource stack must be an array.');
41
42 27
        $this->stack = $stack;
43 27
    }
44
45
    /**
46
     * Get the current version resource.
47
     *
48
     * @return PuliResource
49
     */
50 10
    public function getCurrent()
51
    {
52 10
        return $this->get($this->getCurrentVersion());
53
    }
54
55
    /**
56
     * Get the current version number.
57
     *
58
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60 12 View Code Duplication
    public function getCurrentVersion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62 12
        $versions = $this->getVersions();
63
64 12
        if (0 === count($versions)) {
65 2
            throw new RuntimeException('Could not retrieve the current version of an empty stack.');
66
        }
67
68 10
        return end($versions);
69
    }
70
71
    /**
72
     * Get the current version resource.
73
     *
74
     * @return PuliResource
75
     */
76 10
    public function getFirst()
77
    {
78 10
        return $this->get($this->getFirstVersion());
79
    }
80
81
    /**
82
     * Get the first version number.
83
     *
84
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86 12 View Code Duplication
    public function getFirstVersion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88 12
        $versions = $this->getVersions();
89
90 12
        if (0 === count($versions)) {
91 2
            throw new RuntimeException('Could not retrieve the first version of an empty stack.');
92
        }
93
94 10
        return reset($versions);
95
    }
96
97
    /**
98
     * Get a specific version resource from the stack by its version number.
99
     *
100
     * @param int $version The version number (first is 0).
101
     *
102
     * @return PuliResource
103
     */
104 15
    public function get($version)
105
    {
106 15
        $versions = $this->getVersions();
107
108 15
        if (0 === count($versions)) {
109 1
            throw new RuntimeException(sprintf(
110 1
                'Could not retrieve the version %s of an empty stack.',
111
                $version
112 1
            ));
113
        }
114
115 14
        Assert::oneOf($version, $versions, 'Could not retrieve the version %s (stack: %s).');
116
117 13
        return $this->stack[$version];
118
    }
119
120
    /**
121
     * Get an array of the available versions of this resource.
122
     *
123
     * @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...
124
     */
125 23
    public function getVersions()
126
    {
127 23
        return array_keys($this->stack);
128
    }
129
130
    /**
131
     * Returns the stack contents as array.
132
     *
133
     * @return PuliResource[] The resources in the stack.
134
     */
135
    public function toArray()
136
    {
137
        return $this->stack;
138
    }
139
}
140