JobFactory::validateResponseData()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3
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