Cabinet::in()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
namespace Fyuze\File;
3
4
use Fyuze\File\Iterators\ExtensionIterator;
5
use GlobIterator;
6
use AppendIterator;
7
use FilesystemIterator;
8
use RecursiveDirectoryIterator;
9
use IteratorAggregate;
10
use InvalidArgumentException;
11
use RuntimeException;
12
13
/**
14
 * Directory handler for fyuze
15
 *
16
 * @package Fyuze\File
17
 */
18
class Cabinet implements IteratorAggregate
19
{
20
    /**
21
     * @var
22
     */
23
    protected $search = [];
24
25
    /**
26
     * @var
27
     */
28
    protected $index;
29
30
    /**
31
     * @var
32
     */
33
    protected $flag;
34
35
    /**
36
     * @var array
37
     */
38
    public $directory = [];
39
40
    /**
41
     * @param $pattern
42
     * @return $this
43
     */
44 1
    public function search($pattern)
45
    {
46 1
        $this->search[] = $pattern;
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * @param $flag
53
     * @return $this
54
     * @throws InvalidArgumentException
55
     */
56 4
    public function only($flag)
57
    {
58 4
        if ($flag !== 'files' && $flag !== 'folders') {
59 1
            throw new InvalidArgumentException(
60 1
                sprintf('The flag \'%s\' is not supported.', $flag)
61 1
            );
62
        }
63
64 3
        $this->flag = $flag;
65
66 3
        return $this;
67
    }
68
69
    /**
70
     * @param $directory
71
     * @return $this
72
     * @throws RuntimeException
73
     */
74 5
    public function in($directory)
75
    {
76 5
        if (file_exists($directory) === false || is_dir($directory) === false) {
77 1
            throw new RuntimeException(
78 1
                sprintf('The file %s either does not exist or is not a directory', $directory)
79 1
            );
80
        }
81
82 4
        $this->directory = new RecursiveDirectoryIterator(
0 ignored issues
show
Documentation Bug introduced by
It seems like new RecursiveDirectoryIt...temIterator::SKIP_DOTS) of type RecursiveDirectoryIterator is incompatible with the declared type array of property $directory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83 4
            $directory,
84 4
            FilesystemIterator::SKIP_DOTS
85 4
        );
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @return AppendIterator
92
     */
93 4
    public function getIterator()
94
    {
95 4
        $iterator = new AppendIterator();
96
97 4
        $directoryIterator = $this->createFilter($this->directory);
98
99 4
        foreach ($this->search as $query) {
100
101 1
            $iterator->append(
102 1
                new ExtensionIterator(
103 1
                    $this->directory,
0 ignored issues
show
Bug introduced by
$this->directory of type array is incompatible with the type Iterator expected by parameter $iterator of Fyuze\File\Iterators\Ext...Iterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                    /** @scrutinizer ignore-type */ $this->directory,
Loading history...
104 1
                    $query
105 1
                )
106 1
            );
107
        }
108
109 4
        $iterator->append($directoryIterator);
0 ignored issues
show
Bug introduced by
It seems like $directoryIterator can also be of type array; however, parameter $iterator of AppendIterator::append() does only seem to accept Iterator, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        $iterator->append(/** @scrutinizer ignore-type */ $directoryIterator);
Loading history...
110
111 4
        return $iterator;
112
    }
113
114
    /**
115
     * @param $iterator
116
     * @return mixed
117
     */
118 4
    protected function createFilter($iterator)
119
    {
120 4
        if ($this->flag) {
121
122
            /** @var \FilterIterator $class */
123 3
            $class = sprintf('Fyuze\File\Iterators\%sIterator',
124 3
                ucfirst(str_replace('s', '', $this->flag)));
125
126 3
            return new $class($iterator);
127
        }
128
129 1
        return $iterator;
130
    }
131
}
132