Completed
Pull Request — master (#7)
by Louis
54:05 queued 33:43
created

Client::generatePadID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
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.7';
59
60
    private $apikey = null;
61
    private $url = null;
62
63
    /**
64
     * @param $apikey
65
     * @param string $url
66
     */
67 4
    public function __construct($apikey, $url = 'http://localhost:9001')
68
    {
69 4
        $this->apikey = $apikey;
70 4
        $this->url = $url;
71 4
    }
72
73
    /**
74
     * @param $method
75
     * @param array $args
76
     * @return Response
77
     * @throws Exception\UnsupportedMethodException
78
     */
79 3
    public function __call($method, $args = array())
80
    {
81 3
        if (!in_array($method, array_keys(self::getMethods()))) {
82 3
            throw new UnsupportedMethodException();
83
        }
84
85
        $request = new Request($this->url, $this->apikey, $method, $args);
86
        return new Response($request->send());
87
    }
88
89
    /**
90
     * Generates a random padID
91
     *
92
     * @return string
93
     */
94 1
    public function generatePadID()
95
    {
96 1
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
97 1
        $length = 16;
98 1
        $padID = "";
99
100 1
        for ($i = 0; $i < $length; $i++) {
101 1
            $padID .= $chars[rand()%strlen($chars)];
102 1
        }
103
104 1
        return $padID;
105
    }
106
107
    /**
108
     * Array that holds the available methods and their required parameter names
109
     *
110
     * @return array
111
     */
112 38
    public static function getMethods()
113
    {
114
        return array(
115 38
            'createGroup' => array(),
116 38
            'createGroupIfNotExistsFor' => array('groupMapper'),
117 38
            'deleteGroup' => array('groupID'),
118 38
            'listPads' => array('groupID'),
119 38
            'createGroupPad' => array('groupID', 'padName', 'text'),
120 38
            'listAllGroups' => array(),
121 38
            'createAuthor' => array('name'),
122 38
            'createAuthorIfNotExistsFor' => array('authorMapper', 'name'),
123 38
            'listPadsOfAuthor' => array('authorID'),
124 38
            'getAuthorName' => array('authorID'),
125 38
            'createSession' => array('groupID', 'authorID', 'validUntil'),
126 38
            'deleteSession' => array('sessionID'),
127 38
            'getSessionInfo' => array('sessionID'),
128 38
            'listSessionsOfGroup' => array('groupID'),
129 38
            'listSessionsOfAuthor' => array('authorID'),
130 38
            'getText' => array('padID', 'rev'),
131 38
            'setText' => array('padID', 'text'),
132 38
            'getHTML' => array('padID', 'rev'),
133 38
            'setHTML' => array('padID', 'html'),
134 38
            'getChatHistory' => array('padID', 'start', 'end'),
135 38
            'getChatHead' => array('padID'),
136 38
            'createPad' => array('padID', 'text'),
137 38
            'getRevisionsCount' => array('padID'),
138 38
            'padUsersCount' => array('padID'),
139 38
            'padUsers' => array('padID'),
140 38
            'deletePad' => array('padID'),
141 38
            'getReadOnlyID' => array('padID'),
142 38
            'setPublicStatus' => array('padID', 'publicStatus'),
143 38
            'getPublicStatus' => array('padID'),
144 38
            'setPassword' => array('padID', 'password'),
145 38
            'isPasswordProtected' => array('padID'),
146 38
            'listAuthorsOfPad' => array('padID'),
147 38
            'getLastEdited' => array('padID'),
148 38
            'sendClientsMessage' => array('padID', 'msg'),
149 38
            'checkToken' => array(),
150 38
            'listAllPads' => array(),
151 38
        );
152
    }
153
}
154