Completed
Pull Request — master (#112)
by Christophe
02:45
created

Gherkin::load()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.6575

Importance

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