Worker::getWorker()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mpclarkson\ResqueBundle;
4
5
/**
6
 * Class Worker
7
 * @package Mpclarkson\ResqueBundle
8
 */
9
class Worker
10
{
11
    /**
12
     * @var \Resque_Worker
13
     */
14
    protected $worker;
15
16
    /**
17
     * Worker constructor.
18
     * @param \Resque_Worker $worker
19
     */
20
    public function __construct(\Resque_Worker $worker)
21
    {
22
        $this->worker = $worker;
23
    }
24
25
    /**
26
     * @return bool
27
     */
28
    public function stop()
29
    {
30
        $parts = \explode(':', $this->getId());
31
32
        return \posix_kill($parts[1], 3);
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getId()
39
    {
40
        return (string)$this->worker;
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function getQueues()
47
    {
48
        return \array_map(function ($queue) {
49
            return new Queue($queue);
50
        }, $this->worker->queues());
51
    }
52
53
    /**
54
     * @return integer
55
     */
56
    public function getProcessedCount()
57
    {
58
        return $this->worker->getStat('processed');
59
    }
60
61
    /**
62
     * @return integer
63
     */
64
    public function getFailedCount()
65
    {
66
        return $this->worker->getStat('failed');
67
    }
68
69
    /**
70
     * @return \DateTime|null
71
     */
72
    public function getCurrentJobStart()
73
    {
74
        $job = $this->worker->job();
75
76
        if (!$job) {
77
            return NULL;
78
        }
79
80
        return new \DateTime($job['run_at']);
81
    }
82
83
    /**
84
     * @return null
85
     */
86
    public function getCurrentJob()
87
    {
88
        $job = $this->worker->job();
89
90
        if (!$job) {
91
            return NULL;
92
        }
93
94
        $job = new \Resque_Job($job['queue'], $job['payload']);
95
96
        return $job->getInstance();
97
    }
98
99
    /**
100
     * @return \Resque_Worker
101
     */
102
    public function getWorker()
103
    {
104
        return $this->worker;
105
    }
106
}
107