Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Process   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 4
dl 0
loc 85
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B decorate() 0 28 6
A getResourceNamespace() 0 4 1
A getLogs() 0 7 1
A getLog() 0 4 1
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;
0 ignored issues
show
Documentation Bug introduced by
$namespace is of type object<Tubee\ResourceNam...urceNamespaceInterface>, but the property $namespace was declared to be of type object<Tubee\ResourceNamespace>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

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.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
The call to getOne() misses a required argument $log.

This check looks for function calls that miss required arguments.

Loading history...
109
    }
110
}
111