Passed
Pull Request — master (#43)
by Baptiste
02:06
created

Context   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 27
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getJson() 0 8 1
A responseIsValidjson() 0 6 1
1
<?php declare(strict_types=1);
2
namespace Behapi\Json;
3
4
use stdClass;
5
use InvalidArgumentException;
6
7
use Webmozart\Assert\Assert;
8
9
use Behapi\Http\Client;
10
use Behapi\HttpHistory\History as HttpHistory;
11
12
use function sprintf;
13
14
use function json_decode;
15
use function json_last_error;
16
use function json_last_error_msg;
17
18
use const JSON_ERROR_NONE;
19
20
class Context extends AbstractContext
21
{
22
    use Client;
23
24
    public function __construct(HttpHistory $history)
25
    {
26
        parent::__construct();
27
        $this->history = $history;
28
    }
29
30
    /** {@inheritDoc} */
31
    protected function getJson()
32
    {
33
        $decoded = json_decode((string) $this->getResponse()->getBody());
34
35
        Assert::same(JSON_ERROR_NONE, json_last_error(), sprintf('The response is not a valid json (%s)', json_last_error_msg()));
36
37
        return $decoded;
38
    }
39
40
    public function responseIsValidjson()
41
    {
42
        Assert::same($this->getResponse()->getHeaderLine('Content-Type'), 'application/json', 'The response should have a valid content-type (expected %2$s, got %1$s)');
43
44
        parent::responseIsValidjson();
45
    }
46
}
47