CallableDefinition   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
cbo 2
dl 0
loc 69
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A invoke() 0 15 3
A getCallable() 0 4 1
A setCallable() 0 4 1
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 *
26
 * @category  DependencyInjection
27
 * @package   Fwk\Di
28
 * @author    Julien Ballestracci <[email protected]>
29
 * @copyright 2011-2014 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://www.nitronet.org/fwk
32
 */
33
namespace Fwk\Di\Definitions;
34
35
use Fwk\Di\Container;
36
use Fwk\Di\DefinitionInterface;
37
use Fwk\Di\Exception;
38
use Fwk\Di\Exceptions\InvalidCallableDefinitionException;
39
use Fwk\Di\Exceptions;
40
41
/**
42
 * CallableDefinition
43
 * 
44
 * Represents a Definition that require a function (callable) to be executed
45
 * to obtain its value.
46
 *
47
 * @category Definition
48
 * @package  Fwk\Di
49
 * @author   Julien Ballestracci <[email protected]>
50
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
51
 * @link     http://www.nitronet.org/fwk
52
 */
53
class CallableDefinition extends AbstractArgsDefinition implements DefinitionInterface
54
{
55
    /**
56
     * The callable function
57
     * @var callable
58
     */
59
    protected $callable;
60
    
61
    /**
62
     * Constructor
63
     * 
64
     * @param callable     $callable  The function to be called
65
     * @param array<mixed> $arguments List of arguments
66
     * 
67
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
68
     */
69 30
    public function __construct($callable, array $arguments = array())
70
    {
71 30
        $this->callable     = $callable;
72 30
        $this->arguments    = $arguments;
73 30
    }
74
    
75
    /**
76
     * Calls $this->callable and return its value
77
     * 
78
     * @param Container   $container The Di Container
79
     * @param null|string $name      Name of the definition (if any)
80
     * 
81
     * @return mixed
82
     * @throws Exceptions\InvalidCallableDefinitionException
83
     */
84 14
    public function invoke(Container $container, $name = null)
85
    {
86 14
        if (!is_callable($this->callable)) {
87 1
            throw new InvalidCallableDefinitionException($this->callable, $name);
88
        }
89
        
90 13
        $args = array();
91
        try {
92 13
            $args = $this->getConstructorArguments($container, $name);
93 2
        } catch(Exception $exp) {
94 2
            throw new InvalidCallableDefinitionException($this->callable, $name, $exp);
95
        }
96
        
97 11
        return call_user_func_array($this->callable, $args);
98
    }
99
    
100
    /**
101
     * Returns the callable function
102
     * 
103
     * @return callable
104
     */
105 4
    public function getCallable()
106
    {
107 4
        return $this->callable;
108
    }
109
    
110
    /**
111
     * Defines the callable function
112
     * 
113
     * @param callable $callable The callable function
114
     * 
115
     * @return void
116
     */
117 6
    public function setCallable($callable)
118
    {
119 6
        $this->callable = $callable;
120
    }
121
}