Completed
Push — master ( daa27a...019907 )
by Vitaly
03:25
created

checkArgumentExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.08.16 at 15:46
5
 */
6
namespace samsonframework\container\configurator;
7
8
use samsonframework\container\metadata\MethodMetadata;
9
10
/**
11
 * Method argument injection configurator.
12
 *
13
 * @author Vitaly Egorov <[email protected]>
14
 */
15
class InjectableArgumentConfigurator extends InjectableAbstractConfigurator implements MethodConfiguratorInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     *
20
     * @throws \InvalidArgumentException
21
     */
22 14
    public function toMethodMetadata(MethodMetadata $methodMetadata)
23
    {
24
        // Check for argument name input and validity
25 14
        if (!$this->checkArgumentExists($this->argumentName, $methodMetadata)) {
26 2
            throw new \InvalidArgumentException(
27
                '@InjectArgument argument "'
28 2
                . $methodMetadata->classMetadata->className . '::'
29 2
                . $methodMetadata->name . ' '
30 2
                . $this->argumentName . '" does not exists'
31
            );
32
        }
33
34
        // Check for type input
35 12
        if ($this->argumentType === null || $this->argumentType === '') {
36 1
            throw new \InvalidArgumentException(
37
                '@InjectArgument argument "'
38 1
                . $methodMetadata->classMetadata->className . '::'
39 1
                . $methodMetadata->name . ' '
40 1
                . $this->argumentName . '" type not specified'
41
            );
42
        }
43
44
        // Store dependency with fully qualified type name
45 11
        $methodMetadata->dependencies[$this->argumentName] = $this->buildFullClassName(
46 11
            $this->argumentType,
47 11
            $methodMetadata->classMetadata->nameSpace
48
        );
49 11
    }
50
51
    /**
52
     * Check method argument existence.
53
     *
54
     * @param string         $argument
55
     * @param MethodMetadata $methodMetadata
56
     *
57
     * @return bool True if @InjectArgument argument name is valid
58
     */
59 14
    protected function checkArgumentExists(string $argument, MethodMetadata $methodMetadata) : bool
60
    {
61 14
        return $argument !== null && array_key_exists($argument, $methodMetadata->parametersMetadata);
62
    }
63
}
64