|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* tubee.io |
|
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 DateTime; |
|
15
|
|
|
use Generator; |
|
16
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
|
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
18
|
|
|
use TaskScheduler\JobInterface; |
|
19
|
|
|
use Tubee\Log\Factory as LogFactory; |
|
20
|
|
|
use Tubee\Log\LogInterface; |
|
21
|
|
|
use Tubee\Process\ProcessInterface; |
|
22
|
|
|
use Tubee\Resource\AbstractResource; |
|
23
|
|
|
use Tubee\Resource\AttributeResolver; |
|
24
|
|
|
use Tubee\ResourceNamespace\ResourceNamespaceInterface; |
|
25
|
|
|
|
|
26
|
|
|
class Process extends AbstractResource implements ProcessInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Namespace. |
|
30
|
|
|
* |
|
31
|
|
|
* @var ResourceNamespace |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $namespace; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Log factory. |
|
37
|
|
|
* |
|
38
|
|
|
* @var LogFactory |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $log_factory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Process. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $resource, ResourceNamespaceInterface $namespace, LogFactory $log_factory) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->resource = $resource; |
|
48
|
|
|
$this->namespace = $namespace; |
|
|
|
|
|
|
49
|
|
|
$this->log_factory = $log_factory; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function decorate(ServerRequestInterface $request): array |
|
56
|
|
|
{ |
|
57
|
|
|
$data = $this->getData(); |
|
58
|
|
|
$parent = isset($data['parent']) ? (string) $data['parent'] : null; |
|
59
|
|
|
$job = isset($data['job']) ? $data['job'] : null; |
|
60
|
|
|
unset($data['parent'], $data['namespace'], $data['job']); |
|
61
|
|
|
|
|
62
|
|
|
$result = [ |
|
63
|
|
|
'_links' => [ |
|
64
|
|
|
'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$this->namespace->getName())], |
|
65
|
|
|
], |
|
66
|
|
|
'kind' => 'Process', |
|
67
|
|
|
'namespace' => $this->namespace->getName(), |
|
68
|
|
|
'changed' => $this->getCreated()->toDateTime()->format('c'), |
|
69
|
|
|
'data' => $data, |
|
70
|
|
|
'status' => [ |
|
71
|
|
|
'job' => $job, |
|
72
|
|
|
'parent' => $parent, |
|
73
|
|
|
'next' => $this->resource['options']['at'] === 0 ? null : (new DateTime('@'.(string) $this->resource['options']['at']))->format('c'), |
|
74
|
|
|
'started' => $this->resource['status'] === 0 ? null : $this->resource['started']->toDateTime()->format('c'), |
|
75
|
|
|
'ended' => $this->resource['status'] <= 2 ? null : $this->resource['ended']->toDateTime()->format('c'), |
|
76
|
|
|
'result' => JobInterface::STATUS_MAP[$this->resource['status']], |
|
77
|
|
|
'code' => $this->resource['status'], |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
return AttributeResolver::resolve($request, $this, $result); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getResourceNamespace(): ResourceNamespaceInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->namespace; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getLogs(array $query = [], ?int $offset = null, ?int $limit = null, ?array $sort = []): Generator |
|
96
|
|
|
{ |
|
97
|
|
|
$query['$or'][] = ['context.process' => (string) $this->getId()]; |
|
98
|
|
|
$query['$or'][] = ['context.parent' => (string) $this->getId()]; |
|
99
|
|
|
|
|
100
|
|
|
return $this->log_factory->getAll($query, $offset, $limit, $sort); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getLog(ObjectIdInterface $id): LogInterface |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->log_factory->getOne($id); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.