Skip to content

Commit c0d19ba

Browse files
committed
GitHub #95 - Selecting usernames will be deprecated in the future
1 parent 99765c4 commit c0d19ba

File tree

4 files changed

+16
-71
lines changed

4 files changed

+16
-71
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ If the username provided is not created, you can use the convenience script to a
102102
```php
103103
// Push the bridge's link button prior to running this
104104
try {
105-
$client->sendCommand(
106-
new \Phue\Command\CreateUser($client->getUsername())
105+
$response = $client->sendCommand(
106+
new \Phue\Command\CreateUser
107107
);
108108

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

492492
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.
493493

494-
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.
494+
At this point, you should be ready to authenticate with the bridge. The bridge will generate a username for you.
495495

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

498498
```
499-
$ ./bin/phue-create-user 10.0.1.31 yourusername
499+
$ ./bin/phue-create-user 10.0.1.31
500500
```
501501

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

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

524524
### Scanning / registering new lights
525525

bin/phue-create-user

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ if (is_file(__DIR__ . '/../vendor/autoload.php')) {
77
require_once __DIR__ . '/../../../autoload.php';
88
}
99

10-
// Show usage if host and username not passed
11-
if (!isset($argv[1], $argv[2])) {
10+
// Show usage if host is not passed
11+
if (!isset($argv[1])) {
1212
echo "Philips Hue User Creator", "\n\n",
1313
"Usage:", "\n",
14-
" <host> <username>", "\n\n";
14+
" <host>", "\n\n";
1515
exit(1);
1616
}
1717

1818
// Initialize client
19-
$client = new \Phue\Client($argv[1], $argv[2]);
19+
$client = new \Phue\Client($argv[1]);
2020

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

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

4545
echo "\n\n", "Successfully created new user: {$response->username}", "\n\n";

library/Phue/Command/CreateUser.php

+1-41
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ class CreateUser implements CommandInterface
2222
*/
2323
const DEFAULT_DEVICE_TYPE = 'Phue';
2424

25-
/**
26-
* Username to create
27-
*
28-
* @var string
29-
*/
30-
protected $username;
31-
3225
/**
3326
* Device type
3427
*
@@ -39,41 +32,13 @@ class CreateUser implements CommandInterface
3932
/**
4033
* Instantiates a create user command
4134
*
42-
* @param string $username Username
4335
* @param string $deviceType Device type
4436
*/
45-
public function __construct($username = null, $deviceType = self::DEFAULT_DEVICE_TYPE)
37+
public function __construct($deviceType = self::DEFAULT_DEVICE_TYPE)
4638
{
47-
$this->setUsername($username);
4839
$this->setDeviceType($deviceType);
4940
}
5041

51-
/**
52-
* Set username
53-
*
54-
* @param string $username Username
55-
*
56-
* @throws \InvalidArgumentException
57-
*
58-
* @return self This object
59-
*/
60-
public function setUsername($username)
61-
{
62-
// Allow for null username
63-
if ($username === null) {
64-
return;
65-
}
66-
67-
// Match username format
68-
if (!preg_match('/^[a-z0-9]{10,40}$/i', $username)) {
69-
throw new \InvalidArgumentException(
70-
"Username must contain alphanumeric characters, and be between 10 and 40 characters"
71-
);
72-
}
73-
74-
$this->username = $username;
75-
}
76-
7742
/**
7843
* Set device type
7944
*
@@ -129,11 +94,6 @@ protected function buildRequestData(Client $client)
12994
'devicetype' => $this->deviceType
13095
];
13196

132-
// Leave username blank if one not provided
133-
if ($this->username !== null) {
134-
$request['username'] = (string) $this->username;
135-
}
136-
13797
return (object) $request;
13898
}
13999
}

tests/Phue/Test/Command/CreateUserTest.php

+3-18
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,11 @@ public function setUp()
5151
* Test: Instantiating CreateUser command
5252
*
5353
* @covers \Phue\Command\CreateUser::__construct
54-
* @covers \Phue\Command\CreateUser::setUsername
5554
* @covers \Phue\Command\CreateUser::setDeviceType
5655
*/
5756
public function testInstantiation()
5857
{
59-
$command = new CreateUser('testuser0123', 'phpunit');
60-
}
61-
62-
/**
63-
* Test: Setting invalid username
64-
*
65-
* @covers \Phue\Command\CreateUser::setUsername
66-
*
67-
* @expectedException \InvalidArgumentException
68-
*/
69-
public function testExceptionOnInvalidUsername()
70-
{
71-
$command = new CreateUser;
72-
$command->setUsername('test');
58+
$command = new CreateUser('phpunit');
7359
}
7460

7561
/**
@@ -93,8 +79,7 @@ public function testExceptionOnInvalidDeviceType()
9379
*/
9480
public function testSend()
9581
{
96-
// Set up username and device type to pass to create user command
97-
$username = 'testuser0123';
82+
// Set up device type to pass to create user command
9883
$deviceType = 'phpunit';
9984

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

11095
$this->assertEquals(
11196
'success!',
112-
(new CreateUser('testuser0123', 'phpunit'))->send($this->mockClient)
97+
(new CreateUser('phpunit'))->send($this->mockClient)
11398
);
11499
}
115100
}

0 commit comments

Comments
 (0)