Completed
Branch 2.x (dc1b30)
by Akihito
02:25
created

SetterMethod   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 5
c 3
b 2
f 0
lcom 1
cbo 2
dl 0
loc 50
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptional() 0 4 1
A __construct() 0 5 1
A __invoke() 0 12 3
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Ray\Di\Exception\Unbound;
10
11
final class SetterMethod
12
{
13
    /**
14
     * @var string
15
     */
16
    private $method;
17
18
    /**
19
     * @var Arguments
20
     */
21
    private $arguments;
22
23
    /**
24
     * Is optional binding ?
25
     *
26
     * @var bool
27
     */
28
    private $isOptional = false;
29
30 21
    public function __construct(\ReflectionMethod $method, Name $name)
31
    {
32 21
        $this->method = $method->name;
33 21
        $this->arguments = new Arguments($method, $name);
34 21
    }
35
36 8
    public function setOptional()
37
    {
38 8
        $this->isOptional = true;
39 8
    }
40
41
    /**
42
     * @param object    $instance
43
     * @param Container $container
44
     *
45
     * @throws Unbound
46
     * @throws \Exception
47
     */
48 19
    public function __invoke($instance, Container $container)
49
    {
50
        try {
51 19
            $parameters = $this->arguments->inject($container);
52 19
        } catch (Unbound $e) {
53 6
            if ($this->isOptional) {
54 5
                return;
55
            }
56 1
            throw $e;
57
        }
58 18
        call_user_func_array([$instance, $this->method], $parameters);
59 18
    }
60
}
61