|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Beanie\Job; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Beanie\Command\CommandInterface; |
|
8
|
|
|
use Beanie\Command\Response; |
|
9
|
|
|
use Beanie\Exception\InvalidArgumentException; |
|
10
|
|
|
use Beanie\Server\Server; |
|
11
|
|
|
use Beanie\Util\FactoryInterface; |
|
12
|
|
|
use Beanie\Util\FactoryTrait; |
|
13
|
|
|
|
|
14
|
|
|
class JobFactory implements FactoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use FactoryTrait; |
|
17
|
|
|
|
|
18
|
|
|
private static $responseToStateMap = [ |
|
19
|
|
|
Response::RESPONSE_RESERVED => Job::STATE_RESERVED, |
|
20
|
|
|
Response::RESPONSE_INSERTED => Job::STATE_RELEASED, |
|
21
|
|
|
Response::RESPONSE_RELEASED => Job::STATE_RELEASED, |
|
22
|
|
|
Response::RESPONSE_BURIED => Job::STATE_BURIED |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param \Beanie\Command\Response $response |
|
27
|
|
|
* @return Job |
|
28
|
|
|
* @throws InvalidArgumentException |
|
29
|
|
|
*/ |
|
30
|
14 |
|
public function createFromResponse(Response $response) |
|
31
|
|
|
{ |
|
32
|
14 |
|
$state = isset(self::$responseToStateMap[$response->getName()]) |
|
33
|
14 |
|
? self::$responseToStateMap[$response->getName()] |
|
34
|
14 |
|
: Job::STATE_UNKNOWN; |
|
35
|
|
|
|
|
36
|
14 |
|
$this->validateResponseData($response->getData()); |
|
37
|
|
|
|
|
38
|
12 |
|
return new Job($response->getData()['id'], $response->getData()['data'], $response->getServer(), $state); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param CommandInterface $command |
|
43
|
|
|
* @param Server $server |
|
44
|
|
|
* @return JobOath |
|
45
|
|
|
*/ |
|
46
|
12 |
|
public function createFromCommand(CommandInterface $command, Server $server) |
|
47
|
|
|
{ |
|
48
|
12 |
|
return new JobOath( |
|
49
|
12 |
|
$server->dispatchCommand($command), |
|
50
|
|
|
$this |
|
51
|
10 |
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $data |
|
56
|
|
|
* @throws InvalidArgumentException |
|
57
|
|
|
*/ |
|
58
|
14 |
|
protected function validateResponseData(array $data) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!( |
|
61
|
14 |
|
isset($data['id']) && |
|
62
|
13 |
|
isset($data['data']) |
|
63
|
14 |
|
)) { |
|
64
|
2 |
|
throw new InvalidArgumentException('Could not create Job from response: incorrect data returned'); |
|
65
|
|
|
} |
|
66
|
12 |
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|