JobResult::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 7
    public static function populateFromClientException(ClientException $e)
25
    {
26 7
        $body = $e->getResponse()->getBody();
27 7
        $data = json_decode($body);
28
29
        // find out what kind of error happend and give some extra help
30 7
        if (strpos($data->reason, 'inequivalent arg \'durable\'') !== false) {
31
            $data->cause = 'Queue already exists with different durable stat, delete the queue first';
32
        }
33
34 7
        $res = new Response($e->getCode(), $e->getResponse()->getHeaders(), json_encode($data));
35 7
        return new JobResult($res);
36
    }
37
38 39
    public function __construct($response)
39
    {
40 39
        $this->response = $response;
41 39
    }
42
43 23
    public function getBody()
44
    {
45 23
        $bodyContent = $this->response->getBody()->getContents();
46 23
        if (!empty($bodyContent)) {
47 23
            return json_decode($bodyContent);
48
        }
49
50
        return [];
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56 39
    public function isSuccess()
57
    {
58 39
        return $this->response->getStatusCode() === 200
59 17
            || $this->response->getStatusCode() === 201
60 39
            || $this->response->getStatusCode() === 204;
61
    }
62
}
63