Completed
Push — master ( f87b82...3f7aa1 )
by Mario
02:46
created

NativeHttpClient::send()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
ccs 0
cts 15
cp 0
rs 8.5806
cc 4
eloc 15
nc 4
nop 2
crap 20
1
<?php
2
3
namespace Marek\Toggable\Http\Client;
4
5
use Marek\Toggable\API\Exception\Http\NetworkException;
6
use Marek\Toggable\API\Exception\Http\ServerException;
7
8
/**
9
 * Class NativeHttpClient
10
 * @package Marek\Toggable\Http\Client
11
 */
12
class NativeHttpClient implements HttpClientInterface
13
{
14
    /**
15
     * @inheritDoc
16
     */
17
    public function send($uri, $options)
18
    {
19
        $context = stream_context_create($options);
20
21
        $stream = @fopen($uri, 'r', false, $context);
22
23
        if ($stream == false) {
24
            return array();
25
        }
26
27
        $data = stream_get_contents($stream);
28
29
        if (empty($data)) {
30
            throw new NetworkException('Toggl server is unreachable');
31
        }
32
33
        if ($data['data'] === false) {
34
            throw new ServerException('Toggl server did not return any data');
35
        }
36
37
        $metadata = stream_get_meta_data($stream);
38
        fclose($stream);
39
40
        return array(
41
            'data'      => $data,
42
            'metadata'  => $metadata,
43
        );
44
    }
45
}
46