Client::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 5
cp 0.6
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.2559
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 getHTML($padID, $rev = null) returns the text of a pad formatted as HTML
33
 * @method Response setHTML($padID, $html) sets the HTML of a pad
34
 *
35
 * @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
36
 * @method Response getChatHead($padID) returns the chatHead (last number of the last chat-message) of the pad
37
 *
38
 * @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.
39
 * @method Response getRevisionsCount($padID) returns the number of revisions of this pad
40
 * @method Response padUsersCount($padID) returns the number of user that are currently editing this pad
41
 * @method Response padUsers($padID) returns the list of users that are currently editing this pad
42
 * @method Response deletePad($padID) deletes a pad
43
 * @method Response getReadOnlyID($padID) returns the read only link of a pad
44
 * @method Response setPublicStatus($padID, $publicStatus) sets a boolean for the public status of a pad
45
 * @method Response getPublicStatus($padID) return true of false
46
 * @method Response setPassword($padID, $password) returns ok or a error message
47
 * @method Response isPasswordProtected($padID) returns true or false
48
 * @method Response listAuthorsOfPad($padID) returns an array of authors who contributed to this pad
49
 * @method Response getLastEdited($padID) returns the timestamp of the last revision of the pad
50
 * @method Response sendClientsMessage($padID, $msg) sends a custom message of type $msg to the pad
51
 * @method Response checkToken() returns ok when the current api token is valid
52
 *
53
 * @method Response listAllPads() lists all pads on this epl instance
54
 *
55
 */
56
class Client
57
{
58
    const API_VERSION = '1.2.13';
59
60
    /**
61
     * @var string|null
62
     */
63
    private $apikey = null;
64
    /**
65
     * @var string|null
66
     */
67
    private $url = null;
68
69
    /**
70
     * @param string $apikey
71
     * @param string $url
72
     */
73 4
    public function __construct(string $apikey, string $url = 'http://localhost:9001')
74
    {
75 4
        $this->apikey = $apikey;
76 4
        $this->url = $url;
77 4
    }
78
79
    /**
80
     * @param string $method
81
     * @param array $args
82
     * @return Response
83
     * @throws Exception\UnsupportedMethodException
84
     */
85 3
    public function __call(string $method, $args = []): Response
86
    {
87 3
        if (!in_array($method, array_keys(self::getMethods()))) {
88 3
            throw new UnsupportedMethodException();
89
        }
90
91
        $request = new Request($this->url, $this->apikey, $method, $args);
92
93
        return new Response($request->send());
94
    }
95
96
    /**
97
     * Generates a random padID
98
     *
99
     * @return string
100
     */
101 1
    public function generatePadID(): string
102
    {
103 1
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
104 1
        $length = 16;
105 1
        $padID = "";
106
107 1
        for ($i = 0; $i < $length; $i++) {
108 1
            $padID .= $chars[rand() % strlen($chars)];
109
        }
110
111 1
        return $padID;
112
    }
113
114
    /**
115
     * Array that holds the available methods and their required parameter names
116
     *
117
     * @return array
118
     */
119 39
    public static function getMethods(): array
120
    {
121
        return [
122 39
            'createGroup' => [],
123
            'createGroupIfNotExistsFor' => ['groupMapper'],
124
            'deleteGroup' => ['groupID'],
125
            'listPads' => ['groupID'],
126
            'createGroupPad' => ['groupID', 'padName', 'text'],
127
            'listAllGroups' => [],
128
            'createAuthor' => ['name'],
129
            'createAuthorIfNotExistsFor' => ['authorMapper', 'name'],
130
            'listPadsOfAuthor' => ['authorID'],
131
            'getAuthorName' => ['authorID'],
132
            'createSession' => ['groupID', 'authorID', 'validUntil'],
133
            'deleteSession' => ['sessionID'],
134
            'getSessionInfo' => ['sessionID'],
135
            'listSessionsOfGroup' => ['groupID'],
136
            'listSessionsOfAuthor' => ['authorID'],
137
            'getText' => ['padID', 'rev'],
138
            'setText' => ['padID', 'text'],
139
            'getHTML' => ['padID', 'rev'],
140
            'setHTML' => ['padID', 'html'],
141
            'getAttributePool' => ['padID'],
142
            'getChatHistory' => ['padID', 'start', 'end'],
143
            'getChatHead' => ['padID'],
144
            'createPad' => ['padID', 'text'],
145
            'getRevisionsCount' => ['padID'],
146
            'listSavedRevisions' => ['padID'],
147
            'padUsersCount' => ['padID'],
148
            'padUsers' => ['padID'],
149
            'deletePad' => ['padID'],
150
            'getReadOnlyID' => ['padID'],
151
            'setPublicStatus' => ['padID', 'publicStatus'],
152
            'getPublicStatus' => ['padID'],
153
            'setPassword' => ['padID', 'password'],
154
            'isPasswordProtected' => ['padID'],
155
            'listAuthorsOfPad' => ['padID'],
156
            'getLastEdited' => ['padID'],
157
            'sendClientsMessage' => ['padID', 'msg'],
158
            'checkToken' => [],
159
            'listAllPads' => [],
160
        ];
161
    }
162
}
163