Completed
Push — dev ( 4b701d...397072 )
by James Ekow Abaka
03:37
created

FileCollection::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace ntentan\utils\filesystem;
5
6
7
use ntentan\utils\Filesystem;
8
9
class FileCollection implements \Iterator, \ArrayAccess, FileInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: delete, getPath
Loading history...
10
{
11
    private $paths;
12
    private $iteratorIndex;
13
    private $instances;
14
15
    public function __construct($paths)
16
    {
17
        $this->paths = $paths;
18
        $this->iteratorIndex = 0;
19
    }
20
21
    private function getInstance($index)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
22
    {
23
        if(!isset($this->instances[$index])) {
24
            if(is_dir($this->paths[$index])) {
25
                $this->instances[$index] = Filesystem::directory($this->paths[$index]);
26
            } else {
27
                $this->instances[$index] = Filesystem::file($this->paths[$index]);
28
            }
29
        }
30
        return $this->instances[$index];
31
    }
32
33
    public function rewind()
34
    {
35
        $this->iteratorIndex = 0;
36
    }
37
38
    public function current()
39
    {
40
        return $this->getInstance($this->iteratorIndex);
41
    }
42
43
    public function key()
44
    {
45
        return $this->iteratorIndex;
46
    }
47
48
    public function next()
49
    {
50
        $this->iteratorIndex++;
51
    }
52
53
    public function valid()
54
    {
55
        return isset($this->paths[$this->iteratorIndex]);
56
    }
57
58
    public function offsetSet($index, $path)
59
    {
60
        if(is_null()) {
61
            $this->paths[] = $path;
62
        } else {
63
            $this->paths[$index] = $path;
64
            unset($this->instances[$index]);
65
        }
66
    }
67
68
    public function offsetExists($index)
69
    {
70
        return isset($this->paths[$index]);
71
    }
72
73
    public function offsetGet($index)
74
    {
75
        return isset($this->paths[$index]) ? $this->paths[$index] : null;
76
    }
77
78
    public function offsetUnset($index)
79
    {
80
        unset($this->paths[$index]);
81
    }
82
83
    public function moveTo(string $destination): void
84
    {
85
        foreach($this as $file) {
86
            $file->moveTo($destination);
87
        }
88
    }
89
90
    public function getSize(): int
91
    {
92
        return array_reduce($this,
93
            function($carry, $item){
94
                $carry += $item->getSize();
95
            }, 0);
96
    }
97
98
    public function copyTo(string $destination): void
99
    {
100
        foreach($this as $file) {
101
            $this->copyTo($destination);
102
        }
103
    }
104
}