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

KeyValueStoreChangeStream   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 6.85 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 4
dl 5
loc 73
ccs 28
cts 28
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A append() 0 8 1
A purge() 0 4 1
A contains() 0 4 1
A clear() 0 4 1
A getVersions() 5 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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