Completed
Push — develop ( cba78f...09bcdb )
by Nicolas
12:47
created

Job::timeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace devtransition\jobbers;
4
5
use devtransition\Jobbers\exception\JobPendingException;
6
7
class Job
8
{
9
10
    private $_proxy;
11
12
    private $_id;
13
    private $_timeout;
14
15
    private $_result;
16
17
    /**
18
     * @param $proxy Proxy
19
     */
20
    public function __construct(&$proxy)
21
    {
22
        $this->_proxy = $proxy;
23
24
        $this->_applyOptions();
25
26
        /*
27
         * generate 16 hex random id
28
         */
29
        if (!$this->_id) {
30
            $this->_id = bin2hex(openssl_random_pseudo_bytes(8));
31
        }
32
33
        /*
34
         * set dummy result
35
         */
36
        $this->_result = new JobPendingException('not started yet');
37
    }
38
39
    public function id($id)
40
    {
41
        $this->_id = $id;
42
        return clone($this);
43
    }
44
45
    public function timeout($seconds)
46
    {
47
        $this->_timeout = $seconds;
48
        return clone($this);
49
    }
50
51
    public function &run()
52
    {
53
        $this->_result = call_user_func_array([$this->_proxy->getClass(), $this->_proxy->getMethod()],
54
            $this->_proxy->getArgs());
55
        return $this->_result;
56
    }
57
58
    public function &attach(Pool $pool)
59
    {
60
        $pool->add($this);
61
        return $this->_result;
62
    }
63
64
    public function getId()
65
    {
66
        return $this->_id;
67
    }
68
69
    public function &getResult()
70
    {
71
        return $this->_result;
72
    }
73
74
    private function _applyOptions()
75
    {
76
        $defaults = $this->_proxy->getDefaults();
77
78
        // Timeout
79
        if (isset($defaults['timeout'])) {
80
            $this->_timeout = $defaults['timeout'];
81
        }
82
83
        /* TODO: Add Priority? */
84
    }
85
}
86