Completed
Push — master ( ce9410...27e308 )
by Denis
06:09
created

Client::getResponse()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 10
cp 0.7
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 5
nop 2
crap 6.972
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 25.07.17
6
 * Time: 17:18
7
 */
8
9
namespace Lan\Ebs\Sdk;
10
11
use Exception;
12
use Lan\Ebs\Sdk\Helper\Curl;
13
use Lan\Ebs\Sdk\Helper\Debuger;
14
15
final class Client
16
{
17
    private $host = 'http://openapi.landev.ru';
18
19
    private $token;
20
21
    /**
22
     * Ebs constructor.
23
     *
24
     * @param  $token
25
     * @throws Exception
26
     */
27 25
    public function __construct($token)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER 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...
28
    {
29 25
        if (empty($token)) {
30 1
            throw new Exception('Token is empty');
31
        }
32
33 25
        $this->token = $token;
34
35 25
        if ($_SERVER['USER'] == 'dp') {
36
            $this->host = 'http://eop.local';
37
        }
38
39 25
        if (\session_status() === PHP_SESSION_NONE && !\headers_sent()) {
40
            session_start();
41
        }
42 25
    }
43
44 14
    public function getResponse(array $request, array $params = [])
45
    {
46 14
        if (empty($request['url']) || empty($request['method']) || empty($request['code'])) {
47
            throw new Exception('Request url, method or success_code is missing');
48
        }
49
50 14
        $response = Curl::getResponse($this->host, $request['url'], $request['method'], $this->token, $params);
51
52 14
        if (isset($response['debug'])) {
53
            Debuger::dump(['request' => $request, 'params' => $params, 'response' => $response]);
54
        }
55
56 14
        if ($response['status'] != $request['code']) {
57 4
            throw new Exception($response['message'], $response['status']);
58
        }
59
60 11
        return $response;
61
    }
62
}