Failed Conditions
Pull Request — 1.0 (#81)
by Bernhard
16:33
created

JsonChangeStream::getVersions()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 9
Ratio 37.5 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 24
ccs 15
cts 15
cp 1
rs 8.5126
cc 5
eloc 12
nc 8
nop 2
crap 5
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
use Webmozart\KeyValueStore\Api\KeyValueStore;
22
23
/**
24
 * A change stream backed by a JSON file.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class JsonChangeStream implements ChangeStream
31
{
32
    /**
33
     * @var string
34
     */
35
    private $path;
36
37
    /**
38
     * @var array
39
     */
40
    private $json;
41
42
    /**
43
     * @var JsonEncoder
44
     */
45
    private $encoder;
46
47
    /**
48
     * @param string $path The path to the JSON file.
49
     */
50 20
    public function __construct($path)
51
    {
52 20
        $this->path = $path;
53 20
        $this->encoder = new JsonEncoder();
54 20
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 18
    public function append(PuliResource $resource)
60
    {
61 18
        if (null === $this->json) {
62 17
            $this->load();
63 17
        }
64
65 18
        if (!isset($this->json[$resource->getPath()])) {
66 18
            $this->json[$resource->getPath()] = array();
67 18
        }
68
69 18
        $this->json[$resource->getPath()][] = serialize($resource);
70
71 18
        $this->flush();
72 18
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function purge($path)
78
    {
79 6
        if (null === $this->json) {
80
            $this->load();
81
        }
82
83 6
        unset($this->json[$path]);
84
85 6
        $this->flush();
86 6
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function clear()
92
    {
93 2
        if (null === $this->json) {
94
            $this->load();
95
        }
96
97 2
        $this->json = array();
98
99 2
        $this->flush();
100 2
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 8
    public function contains($path)
106
    {
107 8
        if (null === $this->json) {
108 5
            $this->load();
109 5
        }
110
111 8
        return isset($this->json[$path]);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 14
    public function getVersions($path, ResourceRepository $repository = null)
118
    {
119 14
        if (null === $this->json) {
120 7
            $this->load();
121 7
        }
122
123 14
        if (!isset($this->json[$path])) {
124 4
            throw NoVersionFoundException::forPath($path);
125
        }
126
127 10
        $versions = array();
128
129 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...
130 10
            $resource = unserialize($resource);
131
132 10
            if (null !== $repository) {
133 4
                $resource->attachTo($repository, $path);
134 4
            }
135
136 10
            $versions[] = $resource;
137 10
        }
138
139 10
        return new VersionList($path, $versions);
140
    }
141
142
    /**
143
     * Loads the JSON file.
144
     */
145 20
    private function load()
146
    {
147 20
        $decoder = new JsonDecoder();
148 20
        $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
149
150 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...
151 20
            ? $decoder->decodeFile($this->path)
152 20
            : array();
153 20
    }
154
155
    /**
156
     * Writes the JSON file.
157
     */
158 18
    private function flush()
159
    {
160 18
        $this->encoder->encodeFile($this->json, $this->path);
161 18
    }
162
}
163