JsonChangeStream   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 18
c 2
b 0
f 1
lcom 1
cbo 5
dl 9
loc 135
ccs 50
cts 52
cp 0.9615
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 4 1
A __construct() 0 7 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
A load() 0 9 2

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
        $this->encoder->setPrettyPrinting(true);
54 20
        $this->encoder->setEscapeSlash(false);
55 20
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 18
    public function append(PuliResource $resource)
61
    {
62 18
        if (null === $this->json) {
63 17
            $this->load();
64
        }
65
66 18
        if (!isset($this->json[$resource->getPath()])) {
67 18
            $this->json[$resource->getPath()] = array();
68
        }
69
70 18
        $this->json[$resource->getPath()][] = serialize($resource);
71
72 18
        $this->flush();
73 18
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 6
    public function purge($path)
79
    {
80 6
        if (null === $this->json) {
81
            $this->load();
82
        }
83
84 6
        unset($this->json[$path]);
85
86 6
        $this->flush();
87 6
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function clear()
93
    {
94 2
        if (null === $this->json) {
95
            $this->load();
96
        }
97
98 2
        $this->json = array();
99
100 2
        $this->flush();
101 2
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 8
    public function contains($path)
107
    {
108 8
        if (null === $this->json) {
109 5
            $this->load();
110
        }
111
112 8
        return isset($this->json[$path]);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 14
    public function getVersions($path, ResourceRepository $repository = null)
119
    {
120 14
        if (null === $this->json) {
121 7
            $this->load();
122
        }
123
124 14
        if (!isset($this->json[$path])) {
125 4
            throw NoVersionFoundException::forPath($path);
126
        }
127
128 10
        $versions = array();
129
130 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...
131 10
            $resource = unserialize($resource);
132
133 10
            if (null !== $repository) {
134 4
                $resource->attachTo($repository, $path);
135
            }
136
137 10
            $versions[] = $resource;
138
        }
139
140 10
        return new VersionList($path, $versions);
141
    }
142
143
    /**
144
     * Loads the JSON file.
145
     */
146 20
    private function load()
147
    {
148 20
        $decoder = new JsonDecoder();
149 20
        $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
150
151 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...
152 9
            ? $decoder->decodeFile($this->path)
153 20
            : array();
154 20
    }
155
156
    /**
157
     * Writes the JSON file.
158
     */
159 18
    private function flush()
160
    {
161 18
        $this->encoder->encodeFile($this->json, $this->path);
162 18
    }
163
}
164