Completed
Pull Request — 1.0 (#58)
by Titouan
03:47 queued 01:10
created

AbstractEditableRepository::appendToChangeStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 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
    public function __construct(ChangeStream $changeStream = null)
40
    {
41
        $this->changeStream = $changeStream;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getStack($path)
48
    {
49
        if (!$this->changeStream) {
50
            return parent::getStack($path);
51
        }
52
53
        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
    protected function appendToChangeStream(PuliResource $resource)
62
    {
63
        if ($this->changeStream) {
64
            $this->changeStream->append($resource);
65
        }
66
    }
67
}
68