ClientCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 12
cp 0
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\Exceptions;
30
use IPub\MQTTClient\Logger;
31
32
/**
33
 * MQTT client command
34
 *
35
 * @package        iPublikuj:MQTTClient!
36
 * @subpackage     Commands
37
 *
38
 * @author         Adam Kadlec <[email protected]>
39
 */
40 1
class ClientCommand extends Console\Command\Command
41
{
42
	/**
43
	 * @var Client\IClient
44
	 */
45
	private $client;
46
47
	/**
48
	 * @var Log\LoggerInterface|Log\NullLogger|NULL
49
	 */
50
	private $logger;
51
52
	/**
53
	 * @var bool
54
	 */
55
	private $isRunning = FALSE;
56
57
	/**
58
	 * @param Client\IClient $client
59
	 * @param Log\LoggerInterface|NULL $logger
60
	 * @param string|NULL $name
61
	 */
62
	public function __construct(
63
		Client\IClient $client,
64
		Log\LoggerInterface $logger = NULL,
65
		string $name = NULL
66
	) {
67
		parent::__construct($name);
68
69
		$this->client = $client;
70
		$this->logger = $logger === NULL ? new Log\NullLogger : $logger;
71
	}
72
73
	/**
74
	 * @param Log\LoggerInterface $logger
75
	 *
76
	 * @return void
77
	 *
78
	 * @throws Exceptions\InvalidStateException
79
	 */
80
	public function setLogger(Log\LoggerInterface $logger) : void
81
	{
82
		if ($this->isRunning) {
83
			throw new Exceptions\InvalidStateException('Client is connected, logger could not be set.');
84
		}
85
86
		$this->logger = $logger;
87
	}
88
89
	/**
90
	 * @return void
91
	 */
92
	protected function configure()
93
	{
94
		$this
95
			->setName('ipub:mqttclient:start')
96
			->setDescription('Start MQTT client.');
97
	}
98
99
	/**
100
	 * {@inheritdoc}
101
	 */
102
	protected function execute(Input\InputInterface $input, Output\OutputInterface $output)
103
	{
104
		$io = new Style\SymfonyStyle($input, $output);
105
106
		$io->text([
107
			'',
108
			'+-------------+',
109
			'| MQTT client |',
110
			'+-------------+',
111
			'',
112
		]);
113
114
		if ($this->logger instanceof Logger\Console) {
115
			$this->logger->setFormatter(new Logger\Formatter\Symfony($io));
116
		}
117
118
		$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...
119
			$this->logger->debug(sprintf('Connection to %s opened', $client->getUri()));
120
		});
121
122
		$this->client->connect();
123
124
		$this->isRunning = TRUE;
125
126
		$this->client->getLoop()->run();
127
	}
128
}
129