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

NativeHttpClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 34
ccs 0
cts 15
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B send() 0 28 4
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