Completed
Push — master ( 980fdb...513d47 )
by Matthias
11s
created

UserAPI   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 20 %

Test Coverage

Coverage 60.42%

Importance

Changes 0
Metric Value
dl 25
loc 125
ccs 29
cts 48
cp 0.6042
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 getPrivateChatMessage() 10 10 1
A createUser() 0 7 1
A updateUser() 0 4 1
A privateMessageUser() 0 8 2
A getPhoto() 0 7 1
A getAllUsers() 0 10 2
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 7
    public function __construct(ClientInterface $client)
21
    {
22 7
        $this->client = $client;
23 7
    }
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
        }
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 1
    public function privateMessageUser($userId, $message)
83
    {
84 1
        if (is_string($message)) {
85 1
            $content = array('message' => $message);
86
        } else { // Assuming its a Message
87
            $content = $message->toJson();
88
        }
89 1
        $this->client->post(sprintf('/v2/user/%s/message', $userId), $content);
90 1
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 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
        $response = $this->client->get(
98
            sprintf('/v2/user/%s/history/latest', $userId),
99
            $parameters
100
        );
101
102
        $messages = array();
103
        foreach ($response['items'] as $response) {
104
            $messages[] = new Message($response);
105
        }
106
107
        return $messages;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 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
        $response = $this->client->get(
116
            sprintf('/v2/user/%s/history/%s', $user, $messageId),
117
            $parameters
118
        );
119
120
        $message = new Message($response['message']);
121
122
        return $message;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function getPhoto($userId, $size)
129
    {
130
        $response = $this->client->get(
131
            sprintf('/v2/user/%s/photo/%s', $userId, $size)
132
        );
133
134
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type array which is incompatible with the return type mandated by SolutionDrive\HipchatAPI...PIInterface::getPhoto() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
135
    }
136
}
137