JsonMatcherAwareInitializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initializeContext() 0 6 3
A usesTrait() 0 8 2
1
<?php
2
3
namespace JsonSpec\Behat\Context\Initializer;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Behat\Context\Initializer\ContextInitializer;
7
use Fesor\JsonMatcher\JsonMatcherFactory;
8
use JsonSpec\Behat\Context\JsonMatcherAware;
9
10
/**
11
 * Class JsonMatcherAwareInitializer
12
 * @package JsonSpec\Behat\Context\Initializer
13
 */
14
class JsonMatcherAwareInitializer implements ContextInitializer
15
{
16
17
    /**
18
     * @var JsonMatcherFactory
19
     */
20
    private $jsonMatcherFactory;
21
22
    /**
23
     * @param JsonMatcherFactory $memoryHelper
24
     */
25
    public function __construct(JsonMatcherFactory $memoryHelper)
26
    {
27
        $this->jsonMatcherFactory = $memoryHelper;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function initializeContext(Context $context)
34
    {
35
        if ($context instanceof JsonMatcherAware || $this->usesTrait($context)) {
36
            $context->setJsonMatcher($this->jsonMatcherFactory);
37
        }
38
    }
39
40
    private function usesTrait(Context $context)
41
    {
42
        $usedTraits = class_uses($context);
43
44
        return is_array($usedTraits) && in_array(
45
            'JsonSpec\\Behat\\Context\\Traits\\JsonMatcherAwareTrait', $usedTraits
46
        );
47
    }
48
49
}
50