Completed
Push — master ( a12959...164d8d )
by Mario
03:20
created

ClientService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 132
Duplicated Lines 42.42 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 14
c 9
b 0
f 1
lcom 1
cbo 14
dl 56
loc 132
ccs 0
cts 62
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 16 1
A getClientDetails() 22 22 3
A getClients() 0 17 2
A getClientProjects() 0 17 2
A updateClient() 22 22 3
A deleteClient() 12 12 3

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
namespace Marek\Toggable\Service\Client;
4
5
use InvalidArgumentException;
6
use Marek\Toggable\API\Http\Request\Client\UpdateClient;
7
use Marek\Toggable\API\Http\Response\Client\Client as ClientResponse;
8
use Marek\Toggable\API\Http\Response\Client\Clients as ClientsResponse;
9
use Marek\Toggable\API\Http\Response\Project\Projects as ProjectsResponse;
10
use Marek\Toggable\API\Toggl\Values\Project\Project as ProjectValue;
11
use Marek\Toggable\API\Toggl\Values\Client\Client as ClientValue;
12
use Marek\Toggable\API\Http\Request\Client\CreateClient as CreateClientRequest;
13
use Marek\Toggable\API\Http\Request\Client\GetClientDetails as GetClientDetailsRequest;
14
use Marek\Toggable\API\Http\Request\Client\GetClientProjects as GetClientProjectsRequest;
15
use Marek\Toggable\API\Http\Request\Client\GetClients as GetClientsRequest;
16
use Marek\Toggable\API\Http\Request\Client\DeleteClient as DeleteClientRequest;
17
use Marek\Toggable\Service\AbstractService;
18
use Marek\Toggable\API\Toggl\Values\Activity;
19
20
/**
21
 * Class ClientService
22
 * @package Marek\Toggable\Service\Client
23
 */
24
class ClientService extends AbstractService implements \Marek\Toggable\API\Toggl\ClientServiceInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createClient(ClientValue $client)
30
    {
31
        $request = new CreateClientRequest(
32
            array(
33
                'data' => $this->extractDataFromObject($client),
34
            )
35
        );
36
37
        $response = $this->delegate($request);
38
39
        return new ClientResponse(
40
            array(
41
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()),
42
            )
43
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 View Code Duplication
    public function getClientDetails($clientId)
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...
50
    {
51
        if (empty($clientId) || !is_int($clientId)) {
52
            throw new InvalidArgumentException(
53
                sprintf('$clientId argument not provided in %s', get_class($this))
54
            );
55
        }
56
57
        $request = new GetClientDetailsRequest(
58
            array(
59
                'clientId' => $clientId,
60
            )
61
        );
62
63
        $response = $this->delegate($request);
64
65
        return new ClientResponse(
66
            array(
67
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue())
68
            )
69
        );
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getClients()
76
    {
77
        $request = new GetClientsRequest();
78
79
        $response = $this->delegate($request);
80
81
        $clients = array();
82
        foreach($response->body as $client) {
83
            $clients[] = $this->hydrator->hydrate($client, new ClientValue);
84
        }
85
86
        return new ClientsResponse(
87
            array(
88
                'clients' => $clients,
89
            )
90
        );
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getClientProjects($clientId, $active = Activity::ACTIVE)
97
    {
98
        $request = new GetClientProjectsRequest(array(
99
            'clientId' => $clientId,
100
            'active' => $active,
101
            )
102
        );
103
104
        $response = $this->delegate($request);
105
106
        $projects = array();
107
        foreach ($response->body as $project) {
108
            $projects[] = $this->hydrator->hydrate($project, new ProjectValue());
109
        }
110
111
        return new ProjectsResponse(array('projects' => $projects));
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 View Code Duplication
    public function updateClient($clientId, ClientValue $client)
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...
118
    {
119
        if (empty($clientId) || !is_int($clientId)) {
120
            throw new InvalidArgumentException(
121
                sprintf('$clientId argument not provided in %s', get_class($this))
122
            );
123
        }
124
125
        $request = new UpdateClient(array(
126
                'clientId' => $clientId,
127
                'data' => $this->extractDataFromObject($client),
128
            )
129
        );
130
131
        $response = $this->delegate($request);
132
133
        return new ClientResponse(
134
            array(
135
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()),
136
            )
137
        );
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 View Code Duplication
    public function deleteClient($clientId)
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...
144
    {
145
        if (empty($clientId) || !is_int($clientId)) {
146
            throw new InvalidArgumentException(
147
                sprintf('$clientId argument not provided in %s', get_class($this))
148
            );
149
        }
150
151
        $request = new DeleteClientRequest(array('clientId' => $clientId));
152
153
        return $this->delegate($request);
154
    }
155
}
156