Process::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * TaskScheduler
7
 *
8
 * @author      Raffael Sahli <[email protected]>
9
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
10
 * @license     MIT https://opensource.org/licenses/MIT
11
 */
12
13
namespace TaskScheduler;
14
15
use MongoDB\BSON\ObjectId;
16
17
class Process
18
{
19
    /**
20
     * Job.
21
     *
22
     * @var array
23
     */
24
    protected $job;
25
26
    /**
27
     * Scheduler.
28
     *
29
     * @var Scheduler
30
     */
31
    protected $scheduler;
32
33
    /**
34
     * Events queue.
35
     *
36
     * @var MessageQueue
37
     */
38
    protected $events;
39
40
    /**
41
     * Initialize process.
42
     */
43 46
    public function __construct(array $job, Scheduler $scheduler, MessageQueue $events)
44
    {
45 46
        $this->job = $job;
46 46
        $this->scheduler = $scheduler;
47 46
        $this->events = $events;
48 46
    }
49
50
    /**
51
     * To array.
52
     */
53 18
    public function toArray(): array
54
    {
55 18
        return $this->job;
56
    }
57
58
    /**
59
     * Get job options.
60
     */
61 10
    public function getOptions(): array
62
    {
63 10
        return $this->job['options'];
64
    }
65
66
    /**
67
     * Get class.
68
     */
69 1
    public function getClass(): string
70
    {
71 1
        return $this->job['class'];
72
    }
73
74
    /**
75
     * Get job data.
76
     */
77 1
    public function getData()
78
    {
79 1
        return $this->job['data'];
80
    }
81
82
    /**
83
     * Get ID.
84
     */
85 32
    public function getId(): ObjectId
86
    {
87 32
        return $this->job['_id'];
88
    }
89
90
    /**
91
     * Restart job.
92
     */
93 1
    public function getWorker(): ObjectId
94
    {
95 1
        return $this->job['worker'];
96
    }
97
98
    /**
99
     * Wait for job beeing executed.
100
     */
101 2
    public function wait(): Process
102
    {
103 2
        $cursor = $this->events->getCursor([
104 2
            'job' => $this->getId(),
105
            'status' => ['$gte' => JobInterface::STATUS_DONE],
106
        ]);
107
108 2
        while (true) {
109 2
            if (null === $cursor->current()) {
110
                if ($cursor->getInnerIterator()->isDead()) {
0 ignored issues
show
Bug introduced by
The method isDead() does not exist on Iterator. It seems like you code against a sub-type of Iterator such as Helmich\MongoMock\MockCursor or Nette\Iterators\CachingIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
                if ($cursor->getInnerIterator()->/** @scrutinizer ignore-call */ isDead()) {
Loading history...
111
                    $this->events->create();
112
113
                    return $this->wait();
114
                }
115
116
                $this->events->next($cursor, function () {
117
                    $this->wait();
118
                });
119
120
                continue;
121
            }
122
123 2
            $event = $cursor->current();
124 2
            $this->events->next($cursor, function () {
125
                $this->wait();
126 2
            });
127
128 2
            $this->job['status'] = $event['status'];
129
130 2
            if (JobInterface::STATUS_FAILED === $this->job['status'] && isset($event['exception'])) {
131 1
                throw new $event['exception']['class'](
132 1
                    $event['exception']['message'],
133 1
                    $event['exception']['code']
134
                );
135
            }
136
137 1
            return $this;
138
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return TaskScheduler\Process. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
139
    }
140
141
    /**
142
     * Get status.
143
     */
144 26
    public function getStatus(): int
145
    {
146 26
        return $this->job['status'];
147
    }
148
}
149