Completed
Pull Request — 1.0 (#58)
by Titouan
02:38
created

AbstractEditableRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 5
c 4
b 1
f 2
lcom 1
cbo 3
dl 0
loc 46
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStack() 0 12 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 607
    public function __construct(ChangeStream $changeStream = null)
41
    {
42 607
        $this->changeStream = $changeStream;
43 607
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 14
    public function getStack($path)
49
    {
50 14
        if (!$this->changeStream) {
51 7
            throw new UnsupportedOperationException(sprintf(
52
                'Could not retrieve the resource stack for path "%s" as no ChangeStream was passed to the '.
53 7
                'constructor of the repository.',
54
                $path
55 7
            ));
56
        }
57
58 7
        return $this->changeStream->buildStack($this, $path);
59
    }
60
61
    /**
62
     * Append a change to the ChangeStream if possible.
63
     *
64
     * @param PuliResource $resource
65
     */
66 498
    protected function appendToChangeStream(PuliResource $resource)
67
    {
68 498
        if ($this->changeStream) {
69 7
            $this->changeStream->append($resource);
70 7
        }
71 498
    }
72
}
73