Completed
Push — master ( f14030...b5bdc1 )
by Laurynas
38:14 queued 35:54
created

Client::post()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 8.5906
cc 5
eloc 14
nc 4
nop 3
crap 5
1
<?php namespace Qualia;
2
3
use Httpful\Mime;
4
use Httpful\Request;
5
use Qualia\Exceptions\ConnectionErrorException;
6
use Qualia\Exceptions\EmailExistsException;
7
use Qualia\Exceptions\RequestException;
8
9
class Client
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $survey;
15
    /**
16
     * @var string
17
     */
18
    protected $token;
19
    /**
20
     * @var string
21
     */
22
    private $endpoint;
23
24 84
    public function __construct($survey, $token, $endpoint = 'api.qualiaanalytics.org')
25
    {
26 84
        $this->survey = $survey;
27 84
        $this->token  = $token;
28 84
        $this->endpoint = $endpoint;
29
30 84
        $template = Request::init()
31 84
            ->withStrictSSL()
32 84
            ->addHeader('Authorization', 'token')
33 84
            ->addHeader('User-Agent', 'Qualia API/PHP/'. PHP_VERSION . ' (' . PHP_OS . ')')
34 84
            ->parseWith(function($body) {
35 72
                return json_decode($body, true);
36 84
            })
37 84
            ->sendsAndExpects(Mime::JSON);
38
39 84
        Request::ini($template);
40 84
    }
41
42
    /**
43
     * Get the provided survey
44
     *
45
     * @return string
46
     */
47 78
    public function getSurveyId()
48
    {
49 78
        return $this->survey;
50
    }
51
52 72
    public function post($url, $data = array(), $additional = array())
53
    {
54
        try {
55 72
            $response = Request::post("https://" . $this->endpoint . '/' . $url)->body(array_merge($additional, array(
56 72
                'api_token'     => $this->token,
57 72
                'data'          => $data,
58 72
            )))->send();
59 66
            $body = $response->body;
60 39
        } catch (\Httpful\Exception\ConnectionErrorException $e) {
61
            // rethrow the error.
62 6
            throw new ConnectionErrorException($e->getMessage(), $e->getCode(), $e->getPrevious());
63
        }
64
65 66
        if (isset($body['status_code']) && $body['status_code'] == 208) {
66 7
            throw new EmailExistsException($body['id'], $response->code);
0 ignored issues
show
Unused Code introduced by
The call to EmailExistsException::__construct() has too many arguments starting with $response->code.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
67
        }
68
69 65
        if ($response->code !== 200) {
70 24
            throw new RequestException($body['message'], $response->code);
71
        }
72
73 41
        return $body;
74
    }
75
76 6
    public function get($url)
77
    {
78 6
        $url = "https://" . $this->endpoint . '/' . $url . '?api_token=' . $this->token;
79
80 6
        echo("You may want to view this in your browser with enabled JSON Formatter or with REST API Client: $url");
81
82 6
        return Request::get($url)
83 6
                      ->send()->body;
84
    }
85
}