Completed
Pull Request — 1.0 (#58)
by Titouan
04:38 queued 02:05
created

ChangeStream::log()   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 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
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 Puli\Repository\Api\Resource\PuliResource;
15
use Puli\Repository\Api\ResourceRepository;
16
17
/**
18
 * Stream to track repositories changes and fetch previous versions of resources.
19
 *
20
 * @since  1.0
21
 *
22
 * @author Titouan Galopin <[email protected]>
23
 */
24
class ChangeStream
25
{
26
    /**
27
     * @var array
28
     */
29
    private $stack = array();
30
31
    /**
32
     * Store a version of a resource in the ChangeStream to retrieve it if needed.
33
     *
34
     * @param string       $path
35
     * @param PuliResource $resource
36
     */
37 12
    public function log($path, PuliResource $resource)
38
    {
39 12
        if (!array_key_exists($path, $this->stack)) {
40 12
            $this->stack[$path] = array();
41 12
        }
42
43 12
        $this->stack[$path][] = serialize($resource);
44 12
    }
45
46
    /**
47
     * Create a stack of resources for the given path.
48
     *
49
     * @param ResourceRepository $repository
50
     * @param string             $path
51
     *
52
     * @return ResourceStack
53
     */
54 8
    public function buildResourceStack(ResourceRepository $repository, $path)
55
    {
56 8
        $stack = array();
57
58 8
        if (isset($this->stack[$path]) && is_array($this->stack[$path])) {
59 8
            foreach ($this->stack[$path] as $data) {
60 8
                $resource = unserialize($data);
61 8
                $resource->attachTo($repository, $path);
62
63 8
                $stack[] = $resource;
64 8
            }
65 8
        }
66
67 8
        return new ResourceStack($stack);
68
    }
69
}
70