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

AbstractEditableRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 78.57%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStack() 0 12 2
A logChange() 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\EditableRepository;
15
use Puli\Repository\Api\Resource\PuliResource;
16
use Puli\Repository\Api\UnsupportedOperationException;
17
use Puli\Repository\ChangeStream\ChangeStream;
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
    protected $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 583
    public function __construct(ChangeStream $changeStream = null)
41
    {
42 583
        $this->changeStream = $changeStream;
43 583
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 7
    public function getStack($path)
49
    {
50 7
        if (!$this->changeStream) {
51
            throw new UnsupportedOperationException(sprintf(
52
                'Could not retrieve the resource stack for path "%s" as no ChangeStream was passed to the '.
53
                'constructor of the repository.',
54
                $path
55
            ));
56
        }
57
58 7
        return $this->changeStream->buildResourceStack($this, $path);
59
    }
60
61
    /**
62
     * Log a change in the ChangeStream if possible.
63
     *
64
     * @param string       $path
65
     * @param PuliResource $resource
66
     */
67 477
    protected function logChange($path, PuliResource $resource)
68
    {
69 477
        if ($this->changeStream) {
70 7
            $this->changeStream->log($path, $resource);
71 7
        }
72 477
    }
73
}
74