Completed
Push — develop ( f0edc8...54f05a )
by Filipe
22s queued 20s
created

Factory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 35
ccs 4
cts 4
cp 1
rs 10
c 2
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 4 1
A __construct() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di\Definition;
11
12
use Slick\Di\DefinitionInterface;
13
14
/**
15
 * Factory definition allows creation of object with a callable
16
 *
17
 * @package Slick\Di\Definition
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
class Factory extends AbstractDefinition implements DefinitionInterface
21
{
22
    /**
23
     * @var callable
24
     */
25
    protected $callable;
26
27
    /**
28
     * @var array
29
     */
30
    protected $parameters;
31
32
    /**
33
     * Factory needs a name and a callable
34
     *
35
     * @param callable $callable
36
     * @param array    $parameters
37
     */
38
    public function __construct(
39
        callable $callable,
40
        array $parameters = []
41
    ) {
42
        $this->callable = $callable;
43
        $this->parameters = $parameters;
44
    }
45 34
46
    /**
47 34
     * Resolves the definition into a scalar or object
48 34
     *
49 34
     * @return mixed
50
     */
51
    public function resolve()
52
    {
53
        $params = array_replace([$this->container], $this->parameters);
54
        return call_user_func_array($this->callable, $params);
55
    }
56
}
57