Completed
Pull Request — 1.0 (#58)
by Titouan
04:36 queued 01:59
created

AbstractEditableRepository::setChangeStream()   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 0
Metric Value
c 1
b 0
f 0
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\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 580
    public function __construct(ChangeStream $changeStream = null)
41
    {
42 580
        $this->changeStream = $changeStream;
43 580
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 7
    public function getStack($path)
49
    {
50 7
        if (!$this->changeStream) {
51
            throw new UnsupportedOperationException(sprintf(
52
                'Impossible to retrieve resource stack for path "%s" as no ChangeStream is configured.',
53
                $path
54
            ));
55
        }
56
57 7
        return $this->changeStream->buildResourceStack($this, $path);
58
    }
59
60
    /**
61
     * @param ChangeStream $changeStream
0 ignored issues
show
Documentation introduced by
Should the type for parameter $changeStream not be null|ChangeStream?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
62
     */
63 7
    public function setChangeStream(ChangeStream $changeStream = null)
64
    {
65 7
        $this->changeStream = $changeStream;
66 7
    }
67
68
    /**
69
     * Log a change in the ChangeStream if possible.
70
     *
71
     * @param string       $path
72
     * @param PuliResource $resource
73
     */
74 477
    public function logChange($path, PuliResource $resource)
75
    {
76 477
        if ($this->changeStream) {
77 7
            $this->changeStream->log($path, $resource);
78 7
        }
79 477
    }
80
}
81