Passed
Branch dev (b82c9a)
by Raffael
05:11
created

Process::toArray()   A

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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
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-2018 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 31
    public function __construct(array $job, Scheduler $scheduler, MessageQueue $events)
44
    {
45 31
        $this->job = $job;
46 31
        $this->scheduler = $scheduler;
47 31
        $this->events = $events;
48 31
    }
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 5
    public function getOptions(): array
62
    {
63 5
        return $this->job;
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
    public function getData()
78
    {
79
        return $this->job['data'];
80
    }
81
82
    /**
83
     * Get ID.
84
     */
85 12
    public function getId(): ObjectId
86
    {
87 12
        return $this->job['_id'];
88
    }
89
90
    /**
91
     * Restart job.
92
     */
93
    public function restart(): Process
94
    {
95
    }
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...
96
97
    /**
98
     * Kill running process.
99
     */
100
    public function kill(): bool
101
    {
102
    }
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 boolean. 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...
103
104
    /**
105
     * Wait for job beeing executed.
106
     */
107 2
    public function wait(): Process
108
    {
109 2
        $cursor = $this->events->getCursor([
110 2
            'job' => $this->getId(),
111
            'status' => ['$gte' => JobInterface::STATUS_DONE],
112
        ]);
113
114 2
        while (true) {
115 2
            if (null === $cursor->current()) {
116
                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

116
                if ($cursor->getInnerIterator()->/** @scrutinizer ignore-call */ isDead()) {
Loading history...
117
                    $this->events->create();
118
119
                    return $this->wait();
120
                }
121
122
                $this->events->next($cursor, function () {
123
                    $this->wait();
124
                });
125
126
                continue;
127
            }
128
129 2
            $event = $cursor->current();
130 2
            $this->events->next($cursor, function () {
131
                $this->wait();
132 2
            });
133
134 2
            $this->job['status'] = $event['status'];
135
136 2
            if (JobInterface::STATUS_FAILED === $this->job['status']) {
137 1
                $result = unserialize($event['data']);
138 1
                if ($result instanceof \Exception) {
139 1
                    throw $result;
140
                }
141
            }
142
143 1
            return $this;
144
        }
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...
145
    }
146
147
    /**
148
     * Get status.
149
     */
150 12
    public function getStatus(): int
151
    {
152 12
        return $this->job['status'];
153
    }
154
}
155