Completed
Push — master ( 17312a...d17d72 )
by Mario
03:12
created

NativeHttpClient::send()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 27
cp 0
rs 8.5125
cc 6
eloc 26
nc 12
nop 1
crap 42
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
use Marek\Toggable\API\Security\Cookie\SessionCookieInterface;
8
9
/**
10
 * Class NativeHttpClient
11
 * @package Marek\Toggable\Http\Client
12
 */
13
class NativeHttpClient implements HttpClientInterface
14
{
15
    /**
16
     * @var \Marek\Toggable\API\Security\TokenInterface
17
     */
18
    private $token;
19
20
    /**
21
     * @var string
22
     */
23
    private $url;
24
25
    /**
26
     * NativeHttpClient constructor.
27
     *
28
     * @param string $url
29
     * @param \Marek\Toggable\API\Security\TokenInterface $token
30
     */
31 13
    public function __construct($url, \Marek\Toggable\API\Security\TokenInterface $token)
32
    {
33 13
        $this->url = $url;
34 13
        $this->token = $token;
35 13
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function send(\Marek\Toggable\API\Http\Request\RequestInterface $request)
0 ignored issues
show
Coding Style introduced by
send uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
    {
42
        $opts = array(
43
            'http' => array(
44
                'method' => $request->getMethod(),
45
                'header' => array(
46
                    'Content-type: application/json',
47
                ),
48
            ),
49
        );
50
51
        if (session_status() === PHP_SESSION_ACTIVE && !empty($_SESSION[SessionCookieInterface::COOKIE_NAME])) {
52
53
            $opts['http']['header'][] = 'Cookie ' . $_SESSION[SessionCookieInterface::COOKIE_NAME];
54
55
        } else {
56
57
            $auth = $this->token->getAuthentication();
58
            $auth = $auth[0] . ':' . $auth[1];
59
            $auth = base64_encode($auth);
60
            $opts['http']['header'][] = 'Authorization: Basic ' . $auth;
61
62
        }
63
64
        if (!empty($request->getData())) {
65
            $opts['http']['content'] = json_encode($request);
66
        }
67
68
        $context = stream_context_create($opts);
69
70
        $url = $this->url . '/' . $request->getUri();
71
        $stream = @fopen($url, 'r', false, $context);
72
73
        if ($stream == false) {
74
            throw new NetworkException("Error while trying to connect to Toggl");
75
        }
76
77
        $data = stream_get_contents($stream);
78
        if ($data === false) {
79
            throw new ServerException("Error reading response data.");
80
        }
81
82
        $metadata = stream_get_meta_data($stream);
83
        fclose($stream);
84
85
        return new HttpClientResponse($data, $metadata);
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function setToken(\Marek\Toggable\API\Security\TokenInterface $token)
92
    {
93
        $this->token = $token;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function setCookie(\Marek\Toggable\API\Security\Cookie\SessionCookieInterface $cookie)
0 ignored issues
show
Coding Style introduced by
setCookie uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
    {
101
        if (session_status() !== PHP_SESSION_ACTIVE) {
102
            session_start();
103
        }
104
105
        $_SESSION[SessionCookieInterface::COOKIE_NAME] = $cookie->toString();
106
    }
107
}
108