Gherkin::load()   B
last analyzed

Complexity

Conditions 8
Paths 15

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.512

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 16
cts 20
cp 0.8
rs 8.1315
c 0
b 0
f 0
cc 8
nc 15
nop 2
crap 8.512
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;
12
13
use Behat\Gherkin\Filter\FeatureFilterInterface;
14
use Behat\Gherkin\Filter\LineFilter;
15
use Behat\Gherkin\Filter\LineRangeFilter;
16
use Behat\Gherkin\Loader\FileLoaderInterface;
17
use Behat\Gherkin\Loader\LoaderInterface;
18
19
/**
20
 * Gherkin manager.
21
 *
22
 * @author Konstantin Kudryashov <[email protected]>
23
 */
24
class Gherkin
25
{
26
    const VERSION = '4.6.2';
27
28
    /**
29
     * @var LoaderInterface[]
30
     */
31
    protected $loaders = array();
32
    /**
33
     * @var FeatureFilterInterface[]
34
     */
35
    protected $filters = array();
36
37
    /**
38
     * Adds loader to manager.
39
     *
40
     * @param LoaderInterface $loader Feature loader
41
     */
42 4
    public function addLoader(LoaderInterface $loader)
43
    {
44 4
        $this->loaders[] = $loader;
45 4
    }
46
47
    /**
48
     * Adds filter to manager.
49
     *
50
     * @param FeatureFilterInterface $filter Feature filter
51
     */
52 3
    public function addFilter(FeatureFilterInterface $filter)
53
    {
54 3
        $this->filters[] = $filter;
55 3
    }
56
57
    /**
58
     * Sets filters to the parser.
59
     *
60
     * @param FeatureFilterInterface[] $filters
61
     */
62 1
    public function setFilters(array $filters)
63
    {
64 1
        $this->filters = array();
65 1
        array_map(array($this, 'addFilter'), $filters);
66 1
    }
67
68
    /**
69
     * Sets base features path.
70
     *
71
     * @param string $path Loaders base path
72
     */
73 1
    public function setBasePath($path)
74
    {
75 1
        foreach ($this->loaders as $loader) {
76 1
            if ($loader instanceof FileLoaderInterface) {
77 1
                $loader->setBasePath($path);
78
            }
79
        }
80 1
    }
81
82
    /**
83
     * Loads & filters resource with added loaders.
84
     *
85
     * @param mixed                    $resource Resource to load
86
     * @param FeatureFilterInterface[] $filters  Additional filters
87
     *
88
     * @return array
89
     */
90 4
    public function load($resource, array $filters = array())
91
    {
92 4
        $filters = array_merge($this->filters, $filters);
93
94 4
        $matches = array();
95 4
        if (preg_match('/^(.*)\:(\d+)-(\d+|\*)$/', $resource, $matches)) {
96
            $resource = $matches[1];
97
            $filters[] = new LineRangeFilter($matches[2], $matches[3]);
98 4
        } elseif (preg_match('/^(.*)\:(\d+)$/', $resource, $matches)) {
99
            $resource = $matches[1];
100
            $filters[] = new LineFilter($matches[2]);
101
        }
102
103 4
        $loader = $this->resolveLoader($resource);
104
105 4
        if (null === $loader) {
106 1
            return array();
107
        }
108
109 3
        $features = array();
110 3
        foreach ($loader->load($resource) as $feature) {
111 3
            foreach ($filters as $filter) {
112 2
                $feature = $filter->filterFeature($feature);
113
114 2
                if (!$feature->hasScenarios() && !$filter->isFeatureMatch($feature)) {
115 1
                    continue 2;
116
                }
117
            }
118
119 2
            $features[] = $feature;
120
        }
121
122 3
        return $features;
123
    }
124
125
    /**
126
     * Resolves loader by resource.
127
     *
128
     * @param mixed $resource Resource to load
129
     *
130
     * @return LoaderInterface
131
     */
132 4
    public function resolveLoader($resource)
133
    {
134 4
        foreach ($this->loaders as $loader) {
135 3
            if ($loader->supports($resource)) {
136 3
                return $loader;
137
            }
138
        }
139
140 1
        return null;
141
    }
142
}
143