Completed
Push — update-php-7-0 ( d0a860 )
by Louis
23:07 queued 08:09
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 117
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 10 2
A generatePadID() 0 12 2
A getMethods() 0 53 1
1
<?php
2
3
namespace EtherpadLite;
4
5
use EtherpadLite\Exception\UnsupportedMethodException;
6
7
/**
8
 * Class Client
9
 *
10
 * @package EtherpadLite
11
 *
12
 * @method Response createGroup() creates a new group
13
 * @method Response createGroupIfNotExistsFor($groupMapper) this functions helps you to map your application group ids to etherpad lite group ids
14
 * @method Response deleteGroup($groupID) deletes a group
15
 * @method Response listPads($groupID) returns all pads of this group
16
 * @method Response createGroupPad($groupID, $padName, $text = null) creates a new pad in this group
17
 * @method Response listAllGroups() lists all existing groups
18
 *
19
 * @method Response createAuthor($name = null) creates a new author
20
 * @method Response createAuthorIfNotExistsFor($authorMapper, $name = null) this functions helps you to map your application author ids to etherpad lite author ids
21
 * @method Response listPadsOfAuthor($authorID) returns an array of all pads this author contributed to
22
 * @method Response getAuthorName($authorID) Returns the Author Name of the author
23
 *
24
 * @method Response createSession($groupID, $authorID, $validUntil) creates a new session. validUntil is an unix timestamp in seconds
25
 * @method Response deleteSession($sessionID) deletes a session by id
26
 * @method Response getSessionInfo($sessionID) returns informations about a session
27
 * @method Response listSessionsOfGroup($groupID) returns all sessions of a group
28
 * @method Response listSessionsOfAuthor($authorID) returns all sessions of an author
29
 *
30
 * @method Response getText($padID, $rev = null) returns the text of a pad
31
 * @method Response setText($padID, $text) sets the text of a pad
32
 * @method Response appendText($padID, $text) appends text to a pad
33
 * @method Response getHTML($padID, $rev = null) returns the text of a pad formatted as HTML
34
 * @method Response setHTML($padID, $html) sets the html of a pad
35
 * @method Response getAttributePool($padID) returns the attribute pool of a pad
36
 * @method Response getRevisionChangeset($padID, $revision = null) get the changeset at a given revision, or last revision if 'rev' is not defined.
37
 * @method Response createDiffHTML($padID, $startRev, $endRev) returns an object of diffs from 2 points in a pad
38
 * @method Response restoreRevision($padID, $revision) restores revision from past as new changeset
39
 *
40
 * @method Response getChatHistory($padID, $start = null, $end = null) a part of the chat history, when start and end are given, the whole chat histroy, when no extra parameters are given
41
 * @method Response getChatHead($padID) returns the chatHead (last number of the last chat-message) of the pad
42
 * @method Response appendChatMessage($padID, $text, $authorID, $time = null) creates a chat message, saves it to the database and sends it to all connected clients of this pad
43
 *
44
 * @method Response createPad($padID, $text = null) creates a new (non-group) pad. Note that if you need to create a group Pad, you should call createGroupPad.
45
 * @method Response getRevisionsCount($padID) returns the number of revisions of this pad
46
 * @method Response getSavedRevisionsCount($padID) returns the number of saved revisions of this pad
47
 * @method Response listSavedRevisions($padID) returns a list of saved revisions of this pad
48
 * @method Response saveRevision($padID, $revision = null) save a revision of a pad
49
 * @method Response padUsersCount($padID) returns the number of user that are currently editing this pad
50
 * @method Response padUsers($padID) returns the list of users that are currently editing this pad
51
 * @method Response deletePad($padID) deletes a pad
52
 * @method Response movePad($sourceID, $destinationID, $force = false) moves a pad. If force is true and the destination pad exists, it will be overwritten.
53
 * @method Response copyPad($sourceID, $destinationID, $force = false) copies a pad with full history and chat. If force is true and the destination pad exists, it will be overwritten.
54
 * @method Response getReadOnlyID($padID) returns the read only link of a pad
55
 * @method Response getPadID($readOnlyID) returns the id of a pad which is assigned to the readOnlyID
56
 * @method Response setPublicStatus($padID, $publicStatus) sets a boolean for the public status of a pad
57
 * @method Response getPublicStatus($padID) return true of false
58
 * @method Response setPassword($padID, $password) returns ok or a error message
59
 * @method Response isPasswordProtected($padID) returns true or false
60
 * @method Response listAuthorsOfPad($padID) returns an array of authors who contributed to this pad
61
 * @method Response getLastEdited($padID) returns the timestamp of the last revision of the pad
62
 * @method Response sendClientsMessage($padID, $msg) sends a custom message of type $msg to the pad
63
 * @method Response checkToken() returns ok when the current api token is valid
64
 *
65
 * @method Response listAllPads() lists all pads on this epl instance
66
 *
67
 */
