Skip to content

Commit 44ff9ba

Browse files
authored
Merge pull request #25 from jjok/client-factory
Add client factory
2 parents 1bf0027 + 6342673 commit 44ff9ba

File tree

5 files changed

+94
-60
lines changed

5 files changed

+94
-60
lines changed

README.md

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,38 @@
11
# PHP MQTT Client
22

3-
phpMqttClient is a mqtt client library for PHP. Its based on the reactPHP socket-client and added the mqtt protocol specific functions. I hope its a better starting point that the existing php mqtt libraries.
3+
phpMqttClient is an MQTT client library for PHP. Its based on the reactPHP socket-client and added the MQTT protocol
4+
specific functions. I hope its a better starting point that the existing PHP MQTT libraries.
45

56
[![Build Status](https://travis-ci.org/oliverlorenz/phpMqttClient.svg?branch=master)](https://travis-ci.org/oliverlorenz/phpMqttClient)
67
[![Code Climate](https://codeclimate.com/github/oliverlorenz/phpMqttClient/badges/gpa.svg)](https://codeclimate.com/github/oliverlorenz/phpMqttClient)
78
[![Test Coverage](https://codeclimate.com/github/oliverlorenz/phpMqttClient/badges/coverage.svg)](https://codeclimate.com/github/oliverlorenz/phpMqttClient/coverage)
89

9-
### Notice - (May 12th, 2015)
10-
This is library is not stable currently. Its an early state, but I am working on it. I will add more features if I need them. If you need features: please give feedback or contribute to get this library running.
11-
12-
Currently works:
13-
* connect (clean session, no other connect flags)
14-
* disconnect
15-
* publish
16-
* subscribe
17-
1810
## Goal
1911

20-
Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules. Currently, only protocol version 4 (mqtt 3.1.1) is implemented.
12+
Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules.
13+
Currently, only protocol version 4 (mqtt 3.1.1) is implemented.
2114
* Protocol specifications: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html
2215

2316
## Example publish
2417

2518
```php
26-
<?php
27-
28-
require __DIR__ . '/../vendor/autoload.php';
29-
30-
$config = include('config.php');
19+
$config = require 'config.php';
3120

32-
$loop = React\EventLoop\Factory::create();
33-
34-
$dnsResolverFactory = new React\Dns\Resolver\Factory();
35-
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
36-
37-
$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
38-
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
21+
$connector = ClientFactory::createClient(new Version4());
3922

4023
$p = $connector->create($config['server'], $config['port'], $config['options']);
41-
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
24+
$p->then(function(Stream $stream) use ($connector) {
4225
return $connector->publish($stream, 'a/b', 'example message');
4326
});
44-
$loop->run();
27+
$connector->getLoop()->run();
4528
```
4629

4730
## Example subscribe
4831

4932
```php
50-
<?php
33+
$config = require 'config.php';
5134

52-
use oliverlorenz\reactphpmqtt\packet\Publish;
53-
54-
require __DIR__ . '/../vendor/autoload.php';
55-
56-
$config = include('config.php');
57-
58-
$loop = React\EventLoop\Factory::create();
59-
60-
$dnsResolverFactory = new React\Dns\Resolver\Factory();
61-
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
62-
63-
$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
64-
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
35+
$connector = ClientFactory::createClient(new Version4());
6536

6637
$p = $connector->create($config['server'], $config['port'], $config['options']);
6738
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
@@ -73,9 +44,18 @@ $p->then(function(\React\Stream\Stream $stream) use ($connector) {
7344
$connector->subscribe($stream, 'a/c', 0);
7445
});
7546

76-
$loop->run();
47+
$connector->getLoop()->run();
7748
```
7849

50+
## Notice - (May 12th, 2015)
51+
This is library is not stable currently. Its an early state, but I am working on it. I will add more features if I need them. If you need features: please give feedback or contribute to get this library running.
52+
53+
Currently works:
54+
* connect (clean session, no other connect flags)
55+
* disconnect
56+
* publish
57+
* subscribe
58+
7959
## Run tests
8060

8161
./vendor/bin/phpunit -c ./tests/phpunit.xml ./tests

examples/connectPublish.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
4-
5-
$config = include('config.php');
3+
use oliverlorenz\reactphpmqtt\ClientFactory;
4+
use oliverlorenz\reactphpmqtt\protocol\Version4;
5+
use React\Stream\Stream;
66

7-
$loop = React\EventLoop\Factory::create();
7+
require __DIR__ . '/../vendor/autoload.php';
88

9-
$dnsResolverFactory = new React\Dns\Resolver\Factory();
10-
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
9+
$config = require 'config.php';
1110

12-
$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
13-
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
11+
$connector = ClientFactory::createClient(new Version4(), '8.8.8.8');
1412

1513
$p = $connector->create($config['server'], $config['port'], $config['options']);
16-
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
14+
$p->then(function(Stream $stream) use ($connector) {
1715
return $connector->publish($stream, 'hello/world', 'example message');
1816
});
1917

20-
$loop->run();
18+
$connector->getLoop()->run();

examples/connectSubscribe.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<?php
22

3+
use oliverlorenz\reactphpmqtt\ClientFactory;
34
use oliverlorenz\reactphpmqtt\packet\Publish;
5+
use oliverlorenz\reactphpmqtt\protocol\Version4;
6+
use React\Stream\Stream;
47

58
require __DIR__ . '/../vendor/autoload.php';
69

7-
$config = include('config.php');
10+
$config = require 'config.php';
811

9-
$loop = React\EventLoop\Factory::create();
10-
11-
$dnsResolverFactory = new React\Dns\Resolver\Factory();
12-
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
13-
14-
$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
15-
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
12+
$connector = ClientFactory::createClient(new Version4(), '8.8.8.8');
1613

1714
$p = $connector->create($config['server'], $config['port'], $config['options']);
18-
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
15+
$p->then(function(Stream $stream) use ($connector) {
1916
$stream->on(Publish::EVENT, function(Publish $message) {
2017
printf(
2118
'Received payload "%s" for topic "%s"%s',
@@ -28,4 +25,4 @@
2825
return $connector->subscribe($stream, 'hello/world', 0);
2926
});
3027

31-
$loop->run();
28+
$connector->getLoop()->run();

src/ClientFactory.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace oliverlorenz\reactphpmqtt;
4+
5+
use oliverlorenz\reactphpmqtt\protocol\Version;
6+
use React\Dns\Resolver\Factory as DnsResolverFactory;
7+
use React\EventLoop\Factory as EventLoopFactory;
8+
9+
class ClientFactory
10+
{
11+
public static function createClient(Version $version, $resolverIp = '8.8.8.8')
12+
{
13+
$loop = EventLoopFactory::create();
14+
$resolver = self::createDnsResolver($resolverIp, $loop);
15+
16+
return new Connector($loop, $resolver, $version);
17+
}
18+
19+
public static function createSecureClient(Version $version, $resolverIp = '8.8.8.8')
20+
{
21+
$loop = EventLoopFactory::create();
22+
$resolver = self::createDnsResolver($resolverIp, $loop);
23+
24+
return new SecureConnector($loop, $resolver, $version);
25+
}
26+
27+
private static function createDnsResolver($resolverIp, $loop)
28+
{
29+
$dnsResolverFactory = new DnsResolverFactory();
30+
31+
return $dnsResolverFactory->createCached($resolverIp, $loop);
32+
}
33+
}

tests/unit/ClientFactoryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace oliverlorenz\reactphpmqtt;
4+
5+
use oliverlorenz\reactphpmqtt\protocol\Version4;
6+
use PHPUnit_Framework_TestCase as TestCase;
7+
8+
/**
9+
* @covers \oliverlorenz\reactphpmqtt\ClientFactory
10+
*/
11+
final class ClientFactoryTest extends TestCase
12+
{
13+
public function testConnectorCanBeCreated()
14+
{
15+
$client = ClientFactory::createClient(new Version4(), '8.8.8.8');
16+
17+
$this->assertInstanceOf('oliverlorenz\reactphpmqtt\Connector', $client);
18+
}
19+
20+
public function testSecureConnectorCanBeCreated()
21+
{
22+
$client = ClientFactory::createSecureClient(new Version4(), '8.8.8.8');
23+
24+
$this->assertInstanceOf('oliverlorenz\reactphpmqtt\SecureConnector', $client);
25+
}
26+
}

0 commit comments

Comments
 (0)