Completed
Pull Request — 1.0 (#58)
by Titouan
03:06
created

AbstractEditableRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 5
c 4
b 1
f 2
lcom 1
cbo 2
dl 0
loc 42
ccs 11
cts 12
cp 0.9167
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStack() 0 8 2
A appendToChangeStream() 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
use Puli\Repository\Api\UnsupportedOperationException;
18
19
/**
20
 * Abstract base for editable repositories providing tools to avoid code duplication.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 * @author Titouan Galopin <[email protected]>
26
 */
27
abstract class AbstractEditableRepository extends AbstractRepository implements EditableRepository
28
{
29
    /**
30
     * @var ChangeStream
31
     */
32
    private $changeStream;
33
34
    /**
35
     * Create the repository.
36
     *
37
     * @param ChangeStream|null $changeStream If provided, the repository will log
38
     *                                        resources changes in this change stream.
39
     */
40 608
    public function __construct(ChangeStream $changeStream = null)
41
    {
42 608
        $this->changeStream = $changeStream;
43 608
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 14
    public function getStack($path)
49
    {
50 14
        if (!$this->changeStream) {
51
            return parent::getStack($path);
52
        }
53
54 14
        return $this->changeStream->buildStack($this, $path);
55
    }
56
57
    /**
58
     * Append a change to the ChangeStream if possible.
59
     *
60
     * @param PuliResource $resource
61
     */
62 491
    protected function appendToChangeStream(PuliResource $resource)
63
    {
64 491
        if ($this->changeStream) {
65 7
            $this->changeStream->append($resource);
66 7
        }
67 491
    }
68
}
69