Completed
Push — dev ( 397072...03b82e )
by James Ekow Abaka
01:33
created

FileCollection::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
namespace ntentan\utils\filesystem;
5
6
7
use ntentan\utils\exceptions\FileNotFoundException;
8
use ntentan\utils\exceptions\FilesystemException;
9
use ntentan\utils\Filesystem;
10
use test\base;
11
12
class FileCollection implements \Iterator, \ArrayAccess, FileInterface
13
{
14
    private $paths;
15
    private $iteratorIndex;
16
    private $instances;
17
18 2
    public function __construct($paths)
19
    {
20 2
        $this->paths = $paths;
21 2
        $this->iteratorIndex = 0;
22 2
    }
23
24 1
    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...
25
    {
26 1
        if(!isset($this->instances[$index])) {
27 1
            if(is_dir($this->paths[$index])) {
28 1
                $this->instances[$index] = Filesystem::directory($this->paths[$index]);
29
            } else {
30 1
                $this->instances[$index] = Filesystem::file($this->paths[$index]);
31
            }
32
        }
33 1
        return $this->instances[$index];
34
    }
35
36 1
    public function rewind()
37
    {
38 1
        $this->iteratorIndex = 0;
39 1
    }
40
41 1
    public function current()
42
    {
43 1
        return $this->getInstance($this->iteratorIndex);
44
    }
45
46
    public function key()
47
    {
48
        return $this->iteratorIndex;
49
    }
50
51 1
    public function next()
52
    {
53 1
        $this->iteratorIndex++;
54 1
    }
55
56 1
    public function valid()
57
    {
58 1
        return isset($this->paths[$this->iteratorIndex]);
59
    }
60
61
    public function offsetSet($index, $path)
62
    {
63
        if(is_null()) {
64
            $this->paths[] = $path;
65
        } else {
66
            $this->paths[$index] = $path;
67
            unset($this->instances[$index]);
68
        }
69
    }
70
71
    public function offsetExists($index)
72
    {
73
        return isset($this->paths[$index]);
74
    }
75
76
    public function offsetGet($index)
77
    {
78
        return isset($this->paths[$index]) ? $this->paths[$index] : null;
79
    }
80
81
    public function offsetUnset($index)
82
    {
83
        unset($this->paths[$index]);
84
    }
85
86
    public function moveTo(string $destination): void
87
    {
88
        foreach($this as $file) {
89
            $file->moveTo($destination . DIRECTORY_SEPARATOR . basename($file));
90
        }
91
    }
92
93
    public function copyTo(string $destination): void
94
    {
95
        foreach($this as $file) {
96
            $file->copyTo($destination . DIRECTORY_SEPARATOR . basename($file));
97
        }
98
    }
99
100
    public function getSize(): int
101
    {
102
        return array_reduce($this,
103
            function($carry, $item){
104
                $carry += $item->getSize();
105
            }, 0);
106
    }
107
108 1
    public function delete(): void
109
    {
110 1
        foreach($this as $file) {
111 1
            $file->delete();
112
        }
113 1
    }
114
115
    public function getPath(): string
116
    {
117
        return array_reduce($this->paths,
118
            function($carry, $path) {
119
                $carry .= escapeshellarg($path) . " ";
120
            }, "");
121
    }
122
}