FinderByConfig::setStrictBoolOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Schnittstabil\FinderByConfig;
4
5
use Schnittstabil\Get;
6
use Symfony\Component\Finder\Finder;
7
8
class FinderByConfig
9
{
10
    protected function createFromArray(array $paths)
11
    {
12
        $finder = new Finder();
13
        $files = [];
14
        $dirs = [];
15
16
        foreach ($paths as $path) {
17
            if (is_file($path)) {
18
                $files[] = $path;
19
                continue;
20
            }
21
22
            $dirs[] = $path;
23
        }
24
25
        return $finder->append($files)->in($dirs);
26
    }
27
28
    protected function setWithoutValueOptions(Finder $finder, $config)
29
    {
30
        foreach ([
31
            'directories',
32
            'files',
33
            'sortByName',
34
            'sortByType',
35
            'sortByAccessedTime',
36
            'sortByChangedTime',
37
            'sortByModifiedTime',
38
            'followLinks',
39
        ] as $setter) {
40
            if (filter_var(Get\getValue($setter, $config), FILTER_VALIDATE_BOOLEAN)) {
0 ignored issues
show
Deprecated Code introduced by
The function Schnittstabil\Get\getValue() has been deprecated with message: Use `Schnittstabil\Get\value()` instead of `Schnittstabil\Get\getValue()`

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
41
                $finder->$setter();
42
            }
43
        }
44
    }
45
46
    protected function setArrayOptions(Finder $finder, $config)
47
    {
48
        foreach ([
49
            'depth',
50
            'date',
51
            'name',
52
            'notName',
53
            'contains',
54
            'notContains',
55
            'path',
56
            'notPath',
57
            'size',
58
            'exclude',
59
            'addVCSPattern',
60
        ] as $setter) {
61
            foreach ((array) Get\getValue($setter, $config, []) as $value) {
0 ignored issues
show
Deprecated Code introduced by
The function Schnittstabil\Get\getValue() has been deprecated with message: Use `Schnittstabil\Get\value()` instead of `Schnittstabil\Get\getValue()`

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
62
                $finder->$setter($value);
63
            }
64
        }
65
    }
66
67
    protected function setStrictBoolOptions(Finder $finder, $config)
68
    {
69
        foreach ([
70
            'ignoreDotFiles',
71
            'ignoreVCS',
72
            'ignoreUnreadableDirs',
73
        ] as $setter) {
74
            $value = Get\getValue($setter, $config);
0 ignored issues
show
Deprecated Code introduced by
The function Schnittstabil\Get\getValue() has been deprecated with message: Use `Schnittstabil\Get\value()` instead of `Schnittstabil\Get\getValue()`

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
75
            if ($value !== null) {
76
                $finder->$setter(filter_var($value, FILTER_VALIDATE_BOOLEAN));
77
            }
78
        }
79
    }
80
81
    protected function createFromConfig($config)
82
    {
83
        $finder = $this->createFromArray((array) Get\getValue('in', $config));
0 ignored issues
show
Deprecated Code introduced by
The function Schnittstabil\Get\getValue() has been deprecated with message: Use `Schnittstabil\Get\value()` instead of `Schnittstabil\Get\getValue()`

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
84
        $this->setWithoutValueOptions($finder, $config);
85
        $this->setArrayOptions($finder, $config);
86
        $this->setStrictBoolOptions($finder, $config);
87
88
        return $finder;
89
    }
90
91
    public function __invoke($config)
92
    {
93
        if (Get\getValue('in', $config) === null) {
0 ignored issues
show
Deprecated Code introduced by
The function Schnittstabil\Get\getValue() has been deprecated with message: Use `Schnittstabil\Get\value()` instead of `Schnittstabil\Get\getValue()`

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
94
            return $this->createFromArray((array) $config);
95
        }
96
97
        return $this->createFromConfig($config);
98
    }
99
100
    public static function createFinder($config)
101
    {
102
        $finderByConfig = new static();
103
104
        return $finderByConfig($config);
105
    }
106
}
107