68
class Client
69
{
70
    const API_VERSION = '1.2.13';
71
72
    /**
73
     * @var string|null
74
     */
75
    private $apikey = null;
76
    /**
77
     * @var string|null
78
     */
79
    private $url = null;
80
81
    /**
82
     * @param string $apikey
83
     * @param string $url
84
     */
85 4
    public function __construct(string $apikey, string $url = 'http://localhost:9001')
86
    {
87 4
        $this->apikey = $apikey;
88 4
        $this->url = $url;
89 4
    }
90
91
    /**
92
     * @param string $method
93
     * @param array $args
94
     * @return Response
95
     * @throws Exception\UnsupportedMethodException
96
     */
97 3
    public function __call(string $method, $args = []): Response
98
    {
99 3
        if (!in_array($method, array_keys(self::getMethods()))) {
100 3
            throw new UnsupportedMethodException();
101
        }
102
103
        $request = new Request($this->url, $this->apikey, $method, $args);
104
105
        return new Response($request->send());
106
    }
107
108
    /**
109
     * Generates a random padID
110
     *
111
     * @return string
112
     */
113 1
    public function generatePadID(): string
114
    {
115 1
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
116 1
        $length = 16;
117 1
        $padID = "";
118
119 1
        for ($i = 0; $i < $length; $i++) {
120 1
            $padID .= $chars[rand() % strlen($chars)];
121
        }
122
123 1
        return $padID;
124
    }
125
126
    /**
127
     * Array that holds the available methods and their required parameter names
128
     *
129
     * @return array
130
     */
131 38
    public static function getMethods(): array
132
    {
133
        return [
134 38
            'createGroup' => [],
135
            'createGroupIfNotExistsFor' => ['groupMapper'],
136
            'deleteGroup' => ['groupID'],
137
            'listPads' => ['groupID'],
138
            'createGroupPad' => ['groupID', 'padName', 'text'],
139
            'listAllGroups' => [],
140
            'createAuthor' => ['name'],
141
            'createAuthorIfNotExistsFor' => ['authorMapper', 'name'],
142
            'listPadsOfAuthor' => ['authorID'],
143
            'getAuthorName' => ['authorID'],
144
            'createSession' => ['groupID', 'authorID', 'validUntil'],
145
            'deleteSession' => ['sessionID'],
146
            'getSessionInfo' => ['sessionID'],
147
            'listSessionsOfGroup' => ['groupID'],
148
            'listSessionsOfAuthor' => ['authorID'],
149
            'getText' => ['padID', 'rev'],
150
            'setText' => ['padID', 'text'],
151
            'appendText' => ['padID', 'text'],
152
            'getHTML' => ['padID', 'rev'],
153
            'setHTML' => ['padID', 'html'],
154
            'getAttributePool' => ['padID'],
155
            'createDiffHTML' => ['padID', 'startRev', 'endRev'],
156
            'restoreRevision' => ['padID', 'rev'],
157
            'getRevisionChangeset' => ['padID'],
158
            'getChatHistory' => ['padID', 'start', 'end'],
159
            'getChatHead' => ['padID'],
160
            'appendChatMessage' => ['padID', 'text', 'authorID'],
161
            'createPad' => ['padID', 'text'],
162
            'getRevisionsCount' => ['padID'],
163
            'getSavedRevisionsCount' => ['padID'],
164
            'listSavedRevisions' => ['padID'],
165
            'saveRevision' => ['padID'],
166
            'padUsersCount' => ['padID'],
167
            'padUsers' => ['padID'],
168
            'deletePad' => ['padID'],
169
            'movePad' => ['sourceID', 'destinationID'],
170
            'copyPad' => ['sourceID', 'destinationID'],
171
            'getReadOnlyID' => ['padID'],
172
            'getPadID' => ['readOnlyID'],
173
            'setPublicStatus' => ['padID', 'publicStatus'],
174
            'getPublicStatus' => ['padID'],
175
            'setPassword' => ['padID', 'password'],
176
            'isPasswordProtected' => ['padID'],
177
            'listAuthorsOfPad' => ['padID'],
178
            'getLastEdited' => ['padID'],
179
            'sendClientsMessage' => ['padID', 'msg'],
180
            'checkToken' => [],
181
            'listAllPads' => [],
182
        ];
183
    }
184
}
185