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

JobResult   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 40
ccs 0
cts 7
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A populateFromClientException() 0 12 2
A __construct() 0 4 1
A getBody() 0 9 2
A isSuccess() 0 4 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