Completed
Push — master ( f44bce...b547a8 )
by Filipe
01:54
created

DefinitionData   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 102
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A addCall() 0 9 1
A updateLastMethod() 0 4 1
A updateLastProperty() 0 4 1
A update() 0 11 3
1
<?php
2
3
/**
4
 * This file is part of slick/di
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\Object;
11
12
use Slick\Common\Base;
13
14
/**
15
 * Definition Data for object creation
16
 *
17
 * @package Slick\Di\Definition\Object
18
 * @author  Filipe Silva <[email protected]>
19
 *
20
 * @property string $className
21
 * @property array  $arguments
22
 * @property array  $calls
23
 */
24
class DefinitionData extends Base
25
{
26
27
    const METHOD   = 'method';
28
    const PROPERTY = 'property';
29
30
    /**
31
     * @readwrite
32
     * @var string
33
     */
34
    protected $className;
35
36
    /**
37
     * @readwrite
38
     * @var array
39
     */
40
    protected $arguments = [];
41
42
    /**
43
     * @readwrite
44
     * @var array
45
     */
46
    protected $calls = [];
47
48
    /**
49
     * Definition Data
50
     *
51
     * @param string $className
52
     * @param array  $arguments
53
     * @param array  $calls
54
     */
55
    public function __construct($className, array $arguments = [], array $calls = [])
56
    {
57
        $options = [
58
            'className' => $className,
59
            'arguments' => $arguments,
60
            'calls' => $calls
61
        ];
62
        parent::__construct($options);
63
    }
64
65
    /**
66
     * Adds a call to the stack
67
     *
68
     * @param string $type
69
     * @param string $name
70
     * @param array  $arguments
71
     *
72
     * @return DefinitionData
73
     */
74
    public function addCall($type, $name, $arguments = [])
75
    {
76
        array_push($this->calls, [
77
            'type' => $type,
78
            'name' => $name,
79
            'arguments' => $arguments
80
        ]);
81
        return $this;
82
    }
83
84
    /**
85
     * Updated the arguments of the last defined method
86
     *
87
     * @param array $arguments
88
     *
89
     * @return DefinitionData
90
     */
91
    public function updateLastMethod($arguments)
92
    {
93
        return $this->update(self::METHOD, $arguments);
94
    }
95
96
    /**
97
     * Updated the arguments of the last defined property
98
     *
99
     * @param mixed $value
100
     *
101
     * @return DefinitionData
102
     */
103
    public function updateLastProperty($value)
104
    {
105
        return $this->update(self::PROPERTY, $value);
106
    }
107
108
    /**
109
     * @param string $type
110
     * @param mixed  $arguments
111
     *
112
     * @return DefinitionData
113
     */
114
    protected function update($type, $arguments)
115
    {
116
        $reversed = array_reverse($this->calls);
117
        foreach ($reversed as &$call) {
118
            if ($call['type'] == $type) {
119
                $call['arguments'] = $arguments;
120
            }
121
        }
122
        $this->calls = array_reverse($reversed);
123
        return $this;
124
    }
125
}
126