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

ChangeStream   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 46
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\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