|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* tubee |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tubee; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
use Tubee\AttributeMap\AttributeMapInterface; |
|
17
|
|
|
use Tubee\Endpoint\EndpointInterface; |
|
18
|
|
|
use Tubee\Resource\AbstractResource; |
|
19
|
|
|
use Tubee\Resource\AttributeResolver; |
|
20
|
|
|
use Tubee\V8\Engine as V8Engine; |
|
21
|
|
|
use Tubee\Workflow\Map; |
|
22
|
|
|
use Tubee\Workflow\WorkflowInterface; |
|
23
|
|
|
use V8Js; |
|
24
|
|
|
|
|
25
|
|
|
class Workflow extends AbstractResource implements WorkflowInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Kind. |
|
29
|
|
|
*/ |
|
30
|
|
|
public const KIND = 'Workflow'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Workflow name. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Endpoint. |
|
41
|
|
|
* |
|
42
|
|
|
* @var EndpointInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $endpoint; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Logger. |
|
48
|
|
|
* |
|
49
|
|
|
* @var LoggerInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $logger; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Attribute map. |
|
55
|
|
|
* |
|
56
|
|
|
* @var AttributeMap |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $attribute_map; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Condition. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $ensure = WorkflowInterface::ENSURE_EXISTS; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Condiditon. |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $condition; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* V8 engine. |
|
74
|
|
|
* |
|
75
|
|
|
* @var V8Engine |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $v8; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Initialize. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(string $name, string $ensure, V8Engine $v8, AttributeMapInterface $attribute_map, EndpointInterface $endpoint, LoggerInterface $logger, array $resource = []) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->name = $name; |
|
85
|
|
|
$this->ensure = $ensure; |
|
86
|
|
|
$this->v8 = $v8; |
|
87
|
|
|
$this->attribute_map = $attribute_map; |
|
|
|
|
|
|
88
|
|
|
$this->endpoint = $endpoint; |
|
89
|
|
|
$this->logger = $logger; |
|
90
|
|
|
$this->resource = $resource; |
|
91
|
|
|
$this->condition = $resource['data']['condition']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getIdentifier(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->endpoint->getIdentifier().'::'.$this->name; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get ensure. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getEnsure(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->ensure; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function decorate(ServerRequestInterface $request): array |
|
114
|
|
|
{ |
|
115
|
|
|
$namespace = $this->endpoint->getCollection()->getResourceNamespace()->getName(); |
|
116
|
|
|
$collection = $this->endpoint->getCollection()->getName(); |
|
117
|
|
|
$endpoint = $this->endpoint->getName(); |
|
118
|
|
|
|
|
119
|
|
|
$resource = [ |
|
120
|
|
|
'_links' => [ |
|
121
|
|
|
'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace)], |
|
122
|
|
|
'collection' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace.'/collections/'.$collection)], |
|
123
|
|
|
'endpoint' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace.'/collections/'.$collection.'/endpoints/'.$endpoint)], |
|
124
|
|
|
], |
|
125
|
|
|
'namespace' => $namespace, |
|
126
|
|
|
'collection' => $collection, |
|
127
|
|
|
'endpoint' => $endpoint, |
|
128
|
|
|
'data' => $this->getData(), |
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
|
|
return AttributeResolver::resolve($request, $this, $resource); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getEndpoint(): EndpointInterface |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->endpoint; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getAttributeMap(): AttributeMapInterface |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->attribute_map; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* check condition. |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function checkCondition(array $object): bool |
|
154
|
|
|
{ |
|
155
|
|
|
if ($this->condition === null) { |
|
156
|
|
|
$this->logger->debug('no workflow condition set for workflow ['.$this->getIdentifier().']', [ |
|
157
|
|
|
'category' => get_class($this), |
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
return true; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$this->logger->debug('execute workflow condiditon ['.$this->condition.'] for workflow ['.$this->getIdentifier().']', [ |
|
164
|
|
|
'category' => get_class($this), |
|
165
|
|
|
]); |
|
166
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
$this->v8->object = $object; |
|
169
|
|
|
$this->v8->executeString($this->condition, '', V8Js::FLAG_FORCE_ARRAY); |
|
170
|
|
|
|
|
171
|
|
|
return (bool) $this->v8->getLastResult(); |
|
172
|
|
|
} catch (\Exception $e) { |
|
173
|
|
|
$this->logger->error('failed execute workflow condition ['.$this->condition.']', [ |
|
174
|
|
|
'category' => get_class($this), |
|
175
|
|
|
'exception' => $e, |
|
176
|
|
|
]); |
|
177
|
|
|
|
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.