1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) fduch <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Date: 14.09.16 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Action\Reference; |
14
|
|
|
|
15
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Exception\ActionException; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
use ReflectionMethod; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Reference to service method |
22
|
|
|
* |
23
|
|
|
* @author fduch <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ServiceMethod implements ActionReferenceInterface, ContainerAwareInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Service id of the object which method is used as an action |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $serviceId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Method name used as action |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $methodName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Action type |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $type; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Action method reflection |
50
|
|
|
* |
51
|
|
|
* @var ReflectionMethod |
52
|
|
|
*/ |
53
|
|
|
private $reflectionMethod; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Target object object which method is used as an action |
57
|
|
|
* Used internally to invoke action |
58
|
|
|
* |
59
|
|
|
* @var object |
60
|
|
|
*/ |
61
|
|
|
private $object; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* DI Container instance |
65
|
|
|
* |
66
|
|
|
* @var ContainerInterface |
67
|
|
|
*/ |
68
|
|
|
private $container; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* ActionReference constructor. |
72
|
|
|
* |
73
|
|
|
* @param string $methodName method name |
74
|
|
|
* @param int $serviceId service id which method is used as an action |
75
|
|
|
* @param string $type action reference type |
76
|
|
|
*/ |
77
|
|
|
public function __construct($methodName, $serviceId, $type = self::TYPE_REGULAR) |
78
|
|
|
{ |
79
|
|
|
$this->methodName = $methodName; |
80
|
|
|
$this->serviceId = $serviceId; |
|
|
|
|
81
|
|
|
$this->type = $type; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns action type |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getType() |
90
|
|
|
{ |
91
|
|
|
return $this->type; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function setContainer(ContainerInterface $container = null) |
98
|
|
|
{ |
99
|
|
|
$this->container = $container; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Invokes action |
104
|
|
|
* |
105
|
|
|
* @param array $args action args |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function invoke(array $args) |
110
|
|
|
{ |
111
|
|
|
return $this->getReflectionMethod()->invokeArgs($this->getObject(), $args); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns reflection method |
116
|
|
|
* |
117
|
|
|
* @return ReflectionMethod |
118
|
|
|
*/ |
119
|
|
|
private function getReflectionMethod() |
120
|
|
|
{ |
121
|
|
|
if (!$this->reflectionMethod) { |
122
|
|
|
$this->reflectionMethod = new ReflectionMethod($this->getObject(), $this->methodName); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->reflectionMethod; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns service object |
130
|
|
|
* |
131
|
|
|
* @return object |
132
|
|
|
*/ |
133
|
|
|
private function getObject() |
134
|
|
|
{ |
135
|
|
|
if (!$this->object) { |
136
|
|
|
if (!$this->container) { |
137
|
|
|
throw ActionException::containerUnavailableForServiceMethodReference($this->serviceId); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->object = $this->container->get($this->serviceId); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->object; |
144
|
|
|
} |
145
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.