Failed Conditions
Push — 1.0 ( 22ab07...8ca4b1 )
by Bernhard
04:47
created

JsonChangeStream   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 133
Duplicated Lines 6.77 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
c 1
b 0
f 1
lcom 1
cbo 5
dl 9
loc 133
ccs 54
cts 58
cp 0.931
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 9 2
A flush() 0 4 1
A append() 0 14 3
A purge() 0 10 2
A clear() 0 10 2
A contains() 0 8 2
B getVersions() 9 24 5

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\Json\JsonDecoder;
20
use Webmozart\Json\JsonEncoder;
21
22
/**
23
 * A change stream backed by a JSON file.
24
 *
25
 * @since  1.0
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 */
29
class JsonChangeStream implements ChangeStream
30
{
31
    /**
32
     * @var string
33
     */
34
    private $path;
35
36
    /**
37
     * @var array
38
     */
39
    private $json;
40
41
    /**
42
     * @var JsonEncoder
43
     */
44
    private $encoder;
45
46
    /**
47
     * @param string $path The path to the JSON file.
48
     */
49 20
    public function __construct($path)
50
    {
51 20
        $this->path = $path;
52 20
        $this->encoder = new JsonEncoder();
53 20
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 18
    public function append(PuliResource $resource)
59
    {
60 18
        if (null === $this->json) {
61 17
            $this->load();
62 17
        }
63
64 18
        if (!isset($this->json[$resource->getPath()])) {
65 18
            $this->json[$resource->getPath()] = array();
66 18
        }
67
68 18
        $this->json[$resource->getPath()][] = serialize($resource);
69
70 18
        $this->flush();
71 18
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 6
    public function purge($path)
77
    {
78 6
        if (null === $this->json) {
79
            $this->load();
80
        }
81
82 6
        unset($this->json[$path]);
83
84 6
        $this->flush();
85 6
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function clear()
91
    {
92 2
        if (null === $this->json) {
93
            $this->load();
94
        }
95
96 2
        $this->json = array();
97
98 2
        $this->flush();
99 2
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 9
    public function contains($path)
105
    {
106 8
        if (null === $this->json) {
107 5
            $this->load();
108 5
        }
109
110 9
        return isset($this->json[$path]);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 14
    public function getVersions($path, ResourceRepository $repository = null)
117
    {
118 14
        if (null === $this->json) {
119 7
            $this->load();
120 7
        }
121
122 14
        if (!isset($this->json[$path])) {
123 4
            throw NoVersionFoundException::forPath($path);
124
        }
125
126 10
        $versions = array();
127
128 10 View Code Duplication
        foreach ($this->json[$path] as $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...
129 10
            $resource = unserialize($resource);
130
131 10
            if (null !== $repository) {
132 4
                $resource->attachTo($repository, $path);
133 4
            }
134
135 10
            $versions[] = $resource;
136 10
        }
137
138 10
        return new VersionList($path, $versions);
139
    }
140
141
    /**
142
     * Loads the JSON file.
143
     */
144 20
    private function load()
145
    {
146 20
        $decoder = new JsonDecoder();
147 20
        $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
148
149 20
        $this->json = file_exists($this->path)
0 ignored issues
show
Documentation Bug introduced by
It seems like file_exists($this->path)...($this->path) : array() of type * is incompatible with the declared type array of property $json.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
150 20
            ? $decoder->decodeFile($this->path)
151 20
            : array();
152 20
    }
153
154
    /**
155
     * Writes the JSON file.
156
     */
157 18
    private function flush()
158
    {
159 18
        $this->encoder->encodeFile($this->json, $this->path);
160 18
    }
161
}
162