Completed
Push — master ( e67b12...6841f1 )
by Ilias
05:15
created

src/Job/Job.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
namespace Beanie\Job;
5
6
7
use Beanie\Beanie;
8
use Beanie\Command\CommandFactory;
9
use Beanie\Command\CommandInterface;
10
use Beanie\Command\Response;
11
use Beanie\Server\Server;
12
13
class Job implements \JsonSerializable
14
{
15
    const STATE_UNKNOWN = null;
16
    const STATE_RELEASED = 'RELEASED';
17
    const STATE_RESERVED = 'RESERVED';
18
    const STATE_BURIED = 'BURIED';
19
    const STATE_DELETED = 'DELETED';
20
21
    /** @var int */
22
    protected $id;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $id. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
23
24
    /** @var mixed */
25
    protected $data;
26
27
    /** @var Server */
28
    protected $server;
29
30
    /** @var null|string */
31
    protected $state;
32
33
    /** @var CommandFactory */
34
    protected $commandFactory;
35
36
    /**
37
     * @param int $id
38
     * @param mixed $data
39
     * @param Server $server
40
     * @param null|string $state
41
     */
42 31
    public function __construct($id, $data, Server $server, $state = self::STATE_UNKNOWN)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $id. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
43
    {
44 31
        $this->id = (int) $id;
45 31
        $this->data = $data;
46 31
        $this->server = $server;
47 31
        $this->state = $state;
48 31
        $this->commandFactory = CommandFactory::instance();
49 31
    }
50
51
    /**
52
     * @return null|string
53
     */
54 26
    public function getState()
55
    {
56 26
        return $this->state;
57
    }
58
59
    /**
60
     * @return int
61
     */
62 17
    public function getId()
63
    {
64 17
        return $this->id;
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70 17
    public function getData()
71
    {
72 17
        return $this->data;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78 1
    public function kick()
79
    {
80 1
        $this->executeCommand(CommandInterface::COMMAND_KICK_JOB);
81 1
        return $this;
82
    }
83
84
    /**
85
     * @param int $priority
86
     * @param int $delay
87
     * @return $this
88
     */
89 6
    public function release($priority = Beanie::DEFAULT_PRIORITY, $delay = Beanie::DEFAULT_DELAY)
90
    {
91 6
        $response = $this->executeCommand(CommandInterface::COMMAND_RELEASE, [$priority, $delay]);
92 6
        $this->state = $response->getName() === Response::RESPONSE_RELEASED
93 6
            ? self::STATE_RELEASED
94 6
            : self::STATE_BURIED;
95
96 6
        return $this;
97
    }
98
99
    /**
100
     * @return $this
101
     */
102 1
    public function touch()
103
    {
104 1
        $this->executeCommand(CommandInterface::COMMAND_TOUCH);
105 1
        return $this;
106
    }
107
108
    /**
109
     * @param int $priority
110
     * @return $this
111
     */
112 2
    public function bury($priority = Beanie::DEFAULT_PRIORITY)
113
    {
114 2
        $this->executeCommand(CommandInterface::COMMAND_BURY, [$priority]);
115 2
        $this->state = self::STATE_BURIED;
116 2
        return $this;
117
    }
118
119
    /**
120
     * @return $this
121
     */
122 1
    public function delete()
123
    {
124 1
        $this->executeCommand(CommandInterface::COMMAND_DELETE);
125 1
        $this->state = self::STATE_DELETED;
126 1
        return $this;
127
    }
128
129
    /**
130
     * @return array
131
     */
132 1
    public function stats()
133
    {
134 1
        return $this->executeCommand(CommandInterface::COMMAND_STATS_JOB)->getData();
135
    }
136
137
    /**
138
     * @param string $command
139
     * @param array $arguments
140
     * @return Response
141
     */
142 12
    private function executeCommand($command, $arguments = [])
143
    {
144 12
        return $this->server
145 12
            ->dispatchCommand($this->commandFactory->create($command, array_merge([$this->id], $arguments)))
146 12
            ->invoke();
147
    }
148
149
    /**
150
     * @return array
151
     */
152 1
    public function jsonSerialize()
153
    {
154
        return [
155 1
            'id' => $this->id,
156 1
            'server' => (string)$this->server,
157 1
            'state' => $this->state
158 1
        ];
159
    }
160
}
161