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\ChangeStream; |
15
|
|
|
use Puli\Repository\Api\ChangeStream\VersionList; |
16
|
|
|
use Puli\Repository\Api\NoVersionFoundException; |
17
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
18
|
|
|
use Puli\Repository\Api\ResourceRepository; |
19
|
|
|
use Webmozart\KeyValueStore\Api\KeyValueStore; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A change stream backed by a key-value store. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class KeyValueStoreChangeStream implements ChangeStream |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var KeyValueStore |
32
|
|
|
*/ |
33
|
|
|
private $store; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param KeyValueStore $store |
37
|
|
|
*/ |
38
|
10 |
|
public function __construct(KeyValueStore $store) |
39
|
|
|
{ |
40
|
10 |
|
$this->store = $store; |
41
|
10 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
9 |
|
public function append(PuliResource $resource) |
47
|
|
|
{ |
48
|
9 |
|
$versions = $this->store->get($resource->getPath(), array()); |
49
|
|
|
|
50
|
9 |
|
$versions[] = $resource; |
51
|
|
|
|
52
|
9 |
|
$this->store->set($resource->getPath(), $versions); |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
3 |
|
public function purge($path) |
59
|
|
|
{ |
60
|
3 |
|
$this->store->remove($path); |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function contains($path) |
67
|
|
|
{ |
68
|
4 |
|
return $this->store->exists($path); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function clear() |
75
|
|
|
{ |
76
|
1 |
|
$this->store->clear(); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
7 |
|
public function getVersions($path, ResourceRepository $repository = null) |
83
|
|
|
{ |
84
|
7 |
|
$versions = $this->store->get($path, array()); |
85
|
|
|
|
86
|
7 |
|
if (empty($versions)) { |
87
|
2 |
|
throw NoVersionFoundException::forPath($path); |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
if (null !== $repository) { |
91
|
2 |
View Code Duplication |
foreach ($versions as $key => $resource) { |
|
|
|
|
92
|
2 |
|
$resource = clone $resource; |
93
|
2 |
|
$resource->attachTo($repository); |
94
|
2 |
|
$versions[$key] = $resource; |
95
|
2 |
|
} |
96
|
2 |
|
} |
97
|
|
|
|
98
|
5 |
|
return new VersionList($path, $versions); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.