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

AbstractEditableRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 9
c 5
b 1
f 2
lcom 1
cbo 2
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVersions() 0 8 2
A storeVersion() 0 6 2
A removeVersions() 0 6 2
A clearVersions() 0 6 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