Permalink
Browse files

GitHub #44 - Adding GetUsers and DeleteUser command. New getUsers met…

…hod on client.
  • Loading branch information...
1 parent 6b2edba commit eb2a87edb13cb987b3cd36e688c82e01a3d7b70c @sqmk committed Apr 1, 2013
View
@@ -162,27 +162,6 @@ public function getSoftwareVersion()
}
/**
- * Get whitelisted users
- *
- * @return array List of whitelisted users
- */
- public function getWhitelist()
- {
- $whitelist = [];
-
- // Iterate through each whitelist record and add to list
- foreach ($this->details->whitelist as $username => $record) {
- $whitelist[$username] = [
- 'name' => $record->name,
- 'create_date' => $record->{'create date'},
- 'last_use_date' => $record->{'last use date'},
- ];
- }
-
- return $whitelist;
- }
-
- /**
* Is the link button on?
*
* @return bool True if the link button on, false if not
View
@@ -13,6 +13,7 @@
use Phue\Transport\TransportInterface;
use Phue\Command\CommandInterface;
use Phue\Command\GetBridge;
+use Phue\Command\GetUsers;
use Phue\Command\GetLights;
use Phue\Command\GetGroups;
use Phue\Command\GetSchedules;
@@ -111,6 +112,18 @@ public function getBridge()
}
/**
+ * Get users
+ *
+ * @return array List of User objects
+ */
+ public function getUsers()
+ {
+ return $this->sendCommand(
+ new GetUsers()
+ );
+ }
+
+ /**
* Get lights
*
* @return array List of Light objects
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Phue: Philips Hue PHP Client
+ *
+ * @author Michael Squires <sqmk@php.net>
+ * @copyright Copyright (c) 2012 Michael K. Squires
+ * @license http://github.com/sqmk/Phue/wiki/License
+ */
+
+namespace Phue\Command;
+
+use Phue\Client;
+use Phue\Transport\TransportInterface;
+
+/**
+ * Delete user command
+ */
+class DeleteUser implements CommandInterface
+{
+ /**
+ * Username
+ *
+ * @var string
+ */
+ protected $username;
+
+ /**
+ * Constructs a command
+ *
+ * @param mixed $username Username or User object
+ */
+ public function __construct($username)
+ {
+ $this->username = (string) $username;
+ }
+
+ /**
+ * Send command
+ *
+ * @param Client $client Phue Client
+ */
+ public function send(Client $client)
+ {
+ $client->getTransport()->sendRequest(
+ "{$client->getUsername()}/config/whitelist/{$this->username}",
+ TransportInterface::METHOD_DELETE
+ );
+ }
+}
@@ -0,0 +1,47 @@
+<?php
+/**
+ * Phue: Philips Hue PHP Client
+ *
+ * @author Michael Squires <sqmk@php.net>
+ * @copyright Copyright (c) 2012 Michael K. Squires
+ * @license http://github.com/sqmk/Phue/wiki/License
+ */
+
+namespace Phue\Command;
+
+use Phue\Client;
+use Phue\User;
+
+/**
+ * Get users command
+ */
+class GetUsers implements CommandInterface
+{
+ /**
+ * Send command
+ *
+ * @param Client $client Phue Client
+ *
+ * @return array List of User objects
+ */
+ public function send(Client $client)
+ {
+ // Get response
+ $response = $client->getTransport()->sendRequest(
+ "{$client->getUsername()}/config"
+ );
+
+ // Return empty list if no users
+ if (!isset($response->whitelist)) {
+ return [];
+ }
+
+ $users = [];
+
+ foreach ($response->whitelist as $username => $details) {
+ $users[$username] = new User($username, $details, $client);
+ }
+
+ return $users;
+ }
+}
View
@@ -0,0 +1,113 @@
+<?php
+/**
+ * Phue: Philips Hue PHP Client
+ *
+ * @author Michael Squires <sqmk@php.net>
+ * @copyright Copyright (c) 2012 Michael K. Squires
+ * @license http://github.com/sqmk/Phue/wiki/License
+ */
+
+namespace Phue;
+
+use Phue\Command\DeleteUser;
+
+/**
+ * User object
+ */
+class User
+{
+ /**
+ * Username
+ *
+ * @var string
+ */
+ protected $username;
+
+ /**
+ * Details
+ *
+ * @var \stdClass
+ */
+ protected $details;
+
+ /**
+ * Phue client
+ *
+ * @var Client
+ */
+ protected $client;
+
+ /**
+ * Construct a User object
+ *
+ * @param string $username Username
+ * @param \stdClass $details User details
+ * @param Client $client Phue client
+ */
+ public function __construct($username, \stdClass $details, Client $client)
+ {
+ $this->username = (string) $username;
+ $this->details = $details;
+ $this->client = $client;
+ }
+
+ /**
+ * Get username
+ *
+ * @return string Username
+ */
+ public function getUsername()
+ {
+ return $this->username;
+ }
+
+ /**
+ * Get device type
+ *
+ * @return string Device type
+ */
+ public function getDeviceType()
+ {
+ return $this->details->name;
+ }
+
+ /**
+ * Get create date
+ *
+ * @return string Create date
+ */
+ public function getCreateDate()
+ {
+ return $this->details->{'create date'};
+ }
+
+ /**
+ * Get last use date
+ *
+ * @return string Last use date
+ */
+ public function getLastUseDate()
+ {
+ return $this->details->{'last use date'};
+ }
+
+ /**
+ * Delete user
+ */
+ public function delete()
+ {
+ $this->client->sendCommand(
+ (new DeleteUser($this))
+ );
+ }
+
+ /**
+ * __toString
+ *
+ * @return string Username
+ */
+ public function __toString()
+ {
+ return (string) $this->username;
+ }
+}
@@ -213,37 +213,6 @@ public function testGetSoftwareVersion()
}
/**
- * Test: Getting whitelist
- *
- * @covers \Phue\Bridge::getWhitelist
- */
- public function testGetWhitelist()
- {
- $whitelist = $this->bridge->getWhitelist();
- $username = 'abcdefabcdef01234567890123456789';
-
- $this->assertEquals(
- count($whitelist),
- 1
- );
-
- $this->assertEquals(
- $whitelist[$username]['name'],
- $this->details->whitelist[$username]->name
- );
-
- $this->assertEquals(
- $whitelist[$username]['create_date'],
- $this->details->whitelist[$username]->{'create date'}
- );
-
- $this->assertEquals(
- $whitelist[$username]['last_use_date'],
- $this->details->whitelist[$username]->{'last use date'}
- );
- }
-
- /**
* Test: Is Link Button On?
*
* @covers \Phue\Bridge::isLinkButtonOn
@@ -86,6 +86,49 @@ public function testGetBridge()
}
/**
+ * Test: Get users
+ *
+ * @covers \Phue\Client::getUsers
+ */
+ public function testGetUsers()
+ {
+ // Mock transport
+ $mockTransport = $this->getMock(
+ '\Phue\Transport\TransportInterface',
+ ['sendRequest']
+ );
+
+ // Mock results for sendRequest
+ $mockResults = (object) [
+ 'whitelist' => [
+ 'someusername' => new \stdClass,
+ 'anotherusername' => new \stdClass,
+ 'thirdusername' => new \stdClass,
+ ]
+ ];
+
+ // Stub transports sendRequest method
+ $mockTransport->expects($this->once())
+ ->method('sendRequest')
+ ->will($this->returnValue($mockResults));
+
+ // Set transport
+ $this->client->setTransport($mockTransport);
+
+ // Get users
+ $users = $this->client->getUsers();
+
+ // Ensure at least three users
+ $this->assertEquals(3, count($users));
+
+ // Ensure return type is an array of lights
+ $this->assertContainsOnlyInstancesOf(
+ '\Phue\User',
+ $users
+ );
+ }
+
+ /**
* Test: Get lights
*
* @covers \Phue\Client::getLights
Oops, something went wrong.

0 comments on commit eb2a87e

Please sign in to comment.