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

InMemoryChangeStream   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

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