StateApiClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 28.41 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 1
cbo 5
dl 25
loc 88
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createOrUpdateDocument() 0 4 1
A createOrReplaceDocument() 0 4 1
A deleteDocument() 0 8 1
A getDocument() 11 11 1
A doStoreStateDocument() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(
39
        HandlerInterface $requestHandler,
40
        $version,
41
        DocumentDataSerializerInterface $documentDataSerializer,
42
        ActorSerializerInterface $actorSerializer
43
    ) {
44
        parent::__construct($requestHandler, $version, $documentDataSerializer);
45
46
        $this->actorSerializer = $actorSerializer;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function createOrUpdateDocument(StateDocument $document)
53
    {
54
        $this->doStoreStateDocument('post', $document);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function createOrReplaceDocument(StateDocument $document)
61
    {
62
        $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 View Code Duplication
    public function getDocument(State $state)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        /** @var \Xabbuh\XApi\Model\DocumentData $documentData */
83
        $documentData = $this->doGetDocument('activities/state', array(
84
            'activityId' => $state->getActivity()->getId(),
85
            'agent' => $this->actorSerializer->serializeActor($state->getActor()),
86
            '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 View Code Duplication
    private function doStoreStateDocument($method, StateDocument $document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $state = $document->getState();
101
        $this->doStoreDocument(
102
            $method,
103
            'activities/state',
104
            array(
105
                'activityId' => $state->getActivity()->getId(),
106
                'agent' => $this->actorSerializer->serializeActor($state->getActor()),
107
                'stateId' => $state->getStateId(),
108
            ),
109
            $document
110
        );
111
    }
112
}
113