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

InMemoryChangeStream   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 8 2
A buildResourceStack() 0 15 4
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 12
    public function log($path, PuliResource $resource)
36
    {
37 12
        if (!array_key_exists($path, $this->stack)) {
38 12
            $this->stack[$path] = array();
39 12
        }
40
41 12
        $this->stack[$path][] = serialize($resource);
42 12
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 8
    public function buildResourceStack(ResourceRepository $repository, $path)
48
    {
49 8
        $stack = array();
50
51 8
        if (isset($this->stack[$path]) && is_array($this->stack[$path])) {
52 8
            foreach ($this->stack[$path] as $data) {
53 8
                $resource = unserialize($data);
54 8
                $resource->attachTo($repository, $path);
55
56 8
                $stack[] = $resource;
57 8
            }
58 8
        }
59
60 8
        return new ArrayResourceStack($stack);
61
    }
62
}
63