Completed
Push — 84-phar ( 2b69cf )
by Nicolas
51:45 queued 47:52
created

MultipleAdapter::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Karma\Filesystem\Adapters;
4
5
use Gaufrette\Adapter;
6
7
class MultipleAdapter implements Adapter
8
{
9
    private
10
        $mountTable;
11
    
12
    public function __construct()
13
    {
14
        $this->mountTable = array();
15
    }
16
    
17
    public function mount($mountPoint, Adapter $adapter)
18
    {
19
        $directorySeparator = '/';
20
        $mountPoint = rtrim($mountPoint, $directorySeparator) . $directorySeparator;
21
        
22
        $this->mountTable[$mountPoint] = $adapter;
23
    }
24
    
25
    private function find($key)
26
    {
27
        $mountPoints = array_keys($this->mountTable);
28
        
29
        foreach($mountPoints as $mountPoint)
30
        {
31
            if(strpos($key, $mountPoint) === 0)
32
            {
33
                $relativeKey = substr($key, strlen($mountPoint));
34
                
35
                return array($this->mountTable[$mountPoint], $relativeKey);
36
            }
37
        }
38
39
        throw new \RuntimeException("Key $key not found");
40
    }
41
    
42
    public function read($key)
43
    {
44
        list($adapter, $relativeKey) = $this->find($key);
45
        
46
        return $adapter->read($relativeKey);
47
    }
48
    
49
    public function write($key, $content)
50
    {
51
        list($adapter, $relativeKey) = $this->find($key);
52
        
53
        return $adapter->write($relativeKey, $content);
54
    }
55
    
56
    public function exists($key)
57
    {
58
        try
59
        {
60
            list($adapter, $relativeKey) = $this->find($key);
61
        }
62
        catch(\RuntimeException $e)
63
        {
64
            return false;
65
        }
66
        
67
        return $adapter->exists($relativeKey);
68
    }
69
    
70
    public function keys()
71
    {
72
        $keys = array();
73
74
        foreach($this->mountTable as $mountPoint => $adapter)
75
        {
76
            $adapterKeys = $adapter->keys();
77
            
78
            array_walk($adapterKeys, function(& $key) use($mountPoint) {
79
                $key = $mountPoint . $key;
80
            });
81
                        
82
            $keys = array_merge($keys, $adapterKeys);
83
        }
84
        
85
        return $keys;
86
    }
87
    
88
    public function mtime($key)
89
    {
90
        list($adapter, $relativeKey) = $this->find($key);
91
        
92
        return $adapter->mtime($relativeKey);
93
    }
94
    
95
    public function delete($key)
96
    {
97
        list($adapter, $relativeKey) = $this->find($key);
98
        
99
        return $adapter->delete($relativeKey);
100
    }
101
    
102
    public function rename($sourceKey, $targetKey)
103
    {
104
        throw new \RuntimeException('Not implemented yet : ' . __METHOD__);
105
    }
106
    
107
    public function isDirectory($key)
108
    {
109
        list($adapter, $relativeKey) = $this->find($key);
110
        
111
        return $adapter->isDirectory($relativeKey);
112
    }
113
}
114