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

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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