DocumentApiClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Api;
13
14
use Xabbuh\XApi\Client\Request\HandlerInterface;
15
use Xabbuh\XApi\Model\Document;
16
use Xabbuh\XApi\Model\DocumentData;
17
use Xabbuh\XApi\Serializer\DocumentDataSerializerInterface;
18
19
/**
20
 * Base class for the document API classes.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
abstract class DocumentApiClient
25
{
26
    private $requestHandler;
27
    private $version;
28
    private $documentDataSerializer;
29
30
    /**
31
     * @param HandlerInterface                $requestHandler         The HTTP request handler
32
     * @param string                          $version                The xAPI version
33
     * @param DocumentDataSerializerInterface $documentDataSerializer The document data serializer
34
     */
35 12
    public function __construct(HandlerInterface $requestHandler, $version, DocumentDataSerializerInterface $documentDataSerializer)
36
    {
37 12
        $this->requestHandler = $requestHandler;
38 12
        $this->version = $version;
39 12
        $this->documentDataSerializer = $documentDataSerializer;
40 12
    }
41
42
    /**
43
     * Stores a document.
44
     *
45
     * @param string   $method        HTTP method to use
46
     * @param string   $uri           Endpoint URI
47
     * @param array    $urlParameters URL parameters
48
     * @param Document $document      The document to store
49
     */
50 6
    protected function doStoreDocument($method, $uri, $urlParameters, Document $document)
51
    {
52 6
        $request = $this->requestHandler->createRequest(
53 6
            $method,
54 6
            $uri,
55 6
            $urlParameters,
56 6
            $this->documentDataSerializer->serializeDocumentData($document->getData())
57
        );
58 6
        $this->requestHandler->executeRequest($request, array(204));
59 6
    }
60
61
    /**
62
     * Deletes a document.
63
     *
64
     * @param string $uri           The endpoint URI
65
     * @param array  $urlParameters The URL parameters
66
     */
67 3
    protected function doDeleteDocument($uri, array $urlParameters)
68
    {
69 3
        $request = $this->requestHandler->createRequest('delete', $uri, $urlParameters);
70 3
        $this->requestHandler->executeRequest($request, array(204));
71 3
    }
72
73
    /**
74
     * Returns a document.
75
     *
76
     * @param string $uri           The endpoint URI
77
     * @param array  $urlParameters The URL parameters
78
     *
79
     * @return Document The document
80
     */
81 3
    protected function doGetDocument($uri, array $urlParameters)
82
    {
83 3
        $request = $this->requestHandler->createRequest('get', $uri, $urlParameters);
84 3
        $response = $this->requestHandler->executeRequest($request, array(200));
85 3
        $document = $this->deserializeDocument((string) $response->getBody());
86
87 3
        return $document;
88
    }
89
90
    /**
91
     * Deserializes the data of a document.
92
     *
93
     * @param string $data The serialized document data
94
     *
95
     * @return DocumentData The parsed document data
96
     */
97 3
    protected function deserializeDocument($data)
98
    {
99 3
        return $this->documentDataSerializer->deserializeDocumentData($data);
100
    }
101
}
102