Completed
Pushd7ac59...64fa5b
passed — Build
created

FactoryDefinitionHelper::parameter()   A

↳ Parent: FactoryDefinitionHelper

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 6
Code Lines 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
c 1
b 1
f 1
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace DI\Definition\Helper;
4
5
use DI\Definition\DecoratorDefinition;
6
use DI\Definition\FactoryDefinition;
7
8
/**
9
 * Helps defining how to create an instance of a class using a factory (callable).
10
 *
11
 * @author Matthieu Napoli <[email protected]>
12
 */
13
class FactoryDefinitionHelper implements DefinitionHelper
14
{
15
    /**
16
     * @var callable
17
     */
18
    private $factory;
19
20
    /**
21
     * @var string|null
22
     */
23
    private $scope;
24
25
    /**
26
     * @var bool
27
     */
28
    private $decorate;
29
30
    /**
31
     * @var array
32
     */
33
    private $parameters = [];
34
35
    /**
36
     * @param callable $factory
37
     * @param bool     $decorate Is the factory decorating a previous definition?
38
     */
39
    public function __construct($factory, $decorate = false)
40
    {
41
        $this->factory = $factory;
42
        $this->decorate = $decorate;
43
    }
44
45
    /**
46
     * Defines the scope of the entry.
47
     *
48
     * @param string $scope
49
     *
50
     * @return FactoryDefinitionHelper
51
     */
52
    public function scope($scope)
53
    {
54
        $this->scope = $scope;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $entryName Container entry name
61
     * @return FactoryDefinition
62
     */
63
    public function getDefinition($entryName)
64
    {
65
        if ($this->decorate) {
66
            return new DecoratorDefinition($entryName, $this->factory, $this->scope, $this->parameters);
67
        }
68
69
        return new FactoryDefinition($entryName, $this->factory, $this->scope, $this->parameters);
70
    }
71
72
    /**
73
     * Defines arguments to pass to the factory.
74
     *
75
     * Because factory methods do not yet support annotations or autowiring, this method
76
     * should be used to define all parameters except the ContainerInterface and RequestedEntry.
77
     *
78
     * Multiple calls can be made to the method to override individual values.
79
     *
80
     * @param string $parameter Name or index of the parameter for which the value will be given.
81
     * @param mixed  $value     Value to give to this parameter.
82
     *
83
     * @return FactoryDefinitionHelper
84
     */
85
    public function parameter($parameter, $value)
86
    {
87
        $this->parameters[$parameter] = $value;
88
89
        return $this;
90
    }
91
}
92