Completed
Push — master ( 164d8d...5ccfa5 )
by Mario
02:58
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 100%

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 62
cts 62
cp 1
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 1
    public function createClient(ClientValue $client)
30
    {
31 1
        $request = new CreateClientRequest(
32
            array(
33 1
                'data' => $this->extractDataFromObject($client),
34
            )
35 1
        );
36
37 1
        $response = $this->delegate($request);
38
39 1
        return new ClientResponse(
40
            array(
41 1
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()),
42
            )
43 1
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2 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 2
        if (empty($clientId) || !is_int($clientId)) {
52 1
            throw new InvalidArgumentException(
53 1
                sprintf('$clientId argument not provided in %s', get_class($this))
54 1
            );
55
        }
56
57 1
        $request = new GetClientDetailsRequest(
58
            array(
59 1
                'clientId' => $clientId,
60
            )
61 1
        );
62
63 1
        $response = $this->delegate($request);
64
65 1
        return new ClientResponse(
66
            array(
67 1
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue())
68 1
            )
69 1
        );
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getClients()
76
    {
77 1
        $request = new GetClientsRequest();
78
79 1
        $response = $this->delegate($request);
80
81 1
        $clients = array();
82 1
        foreach($response->body as $client) {
83 1
            $clients[] = $this->hydrator->hydrate($client, new ClientValue);
84 1
        }
85
86 1
        return new ClientsResponse(
87
            array(
88 1
                'clients' => $clients,
89
            )
90 1
        );
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function getClientProjects($clientId, $active = Activity::ACTIVE)
97
    {
98 1
        $request = new GetClientProjectsRequest(array(
99 1
            'clientId' => $clientId,
100 1
            'active' => $active,
101
            )
102 1
        );
103
104 1
        $response = $this->delegate($request);
105
106 1
        $projects = array();
107 1
        foreach ($response->body as $project) {
108 1
            $projects[] = $this->hydrator->hydrate($project, new ProjectValue());
109 1
        }
110
111 1
        return new ProjectsResponse(array('projects' => $projects));
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 2 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 2
        if (empty($clientId) || !is_int($clientId)) {
120 1
            throw new InvalidArgumentException(
121 1
                sprintf('$clientId argument not provided in %s', get_class($this))
122 1
            );
123
        }
124
125 1
        $request = new UpdateClient(array(
126 1
                'clientId' => $clientId,
127 1
                'data' => $this->extractDataFromObject($client),
128
            )
129 1
        );
130
131 1
        $response = $this->delegate($request);
132
133 1
        return new ClientResponse(
134
            array(
135 1
                'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()),
136
            )
137 1
        );
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 2 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 2
        if (empty($clientId) || !is_int($clientId)) {
146 1
            throw new InvalidArgumentException(
147 1
                sprintf('$clientId argument not provided in %s', get_class($this))
148 1
            );
149
        }
150
151 1
        $request = new DeleteClientRequest(array('clientId' => $clientId));
152
153 1
        return $this->delegate($request);
154
    }
155
}
156