Completed
Pull Request — master (#11)
by Tobias
02:09 queued 22s
created

UserAPI   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 20 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 25
loc 125
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getUser() 0 5 1
A deleteUser() 0 3 1
A createUser() 0 7 1
A getAllUsers() 0 10 2
A updateUser() 0 4 1
A privateMessageUser() 0 8 2
A getPrivateChatMessage() 10 10 1
A getPhoto() 0 7 1
A getRecentPrivateChatHistory() 13 13 2

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 SolutionDrive\HipchatAPIv2Client\API;
4
5
use SolutionDrive\HipchatAPIv2Client\ClientInterface;
6
use SolutionDrive\HipchatAPIv2Client\Model\User;
7
use SolutionDrive\HipchatAPIv2Client\Model\Message;
8
use SolutionDrive\HipchatAPIv2Client\Model\UserInterface;
9
10
class UserAPI implements UserAPIInterface
11
{
12
    /** @var ClientInterface */
13
    protected $client;
14
15
    /**
16
     * Room api constructor
17
     *
18
     * @param ClientInterface $client that will be used to connect the server
19
     */
20 12
    public function __construct(ClientInterface $client)
21
    {
22 12
        $this->client = $client;
23 12
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28 1
    public function getAllUsers($parameters = array())
29
    {
30 1
        $response = $this->client->get('/v2/user', $parameters);
31
32 1
        $users = array();
33 1
        foreach ($response['items'] as $response) {
34 1
            $users[] = new User($response);
35 1
        }
36
37 1
        return $users;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 1
    public function getUser($userId)
44
    {
45 1
        $response = $this->client->get(sprintf('/v2/user/%s', $userId));
46
47 1
        return new User($response);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function createUser(UserInterface $user, $password)
54
    {
55 1
        $request = $user->toJson();
56 1
        $request['password'] = $password;
57 1
        $response = $this->client->post('/v2/user', $request);
58
59 1
        return $response['id'];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function updateUser(UserInterface $user)
66
    {
67 1
        $request = $user->toJson();
68 1
        $this->client->put(sprintf('/v2/user/%s', $user->getId()), $request);
69 1
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    public function deleteUser($userId)
75
    {
76 1
        $this->client->delete(sprintf('/v2/user/%s', $userId));
77 1
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 2
    public function privateMessageUser($userId, $message)
83
    {
84 2
        if (is_string($message)) {
85 1
            $content = array('message' => $message);
86 1
        } else { // Assuming its a Message
87 1
            $content = $message->toJson();
88
        }
89 2
        $this->client->post(sprintf('/v2/user/%s/message', $userId), $content);
90 2
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 2 View Code Duplication
    public function getRecentPrivateChatHistory($userId, array $parameters = array())
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...
96
    {
97 2
        $response = $this->client->get(
98 2
            sprintf('/v2/user/%s/history/latest', $userId),
99
            $parameters
100 2
        );
101
102 2
        $messages = array();
103 2
        foreach ($response['items'] as $response) {
104 1
            $messages[] = new Message($response);
105 2
        }
106
107 2
        return $messages;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 1 View Code Duplication
    public function getPrivateChatMessage($user, $messageId, array $parameters = array())
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...
114
    {
115 1
        $response = $this->client->get(
116 1
            sprintf('/v2/user/%s/history/%s', $user, $messageId),
117
            $parameters
118 1
        );
119
120 1
        $message = new Message($response['message']);
121
122 1
        return $message;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 1
    public function getPhoto($userId, $size)
129
    {
130 1
        $response = $this->client->get(
131 1
            sprintf('/v2/user/%s/photo/%s', $userId, $size)
132 1
        );
133
134 1
        return $response;
135
    }
136
}
137