Completed
Pull Request — 1.0 (#58)
by Titouan
21:06 queued 02:30
created

InMemoryChangeStream::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 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 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
    public function log($path, PuliResource $resource)
36
    {
37
        if (!array_key_exists($path, $this->stack)) {
38
            $this->stack[$path] = array();
39
        }
40
41
        $this->stack[$path][] = serialize($resource);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function buildResourceStack(ResourceRepository $repository, $path)
48
    {
49
        $stack = array();
50
51
        if (isset($this->stack[$path]) && is_array($this->stack[$path])) {
52
            foreach ($this->stack[$path] as $data) {
53
                $resource = unserialize($data);
54
                $resource->attachTo($repository, $path);
55
56
                $stack[] = $resource;
57
            }
58
        }
59
60
        return new ArrayResourceStack($stack);
61
    }
62
}
63