Completed
Push — master ( 105c93...2eeb7e )
by Christian
04:01
created

StateApiClient::createOrUpdateDocument()   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.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.037
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\Serializer\ActorSerializerInterface;
16
use Xabbuh\XApi\Serializer\DocumentDataSerializerInterface;
17
use Xabbuh\XApi\Model\StateDocument;
18
use Xabbuh\XApi\Model\State;
19
20
/**
21
 * Client to access the state API of an xAPI based learning record store.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
class StateApiClient extends DocumentApiClient implements StateApiClientInterface
26
{
27
    /**
28
     * @var ActorSerializerInterface
29
     */
30
    private $actorSerializer;
31
32
    /**
33
     * @param HandlerInterface                $requestHandler         The HTTP request handler
34
     * @param string                          $version                The xAPI version
35
     * @param DocumentDataSerializerInterface $documentDataSerializer The document data serializer
36
     * @param ActorSerializerInterface        $actorSerializer        The actor serializer
37
     */
38 5
    public function __construct(
39
        HandlerInterface $requestHandler,
40
        $version,
41
        DocumentDataSerializerInterface $documentDataSerializer,
42
        ActorSerializerInterface $actorSerializer
43
    ) {
44 5
        parent::__construct($requestHandler, $version, $documentDataSerializer);
45
46 5
        $this->actorSerializer = $actorSerializer;
47 5
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function createOrUpdateDocument(StateDocument $document)
53
    {
54 1
        $this->doStoreStateDocument('post', $document);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    public function createOrReplaceDocument(StateDocument $document)
61
    {
62 1
        $this->doStoreStateDocument('put', $document);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function deleteDocument(State $state)
69
    {
70
        $this->doDeleteDocument('activities/state', array(
71
            'activityId' => $state->getActivity()->getId(),
72
            'agent' => $this->actorSerializer->serializeActor($state->getActor()),
73
            'stateId' => $state->getStateId(),
74
        ));
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function getDocument(State $state)
81
    {
82
        /** @var \Xabbuh\XApi\Model\DocumentData $documentData */
83 1
        $documentData = $this->doGetDocument('activities/state', array(
84 1
            'activityId' => $state->getActivity()->getId(),
85 1
            'agent' => $this->actorSerializer->serializeActor($state->getActor()),
86 1
            'stateId' => $state->getStateId(),
87
        ));
88
89
        return new StateDocument($state, $documentData);
90
    }
91
92
    /**
93
     * Stores a state document.
94
     *
95
     * @param string        $method   HTTP method to use
96
     * @param StateDocument $document The document to store
97
     */
98 2
    private function doStoreStateDocument($method, StateDocument $document)
99
    {
100 2
        $state = $document->getState();
101 2
        $this->doStoreDocument(
102
            $method,
103 2
            'activities/state',
104
            array(
105 2
                'activityId' => $state->getActivity()->getId(),
106 2
                'agent' => $this->actorSerializer->serializeActor($state->getActor()),
107 2
                'stateId' => $state->getStateId(),
108
            ),
109
            $document
110
        );
111
    }
112
}
113