Completed
Push — 1.0 ( 0e2cc5...14cdbe )
by Bernhard
02:42
created

AbstractEditableRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 608
    public function __construct(ChangeStream $changeStream = null)
40
    {
41 608
        $this->changeStream = $changeStream;
42 608
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 14
    public function getStack($path)
48
    {
49 14
        if (!$this->changeStream) {
50
            return parent::getStack($path);
51
        }
52
53 14
        return $this->changeStream->buildStack($this, $path);
54
    }
55
56
    /**
57
     * Append a change to the ChangeStream if possible.
58
     *
59
     * @param PuliResource $resource
60
     */
61 491
    protected function appendToChangeStream(PuliResource $resource)
62
    {
63 491
        if ($this->changeStream) {
64 7
            $this->changeStream->append($resource);
65 7
        }
66 491
    }
67
}
68