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

InMemoryChangeStream   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 8 2
A buildStack() 0 15 3
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