forked from nickbart/php-plex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.php
167 lines (155 loc) · 4.09 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Plex Server
*
* @category php-plex
* @package Plex_Server
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
*
* This file is part of php-plex.
*
* php-plex is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* php-plex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Represents a Plex server on the network.
*
* @category php-plex
* @package Plex_Server
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
*/
class Plex_Server extends Plex_MachineAbstract
{
/**
* The default port on which a Plex server listens.
*/
const DEFAULT_PORT = 32400;
/**
* The Plex HTTP API endpoint for client listing.
*/
const ENDPOINT_CLIENT = 'clients';
/**
* Sets up our Plex server using the minimum amount of data required to
* interact.
*
* @param string $name The name of the Plex server.
* @param string $address The IP address of the Plex server.
* @param integer $port The port on which the Plex server is listening.
*
* @uses Plex_MachineAbstract::$name
* @uses Plex_MachineAbstract::$address
* @uses Plex_MachineAbstract::$port
* @uses Plex_Server::DEFAULT_PORT
*
* @return void
*/
public function __construct($name, $address, $port)
{
$this->name = $name;
$this->address = $address;
$this->port = $port ? $port : self::DEFAULT_PORT;
}
/**
* Returns all the availalble clients to which the Plex server has access
* inedxed by the Plex client name.
*
* @uses Plex_MachineAbstract::getBaseUrl()
* @uses Plex_MachineAbstract::makeCall()
* @uses Plex_MachineAbstract::xmlAttributesToArray()
* @uses Plex_Server::ENDPOINT_CLIENT
* @uses Plex_Client::setHost()
* @uses Plex_Client::setMachineIdentifier()
* @uses Plex_Client::setVersion()
* @uses Plex_Client::setServer()
*
* @return Plex_Client[] An array of Plex clients indexed by the Plex client
* name.
*/
public function getClients()
{
$url = sprintf(
'%s/%s',
$this->getBaseUrl(),
self::ENDPOINT_CLIENT
);
$clients = array();
$clientArray = $this->makeCall($url);
foreach ($clientArray as $attribute) {
$client = new Plex_Client(
$attribute['name'],
$attribute['address'],
(int) $attribute['port']
);
$client->setHost($attribute['host']);
$client->setMachineIdentifier($attribute['machineIdentifier']);
$client->setVersion($attribute['version']);
$client->setServer($this);
$clients[$attribute['name']] = $client;
}
return $clients;
}
/**
* Returns the Plex library belonging to the instantiatied Plex server.
*
* @uses Plex_MachineAbstract::$name
* @uses Plex_MachineAbstract::$address
* @uses Plex_MachineAbstract::$port
*
* @return Plex_Server_Library The Plex library belonging to the
* instantiated Plex server.
*/
public function getLibrary()
{
return new Plex_Server_Library(
$this->name,
$this->address,
$this->port
);
}
/**
* Returns the Plex server's name.
*
* @uses Plex_MachineAbstract::$name
*
* @return string The name of the Plex server.
*/
public function getName()
{
return $this->name;
}
/**
* Returns the Plex server's IP address.
*
* @uses Plex_MachineAbstract::$address
*
* @return string The IP address of the Plex server.
*/
public function getAddress()
{
return $this->address;
}
/**
* Returns the port on which the Plex server listens.
*
* @uses Plex_MachineAbstract::$port
*
* @return integer The port on which the Plex client server.
*/
public function getPort()
{
return $this->port;
}
}