JobResult::populateFromClientException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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