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

Job::decorate()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 38
cp 0
rs 8.9048
c 0
b 0
f 0
cc 5
nc 1
nop 1
crap 30
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 as TaskJobInterface;
19
use TaskScheduler\Process;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tubee\Process.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use TaskScheduler\Scheduler;
21
use Tubee\Job\JobInterface;
22
use Tubee\Log\Factory as LogFactory;
23
use Tubee\Log\LogInterface;
24
use Tubee\Process\Factory as ProcessFactory;
25
use Tubee\Process\ProcessInterface;
26
use Tubee\Resource\AbstractResource;
27
use Tubee\Resource\AttributeResolver;
28
use Tubee\ResourceNamespace\ResourceNamespaceInterface;
29
30
class Job extends AbstractResource implements JobInterface
31
{
32
    /**
33
     * Namespace.
34
     *
35
     * @var ResourceNamespaceInterface
36
     */
37
    protected $namespace;
38
39
    /**
40
     * Process factory.
41
     *
42
     * @var ProcessFactory
43
     */
44
    protected $process_factory;
45
46
    /**
47
     * Log factory.
48
     *
49
     * @var LogFactory
50
     */
51
    protected $log_factory;
52
53
    /**
54
     * Taskscheduler.
55
     *
56
     * @var Scheduler
57
     */
58
    protected $scheduler;
59
60
    /**
61
     * Data object.
62
     */
63
    public function __construct(array $resource, ResourceNamespaceInterface $namespace, Scheduler $scheduler, ProcessFactory $process_factory, LogFactory $log_factory)
64
    {
65
        $this->resource = $resource;
66
        $this->namespace = $namespace;
67
        $this->process_factory = $process_factory;
68
        $this->log_factory = $log_factory;
69
        $this->scheduler = $scheduler;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function decorate(ServerRequestInterface $request): array
76
    {
77
        $resource = $this;
78
        $scheduler = $this->scheduler;
79
80
        $result = [
81
            '_links' => [
82
                'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$this->namespace->getName())],
83
            ],
84
            'kind' => 'Job',
85
            'namespace' => $this->namespace->getName(),
86
            'data' => $this->getData(),
87
            'status' => function () use ($resource, $scheduler) {
88
                $process = iterator_to_array($scheduler->getJobs([
89
                    'data.job' => $resource->getName(),
90
                    'data.parent' => ['$exists' => false],
91
                ]));
92
93
                $process = end($process);
94
95
                if ($process === false) {
96
                    return [
97
                        'status' => false,
98
                    ];
99
                }
100
101
                $process = $process->toArray();
102
103
                return [
104
                    'status' => true,
105
                    'last_process' => [
106
                        'process' => (string) $process['_id'],
107
                        'next' => $process['options']['at'] === 0 ? null : (new DateTime('@'.(string) $process['options']['at']))->format('c'),
108
                        'started' => $process['status'] === 0 ? null : $process['started']->toDateTime()->format('c'),
109
                        'ended' => $process['status'] <= 2 ? null : $process['ended']->toDateTime()->format('c'),
110
                        'result' => TaskJobInterface::STATUS_MAP[$process['status']],
111
                        'code' => $process['status'],
112
                    ],
113
                ];
114
            },
115
        ];
116
117
        return AttributeResolver::resolve($request, $this, $result);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getLogs(array $query = [], ?int $offset = null, ?int $limit = null, ?array $sort = []): Generator
124
    {
125
        $query['context.job'] = (string) $this->getId();
126
127
        return $this->log_factory->getAll($query, $offset, $limit, $sort);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getResourceNamespace(): ResourceNamespaceInterface
134
    {
135
        return $this->namespace;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getLog(ObjectIdInterface $id): LogInterface
142
    {
143
        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...
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getProcesses(array $query = [], ?int $offset = null, ?int $limit = null, array $sort = []): Generator
150
    {
151
        $query['job'] = $this->getId();
152
153
        return $this->process_factory->getAll($this->namespace, $query, $offset, $limit, $sort);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getProcess(ObjectIdInterface $id): ProcessInterface
160
    {
161
        return $this->process_factory->getOne($this->namespace, $id);
162
    }
163
}
164