Failed Conditions
Push — 1.0 ( 9f5a0b...fe7a2f )
by Bernhard
30:36 queued 17:00
created

AbstractEditableRepository::storeVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
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;
13
14
use Puli\Repository\Api\ChangeStream\ChangeStream;
15
use Puli\Repository\Api\EditableRepository;
16
use Puli\Repository\Api\Resource\PuliResource;
17
18
/**
19
 * Abstract base for editable repositories providing tools to avoid code duplication.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 * @author Titouan Galopin <[email protected]>
25
 */
26
abstract class AbstractEditableRepository extends AbstractRepository implements EditableRepository
27
{
28
    /**
29
     * @var ChangeStream
30
     */
31
    private $changeStream;
32
33
    /**
34
     * Create the repository.
35
     *
36
     * @param ChangeStream|null $changeStream If provided, the repository will log
37
     *                                        resources changes in this change stream.
38
     */
39 886
    public function __construct(ChangeStream $changeStream = null)
40
    {
41 886
        $this->changeStream = $changeStream;
42 886
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 105
    public function getVersions($path)
48
    {
49 105
        if (null === $this->changeStream) {
50 21
            return parent::getVersions($path);
51
        }
52
53 84
        return $this->changeStream->getVersions($path, $this);
54
    }
55
56
    /**
57
     * Stores a version of a resource in the change stream.
58
     *
59
     * @param PuliResource $resource The resource version.
60
     */
61 612
    protected function storeVersion(PuliResource $resource)
62
    {
63 612
        if (null !== $this->changeStream) {
64 314
            $this->changeStream->append($resource);
65
        }
66 612
    }
67
68
    /**
69
     * Removes all versions of a resource from the change stream.
70
     *
71
     * @param string $path The Puli path.
72
     */
73 78
    protected function removeVersions($path)
74
    {
75 78
        if (null !== $this->changeStream) {
76 78
            $this->changeStream->purge($path);
77
        }
78 78
    }
79
80
    /**
81
     * Clears the change stream.
82
     */
83 128
    protected function clearVersions()
84
    {
85 128
        if (null !== $this->changeStream) {
86 112
            $this->changeStream->clear();
87
        }
88 128
    }
89
}
90