Completed
Push — master ( 88332e...572bb1 )
by Adam
02:34
created

ClientCommand::setLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * ClientCommand.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:MQTTClient!
9
 * @subpackage     Commands
10
 * @since          1.0.0
11
 *
12
 * @date           12.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MQTTClient\Commands;
18
19
use Symfony\Component\Console;
20
use Symfony\Component\Console\Input;
21
use Symfony\Component\Console\Style;
22
use Symfony\Component\Console\Output;
23
24
use Psr\Log;
25
26
use BinSoul\Net\Mqtt;
27
28
use IPub\MQTTClient\Client;
29
use IPub\MQTTClient\Logger;
30
31
/**
32
 * MQTT client command
33
 *
34
 * @package        iPublikuj:MQTTClient!
35
 * @subpackage     Commands
36
 *
37
 * @author         Adam Kadlec <[email protected]>
38
 */
39 1
class ClientCommand extends Console\Command\Command
40
{
41
	/**
42
	 * @var Client\IClient
43
	 */
44
	private $client;
45
46
	/**
47
	 * @var Log\LoggerInterface|Log\NullLogger|NULL
48
	 */
49
	private $logger;
50
51
	/**
52
	 * @var bool
53
	 */
54
	private $isRunning = FALSE;
55
56
	/**
57
	 * @param Client\IClient $client
58
	 * @param Log\LoggerInterface|NULL $logger
59
	 * @param string|NULL $name
60
	 */
61
	public function __construct(
62
		Client\IClient $client,
63
		Log\LoggerInterface $logger = NULL,
64
		string $name = NULL
65
	) {
66
		parent::__construct($name);
67
68
		$this->client = $client;
69
		$this->logger = $logger === NULL ? new Log\NullLogger : $logger;
70
	}
71
72
	/**
73
	 * @param Log\LoggerInterface $logger
74
	 *
75
	 * @return void
76
	 */
77
	public function setLogger(Log\LoggerInterface $logger) : void
78
	{
79
		if ($this->isRunning) {
80
			throw new Exceptions\InvalidStateException('Client is connected, logger could not be set.');
81
		}
82
83
		$this->logger = $logger;
84
	}
85
86
	/**
87
	 * @return void
88
	 */
89
	protected function configure()
90
	{
91
		$this
92
			->setName('ipub:mqttclient:start')
93
			->setDescription('Start MQTT client.');
94
	}
95
96
	/**
97
	 * {@inheritdoc}
98
	 */
99
	protected function execute(Input\InputInterface $input, Output\OutputInterface $output)
100
	{
101
		$io = new Style\SymfonyStyle($input, $output);
102
103
		$io->text([
104
			'',
105
			'+-------------+',
106
			'| MQTT client |',
107
			'+-------------+',
108
			'',
109
		]);
110
111
		if ($this->logger instanceof Logger\Console) {
112
			$this->logger->setFormatter(new Logger\Formatter\Symfony($io));
113
		}
114
115
		$this->client->onOpen[] = (function (Mqtt\Connection $connection, Client\Client $client) {
0 ignored issues
show
Bug introduced by
Accessing onOpen on the interface IPub\MQTTClient\Client\IClient suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
116
			$this->logger->debug(sprintf('Connection to %s opened', $client->getUri()));
117
		});
118
119
		$this->client->connect();
120
121
		$this->isRunning = TRUE;
122
123
		$this->client->getLoop()->run();
124
	}
125
}
126