hasContexts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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