GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

YeelightRawClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A search() 0 12 2
A formatResponse() 0 9 1
1
<?php
2
3
namespace Yeelight;
4
5
use Socket\Raw\Socket;
6
use Yeelight\Bulb\Bulb;
7
use Yeelight\Bulb\BulbFactory;
8
use Yeelight\Exceptions\SocketException;
9
10
class YeelightRawClient
11
{
12
    const DISCOVERY_RESPONSE = "M-SEARCH * HTTP/1.1\r\n
13
        HOST: 239.255.255.250:1982\r\n
14
        MAN: \"ssdp:discover\"\r\n
15
        ST: wifi_bulb\r\n";
16
    const MULTICAST_ADDRESS = '239.255.255.250:1982';
17
    const NO_FLAG = 0;
18
    const PACKET_LENGTH = 4096;
19
20
    /**
21
     * @var Bulb[]
22
     */
23
    private $bulbList = [];
24
25
    /**
26
     * @var int
27
     */
28
    private $readTimeout;
29
30
    /**
31
     * @var Socket
32
     */
33
    private $socket;
34
35
    /**
36
     * @var BulbFactory
37
     */
38
    private $bulbFactory;
39
40
    /**
41
     * YeelightClient constructor.
42
     *
43
     * @param int         $readTimeout
44
     * @param Socket      $socket
45
     * @param BulbFactory $bulbFactory
46
     */
47
    public function __construct(int $readTimeout, Socket $socket, BulbFactory $bulbFactory)
48
    {
49
        $this->readTimeout = $readTimeout;
50
        $this->socket = $socket;
51
        $this->bulbFactory = $bulbFactory;
52
    }
53
54
    /**
55
     * Local discovery for bulbs
56
     *
57
     * @return Bulb[]
58
     *
59
     * @throws SocketException
60
     */
61
    public function search(): array
62
    {
63
        $this->socket->sendTo(self::DISCOVERY_RESPONSE, self::NO_FLAG, self::MULTICAST_ADDRESS);
64
        $this->socket->setBlocking(false);
65
        while ($this->socket->selectRead($this->readTimeout)) {
66
            $data = $this->formatResponse($this->socket->read(self::PACKET_LENGTH));
67
            $bulb = $this->bulbFactory->create($data);
68
            $this->bulbList[$bulb->getIp()] = $bulb;
69
        }
70
71
        return $this->bulbList;
72
    }
73
74
    /**
75
     * @param string $data
76
     *
77
     * @return array
78
     */
79
    private function formatResponse(string $data): array
80
    {
81
        return array_reduce(explode("\n", trim($data)), function ($carry, $item) {
82
            $res = explode(':', $item, 2);
83
            $carry[trim(reset($res))] = end($res);
84
85
            return $carry;
86
        }, []);
87
    }
88
}
89