Skip to content

Commit

Permalink
GitHub #95 - Selecting usernames will be deprecated in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Nov 7, 2015
1 parent 99765c4 commit c0d19ba
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 71 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ If the username provided is not created, you can use the convenience script to a
```php
// Push the bridge's link button prior to running this
try {
$client->sendCommand(
new \Phue\Command\CreateUser($client->getUsername())
$response = $client->sendCommand(
new \Phue\Command\CreateUser
);

echo 'You are now authenticated';
echo 'New user created: ' . $response->username;
} catch (\Phue\Transport\Exception\LinkButtonException $e) {
echo 'The link button was not pressed!';
}
Expand Down Expand Up @@ -491,12 +491,12 @@ If the script provided doesn't find your bridge, or if you don't have internet c

To test connectivity and authenticate with the bridge, you can use ```bin/phue-create-user```. The script uses the Phue library to make requests and receive responses from the Philips Hue bridge.

At this point, you should be ready to authenticate with the bridge. The bridge expects a username between 10 and 40 characters with alphanumeric characters to authenticate with.
At this point, you should be ready to authenticate with the bridge. The bridge will generate a username for you.

Here's how to run the script for authenticating/creating a user:

```
$ ./bin/phue-create-user 10.0.1.31 yourusername
$ ./bin/phue-create-user 10.0.1.31
```

If the connection is ok, you will get a response similar to this:
Expand All @@ -516,10 +516,10 @@ Attempting to create user:
Press the Bridge's button!
Waiting..........
Successfully created new user: yourusername
Successfully created new user: abcdef0123456
```

From then on, you should be able to use the username you just created for interacting with the Philips Hue bridge!
From then on, you should be able to use the username generated for interacting with the Philips Hue bridge!

### Scanning / registering new lights

Expand Down
10 changes: 5 additions & 5 deletions bin/phue-create-user
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../../../autoload.php';
}

// Show usage if host and username not passed
if (!isset($argv[1], $argv[2])) {
// Show usage if host is not passed
if (!isset($argv[1])) {
echo "Philips Hue User Creator", "\n\n",
"Usage:", "\n",
" <host> <username>", "\n\n";
" <host>", "\n\n";
exit(1);
}

// Initialize client
$client = new \Phue\Client($argv[1], $argv[2]);
$client = new \Phue\Client($argv[1]);

echo "Testing connection to bridge at {$client->getHost()}", "\n";

Expand All @@ -39,7 +39,7 @@ $maxTries = 30;
for ($i = 1; $i <= $maxTries; ++$i) {
try {
$response = $client->sendCommand(
new \Phue\Command\CreateUser($argv[2])
new \Phue\Command\CreateUser
);

echo "\n\n", "Successfully created new user: {$response->username}", "\n\n";
Expand Down
42 changes: 1 addition & 41 deletions library/Phue/Command/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ class CreateUser implements CommandInterface
*/
const DEFAULT_DEVICE_TYPE = 'Phue';

/**
* Username to create
*
* @var string
*/
protected $username;

/**
* Device type
*
Expand All @@ -39,41 +32,13 @@ class CreateUser implements CommandInterface
/**
* Instantiates a create user command
*
* @param string $username Username
* @param string $deviceType Device type
*/
public function __construct($username = null, $deviceType = self::DEFAULT_DEVICE_TYPE)
public function __construct($deviceType = self::DEFAULT_DEVICE_TYPE)
{
$this->setUsername($username);
$this->setDeviceType($deviceType);
}

/**
* Set username
*
* @param string $username Username
*
* @throws \InvalidArgumentException
*
* @return self This object
*/
public function setUsername($username)
{
// Allow for null username
if ($username === null) {
return;
}

// Match username format
if (!preg_match('/^[a-z0-9]{10,40}$/i', $username)) {
throw new \InvalidArgumentException(
"Username must contain alphanumeric characters, and be between 10 and 40 characters"
);
}

$this->username = $username;
}

/**
* Set device type
*
Expand Down Expand Up @@ -129,11 +94,6 @@ protected function buildRequestData(Client $client)
'devicetype' => $this->deviceType
];

// Leave username blank if one not provided
if ($this->username !== null) {
$request['username'] = (string) $this->username;
}

return (object) $request;
}
}
21 changes: 3 additions & 18 deletions tests/Phue/Test/Command/CreateUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,11 @@ public function setUp()
* Test: Instantiating CreateUser command
*
* @covers \Phue\Command\CreateUser::__construct
* @covers \Phue\Command\CreateUser::setUsername
* @covers \Phue\Command\CreateUser::setDeviceType
*/
public function testInstantiation()
{
$command = new CreateUser('testuser0123', 'phpunit');
}

/**
* Test: Setting invalid username
*
* @covers \Phue\Command\CreateUser::setUsername
*
* @expectedException \InvalidArgumentException
*/
public function testExceptionOnInvalidUsername()
{
$command = new CreateUser;
$command->setUsername('test');
$command = new CreateUser('phpunit');
}

/**
Expand All @@ -93,8 +79,7 @@ public function testExceptionOnInvalidDeviceType()
*/
public function testSend()
{
// Set up username and device type to pass to create user command
$username = 'testuser0123';
// Set up device type to pass to create user command
$deviceType = 'phpunit';

// Stub transport's sendRequest method
Expand All @@ -109,7 +94,7 @@ public function testSend()

$this->assertEquals(
'success!',
(new CreateUser('testuser0123', 'phpunit'))->send($this->mockClient)
(new CreateUser('phpunit'))->send($this->mockClient)
);
}
}

0 comments on commit c0d19ba

Please sign in to comment.