Completed
Push — master ( 40153a...2a97e2 )
by Christian
03:21
created

Handler::getHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Client\Request;
13
14
use Guzzle\Http\ClientInterface;
15
use Guzzle\Http\Exception\ClientErrorResponseException;
16
use Guzzle\Http\Message\RequestInterface;
17
use Xabbuh\XApi\Common\Exception\AccessDeniedException;
18
use Xabbuh\XApi\Common\Exception\ConflictException;
19
use Xabbuh\XApi\Common\Exception\NotFoundException;
20
use Xabbuh\XApi\Common\Exception\XApiException;
21
22
/**
23
 * Prepare and execute xAPI HTTP requests.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
final class Handler implements HandlerInterface
28
{
29
    private $httpClient;
30
31
    private $version;
32
33
    private $username;
34
35
    private $password;
36
37
    /**
38
     * @param ClientInterface $httpClient The HTTP client
39
     * @param string          $version    The xAPI version
40
     * @param string          $username   The optional HTTP auth username
41
     * @param string          $password   The optional HTTP auth password
42
     */
43
    public function __construct(ClientInterface $httpClient, $version, $username = null, $password = null)
44
    {
45
        $this->httpClient = $httpClient;
46
        $this->version = $version;
47
        $this->username = $username;
48
        $this->password = $password;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function createRequest($method, $uri, array $urlParameters = array(), $body = null)
55
    {
56
        if (count($urlParameters) > 0) {
57
            $uri .= '?'.http_build_query($urlParameters);
58
        }
59
60
        switch ($method) {
61
            case 'get':
62
                $request = $this->httpClient->get($uri);
63
                break;
64
            case 'post':
65
                $request = $this->httpClient->post($uri, null, $body);
66
                break;
67
            case 'put':
68
                $request = $this->httpClient->put($uri, null, $body);
69
                break;
70
            case 'delete':
71
                $request = $this->httpClient->delete($uri);
72
                break;
73
            default:
74
                throw new \InvalidArgumentException(
75
                    $method.' is no valid HTTP method'
76
                );
77
        }
78
79
        $request->addHeader('X-Experience-API-Version', $this->version);
80
        $request->addHeader('Content-Type', 'application/json');
81
        $request->setAuth($this->username, $this->password);
82
83
        return $request;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function executeRequest(RequestInterface $request, array $validStatusCodes)
90
    {
91
        try {
92
            $response = $request->send();
93
        } catch (ClientErrorResponseException $e) {
94
            $response = $e->getResponse();
95
        }
96
97
        // catch some common errors
98
        if (in_array($response->getStatusCode(), array(401, 403))) {
99
            throw new AccessDeniedException(
100
                $response->getBody(true),
101
                $response->getStatusCode()
102
            );
103
        } elseif (404 === $response->getStatusCode()) {
104
            throw new NotFoundException($response->getBody(true));
105
        } elseif (409 === $response->getStatusCode()) {
106
            throw new ConflictException($response->getBody(true));
107
        }
108
109
        if (!in_array($response->getStatusCode(), $validStatusCodes)) {
110
            throw new XApiException($response->getBody(true), $response->getStatusCode());
111
        }
112
113
        return $response;
114
    }
115
}
116