EnvironmentReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Behat;
6
7
// Interface for environments.
8
use Behat\Testwork\Environment\Environment;
9
// Interface for environments with contexts.
10
use Behat\Behat\Context\Environment\ContextEnvironment;
11
// Interface for uninitialized environments with contexts.
12
use Behat\Behat\Context\Environment\UninitializedContextEnvironment;
13
// Interface for environment readers.
14
use Behat\Testwork\Environment\Reader\EnvironmentReader as EnvironmentReaderInterface;
15
16
/**
17
 * Class EnvironmentReader.
18
 *
19
 * @package Behat
20
 */
21
final class EnvironmentReader implements EnvironmentReaderInterface
22
{
23
    /**
24
     * Path to extension sources.
25
     *
26
     * @var string
27
     */
28
    private $path = '';
29
    /**
30
     * Namespace of the extension.
31
     *
32
     * @var string
33
     */
34
    private $namespace = '';
35
36
    /**
37
     * EnvironmentReader constructor.
38
     *
39
     * @param string $path
40
     * @param string $namespace
41
     */
42 4
    public function __construct($path, $namespace)
43
    {
44 4
        $this->path = $path;
45 4
        $this->namespace = $namespace;
46 4
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function supportsEnvironment(Environment $environment)
52
    {
53 4
        return $environment instanceof ContextEnvironment;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    public function readEnvironmentCallees(Environment $environment)
60
    {
61 4
        if ($environment instanceof UninitializedContextEnvironment) {
62
            // Read all extension contexts.
63 4
            foreach (new \RecursiveIteratorIterator(
64 4
                new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS)
65 4
            ) as $path => $object) {
66 4
                $basename = basename($path);
67
68
                // Allow names which not starts from "Raw" and ends by "Context.php".
69 4
                if (strrpos($basename, 'Context.php') !== false && strpos($basename, 'Raw') !== 0) {
70 4
                    $class = strtr($path, [$this->path => $this->namespace, '.php' => '', '/' => '\\']);
71
72 4
                    if (!$environment->hasContextClass($class)) {
73 4
                        $environment->registerContextClass($class);
74 4
                    }
75 4
                }
76 4
            }
77 4
        }
78
79
        // Just return an empty array and allow Behat to scan context classes for callees.
80 4
        return [];
81
    }
82
}
83