Completed
Push — master ( b2e536...a4e6dd )
by Arne
02:05
created

LazyLoadedIndex   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addObject() 0 6 1
A getObjectByPath() 0 6 1
A getObjectByBlobId() 0 6 1
A count() 0 6 1
A getIterator() 0 6 1
A loadIndex() 0 14 3
1
<?php
2
3
namespace Storeman\VaultLayout;
4
5
use Storeman\Index;
6
use Storeman\IndexObject;
7
8
class LazyLoadedIndex extends Index
9
{
10
    protected $pathMap = null;
11
12
    /**
13
     * @var callable
14
     */
15
    protected $indexLoader;
16
17
    public function __construct(callable $indexLoader)
18
    {
19
        $this->indexLoader = $indexLoader;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function addObject(IndexObject $indexObject): Index
26
    {
27
        $this->loadIndex();
28
29
        return parent::addObject($indexObject);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getObjectByPath(string $path): ?IndexObject
36
    {
37
        $this->loadIndex();
38
39
        return parent::getObjectByPath($path);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getObjectByBlobId(string $blobId): ?IndexObject
46
    {
47
        $this->loadIndex();
48
49
        return parent::getObjectByBlobId($blobId);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function count(): int
56
    {
57
        $this->loadIndex();
58
59
        return parent::count();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getIterator(): \Traversable
66
    {
67
        $this->loadIndex();
68
69
        return parent::getIterator();
70
    }
71
72
    /**
73
     * Lazy-loads the actual index from the injected loader.
74
     */
75
    protected function loadIndex()
76
    {
77
        if ($this->pathMap === null)
78
        {
79
            $index = call_user_func($this->indexLoader);
80
81
            if (!($index instanceof Index))
82
            {
83
                throw new \LogicException();
84
            }
85
86
            $this->pathMap = $index->pathMap;
87
        }
88
    }
89
}
90