Completed
Pull Request — master (#75)
by
unknown
03:58 queued 02:22
created

ApiClientContextInitializer::initializeContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Behat WebApiExtension.
5
 *
6
 * (c) Konstantin Kudryashov <[email protected]>
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 Behat\WebApiExtension\Context\Initializer;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Behat\Context\Initializer\ContextInitializer;
16
use Behat\WebApiExtension\Context\ApiClientContextInterface;
17
use Psr\Http\Client\ClientInterface;
18
19
/**
20
 * HttpClient contexts initializer.
21
 *
22
 * Sets http client instance to the ApiClientAwareContext.
23
 *
24
 * @author Frédéric G. Marand <[email protected]>
25
 * @author Keyclic <[email protected]>
26
 */
27
class ApiClientContextInitializer implements ContextInitializer
28
{
29
    /**
30
     * @var string
31
     */
32
    private $baseUri;
33
34
    /**
35
     * @var ClientInterface
36
     */
37
    private $client;
38
39
    public function __construct(ClientInterface $client, array $config)
40
    {
41
        $this->client = $client;
42
        $this->baseUri = $config['base_uri'];
43
    }
44
45
    /**
46
     * Initializes provided context.
47
     *
48
     * @param Context $context
49
     */
50
    public function initializeContext(Context $context)
51
    {
52
        if (true === $context instanceof ApiClientContextInterface) {
53
            /* @var $context ApiClientContextInterface */
54
            $context->setClient($this->client);
55
            $context->setBaseUri($this->baseUri);
56
        }
57
    }
58
}
59