Completed
Push — 1.0 ( fe7a2f...22ab07 )
by Bernhard
12:18
created

KeyValueStoreChangeStream::getVersions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 5
Ratio 27.78 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 5
loc 18
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 2
crap 4
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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