Completed
Push — dev ( 03b82e...64e4df )
by James Ekow Abaka
03:28
created

FileCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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