Completed
Push — master ( 531aca...edc713 )
by Kamil
04:19
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ContextServiceExtension package.
5
 *
6
 * (c) FriendsOfBehat
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FriendsOfBehat\ContextServiceExtension\Context\Environment;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Behat\Context\Exception\ContextNotFoundException;
16
use Behat\Testwork\Call\Callee;
17
use Behat\Testwork\Suite\Suite;
18
use FriendsOfBehat\ContextServiceExtension\Context\Environment\Handler\ContextServiceEnvironmentHandler;
19
20
/**
21
 * @internal
22
 *
23
 * @see ContextServiceEnvironmentHandler
24
 */
25
final class InitialisedContextServiceEnvironment implements ContextServiceEnvironment
26
{
27
    /**
28
     * @var Suite
29
     */
30
    private $suite;
31
32
    /**
33
     * @var Context[]
34
     */
35
    private $contexts = [];
36
37
    /**
38
     * @param Suite $suite
39
     */
40
    public function __construct(Suite $suite)
41
    {
42
        $this->suite = $suite;
43
    }
44
45
    /**
46
     * @param Context $context
47
     */
48
    public function registerContext(Context $context)
49
    {
50
        $this->contexts[get_class($context)] = $context;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getSuite()
57
    {
58
        return $this->suite;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function bindCallee(Callee $callee)
65
    {
66
        $callable = $callee->getCallable();
67
68
        if ($callee->isAnInstanceMethod()) {
69
            return [$this->getContext($callable[0]), $callable[1]];
70
        }
71
72
        return $callable;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function hasContexts()
79
    {
80
        return count($this->contexts) > 0;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getContextClasses()
87
    {
88
        return array_keys($this->contexts);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function hasContextClass($class)
95
    {
96
        return isset($this->contexts[$class]);
97
    }
98
99
    /**
100
     * @param string $class
101
     *
102
     * @return Context
103
     *
104
     * @throws ContextNotFoundException
105
     */
106
    private function getContext($class)
107
    {
108
        if (!isset($this->contexts[$class])) {
109
            throw new ContextNotFoundException(sprintf(
110
                '`%s` context is not found in the suite environment. Have you registered it?',
111
                $class
112
            ), $class);
113
        }
114
115
        return $this->contexts[$class];
116
    }
117
}
118