DirectoryLoader::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Loader;
12
13
use Behat\Gherkin\Gherkin;
14
use Behat\Gherkin\Node\FeatureNode;
15
use RecursiveDirectoryIterator;
16
use RecursiveIteratorIterator;
17
18
/**
19
 * Directory contents loader.
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class DirectoryLoader extends AbstractFileLoader
24
{
25
    protected $gherkin;
26
27
    /**
28
     * Initializes loader.
29
     *
30
     * @param Gherkin $gherkin Gherkin manager
31
     */
32 4
    public function __construct(Gherkin $gherkin)
33
    {
34 4
        $this->gherkin = $gherkin;
35 4
    }
36
37
    /**
38
     * Checks if current loader supports provided resource.
39
     *
40
     * @param mixed $path Resource to load
41
     *
42
     * @return Boolean
43
     */
44 1
    public function supports($path)
45
    {
46 1
        return is_string($path)
47 1
        && is_dir($this->findAbsolutePath($path));
48
    }
49
50
    /**
51
     * Loads features from provided resource.
52
     *
53
     * @param string $path Resource to load
54
     *
55
     * @return FeatureNode[]
56
     */
57 3
    public function load($path)
58
    {
59 3
        $path = $this->findAbsolutePath($path);
60
61 3
        $iterator = new RecursiveIteratorIterator(
62 3
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS)
63
        );
64 3
        $paths = array_map('strval', iterator_to_array($iterator));
65 3
        uasort($paths, 'strnatcasecmp');
66
67 3
        $features = array();
68
69 3
        foreach ($paths as $path) {
70 3
            $path = (string) $path;
71 3
            $loader = $this->gherkin->resolveLoader($path);
72
73 3
            if (null !== $loader) {
74 1
                $features = array_merge($features, $loader->load($path));
75
            }
76
        }
77
78 3
        return $features;
79
    }
80
}
81