|
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 Puli\Repository\Resource\Collection\ArrayResourceCollection; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use Webmozart\Assert\Assert; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Represents a versionned stack of resources for a given path. |
|
21
|
|
|
* |
|
22
|
|
|
* You can access different versions of a resource using a ChangeStream |
|
23
|
|
|
* that will return you resource stacks. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Titouan Galopin <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ResourceStack extends ArrayResourceCollection |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Get the last version from the stack. |
|
33
|
|
|
* |
|
34
|
|
|
* @return PuliResource |
|
35
|
|
|
*/ |
|
36
|
11 |
View Code Duplication |
public function getCurrentVersion() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
11 |
|
$stack = $this->toArray(); |
|
39
|
|
|
|
|
40
|
11 |
|
if (!is_array($stack) || 0 === count($stack)) { |
|
41
|
2 |
|
throw new RuntimeException('Impossible to find the current version of an empty stack.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
9 |
|
return end($stack); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the first version from the stack. |
|
49
|
|
|
* |
|
50
|
|
|
* @return PuliResource |
|
51
|
|
|
*/ |
|
52
|
9 |
View Code Duplication |
public function getFirstVersion() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
9 |
|
$stack = $this->toArray(); |
|
55
|
|
|
|
|
56
|
9 |
|
if (!is_array($stack) || 0 === count($stack)) { |
|
57
|
|
|
throw new RuntimeException('Impossible to find the first version of an empty stack.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
9 |
|
return reset($stack); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get a specific version from the stack. |
|
65
|
|
|
* |
|
66
|
|
|
* @param integer $versionNumber The version number (first is 0). |
|
67
|
|
|
* |
|
68
|
|
|
* @return PuliResource |
|
69
|
|
|
*/ |
|
70
|
11 |
|
public function getVersion($versionNumber) |
|
71
|
|
|
{ |
|
72
|
11 |
|
$stack = $this->toArray(); |
|
73
|
|
|
|
|
74
|
11 |
|
if (!is_array($stack) || 0 === count($stack)) { |
|
75
|
1 |
|
throw new RuntimeException(sprintf('Impossible to find the version %s of an empty stack.', $versionNumber)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
10 |
|
Assert::oneOf($versionNumber, $this->getAvailableVersions(), 'Impossible to find version %s (stack: %s).'); |
|
79
|
|
|
|
|
80
|
9 |
|
return $stack[$versionNumber]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get an array of the available versions of this resource. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
11 |
|
public function getAvailableVersions() |
|
89
|
|
|
{ |
|
90
|
11 |
|
return $this->keys(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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.