Test Failed
Push — master ( 9d048f...9d083f )
by Mathijs
07:23
created

JobResult::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mathijs
5
 * Date: 27-4-2016
6
 * Time: 19:24
7
 */
8
9
namespace mcorten87\rabbitmq_api\objects;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Response;
13
14
/**
15
 * A wrapper for \GuzzleHttp\Psr7\Response
16
 *
17
 * Class JobResult
18
 * @package mcorten87\rabbitmq_api\objects
19
 */
20
class JobResult
21
{
22
    private $response;
23
24
    public static function populateFromClientException(ClientException $e)
25
    {
26
        $data = json_decode($e->getResponse()->getBody());
27
28
        // find out what kind of error happend and give some extra help
29
        if (strpos($data->reason, 'inequivalent arg \'durable\'') !== false) {
30
            $data->cause = 'Queue already exists with different durable stat, delete the queue first';
31
        }
32
33
        $res = new Response($e->getCode(), $e->getResponse()->getHeaders(), json_encode($data));
34
        return new JobResult($res);
35
    }
36
37
    public function __construct($response)
38
    {
39
        $this->response = $response;
40
    }
41
42
    public function getBody()
43
    {
44
        $bodyContent = $this->response->getBody()->getContents();
45
        if (!empty($bodyContent)) {
46
            return json_decode($bodyContent);
47
        }
48
49
        return [];
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function isSuccess()
56
    {
57
        return $this->response->getStatusCode() === 200 || $this->response->getStatusCode() === 204;
58
    }
59
}
60