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

InMemoryChangeStream::append()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 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\ChangeStream;
13
14
use InvalidArgumentException;
15
use Puli\Repository\Api\ChangeStream\ChangeStream;
16
use Puli\Repository\Api\Resource\PuliResource;
17
use Puli\Repository\Api\ResourceRepository;
18
19
/**
20
 * ChangeStream stored in memory (as an array).
21
 *
22
 * @since  1.0
23
 *
24
 * @author Titouan Galopin <[email protected]>
25
 */
26
class InMemoryChangeStream implements ChangeStream
27
{
28
    /**
29
     * @var array
30
     */
31
    private $stack = array();
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 16
    public function append(PuliResource $resource)
37
    {
38 16
        if (!isset($this->stack[$resource->getPath()])) {
39 16
            $this->stack[$resource->getPath()] = array();
40 16
        }
41
42 16
        $this->stack[$resource->getPath()][] = $resource;
43 16
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 19
    public function buildStack(ResourceRepository $repository, $path)
49
    {
50 19
        if (!isset($this->stack[$path])) {
51 8
            throw new InvalidArgumentException(sprintf('No version of path %s were found in the ChangeStream', $path));
52
        }
53
54 11
        $resources = array();
55
56 11
        foreach ($this->stack[$path] as $resource) {
57 11
            $resource->attachTo($repository, $path);
58 11
            $resources[] = $resource;
59 11
        }
60
61 11
        return new ResourceStack($resources);
62
    }
63
}
64