Completed
Branch master (8f03a2)
by Kamil
04:35 queued 02:25
created

ArgumentResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 58
ccs 11
cts 17
cp 0.6471
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveArguments() 0 12 2
A resolveArgument() 0 16 3
1
<?php
2
declare(strict_types=1);
3
namespace VirCom\Behat3ZendFramework3Extension\Context\Argument;
4
5
use Behat\Behat\Context\Argument\ArgumentResolver as ArgumentResolverInterface;
6
7
use Zend\Mvc\ApplicationInterface;
8
9
use ReflectionClass;
10
11
class ArgumentResolver implements
12
    ArgumentResolverInterface
13
{
14
    /**
15
     * @var ApplicationInterface
16
     */
17
    protected $application;
18
19
    /**
20
     * @param ApplicationInterface $application
21
     */
22 2
    public function __construct(
23
        ApplicationInterface $application
24
    ) {
25 2
        $this->application = $application;
26 2
    }
27
28
    /**
29
     * @param ReflectionClass $classReflection
30
     * @param array $arguments
31
     * @return array
32
     *
33
     * @SuppressWarnings("unused")
34
     */
35 1
    public function resolveArguments(
36
        ReflectionClass $classReflection,
37
        array $arguments
38
    ): array {
39 1
        $result = [];
40
41 1
        foreach ($arguments as $key => $name) {
42 1
            $result[$key] = $this->resolveArgument($name);
43
        }
44
45
        return $result;
46
    }
47
48
    /**
49
     * @param string $argument
50
     * @return mixed
51
     */
52 1
    protected function resolveArgument(
53
        string $argument
54
    ) {
55 1
        if (substr($argument, 0, 1) !== "@") {
56
            return $argument;
57
        }
58
        
59 1
        $serviceManager = $this->application->getServiceManager();
60 1
        if($serviceManager->has(substr($argument, 1))) {
61
            return $this->application
62
                ->getServiceManager()
63
                ->get(substr($argument, 1));
64
        }
65
66
        return $argument;
67
    }
68
}
69