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.

Lifx::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Kz\Lifx;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
8
/**
9
 * Class Lifx
10
 * @package Kz\Lifx
11
 */
12
class Lifx
13
{
14
    const API_URL = 'https://api.lifx.com/';
15
    const API_VERSION = 'v1beta1';
16
17
    /**
18
     * @var Client
19
     */
20
    private $client;
21
22
    /**
23
     * Instantiates the Guzzle client using the authorization token.
24
     * A token can be obtained at: https://cloud.lifx.com/settings
25
     *
26
     * @param string $token Authorization token for the LIFX HTTP API.
27
     */
28
    public function __construct($token)
29
    {
30
        $client = new Client([
31
            'base_uri' => self::API_URL . self::API_VERSION . '/',
32
            'headers' => [
33
                'Authorization' => 'Bearer ' . $token,
34
            ],
35
        ]);
36
        $this->client = $client;
37
    }
38
39
40
    /**
41
     * Sends a request to the LIFX HTTP API.
42
     *
43
     * @param $request
44
     * @param array $options
45
     * @return \Psr\Http\Message\ResponseInterface
46
     */
47
    public function sendRequest($request, $options = [])
48
    {
49
        $client = $this->client;
50
        $response = $client->send($request, $options);
51
52
        return $response;
53
    }
54
55
    /**
56
     * Gets lights belonging to the authenticated account.
57
     *
58
     * @param string $selector Selector used to filter lights. Defaults to `all`.
59
     * @return \Psr\Http\Message\StreamInterface
60
     */
61 View Code Duplication
    public function getLights($selector = 'all')
62
    {
63
        $request = new Request('GET', 'lights/' . $selector);
64
        $response = $this->sendRequest($request);
65
66
        return $response->getBody();
67
    }
68
69
    /**
70
     * Toggle off lights if they are on, or turn them on if they are off.
71
     * Physically powered off lights are ignored.
72
     *
73
     * @param string $selector Selector used to filter lights. Defaults to `all`.
74
     * @return \Psr\Http\Message\StreamInterface
75
     */
76 View Code Duplication
    public function toggleLights($selector = 'all')
77
    {
78
        $request = new Request('POST', 'lights/' . $selector . '/toggle');
79
        $response = $this->sendRequest($request);
80
81
        return $response->getBody();
82
    }
83
84
    /**
85
     * Turn lights on or off.
86
     *
87
     * @param string $selector Selector used to filter lights. Defaults to `all`.
88
     * @param string $state State of 'on' or 'off'.
89
     * @param float $duration (Optional) Fade to the given `state` over a duration of seconds. Defaults to `1.0`.
90
     * @return \Psr\Http\Message\StreamInterface
91
     */
92 View Code Duplication
    public function setLights($selector = 'all', $state = 'on', $duration = 1.0)
93
    {
94
        $request = new Request('PUT', 'lights/' . $selector . '/power');
95
        $response = $this->sendRequest($request,[
96
            'query' => [
97
                'state' => $state,
98
                'duration' => $duration
99
            ]
100
        ]);
101
102
        return $response->getBody();
103
    }
104
105
    /**
106
     * Set lights to any color.
107
     *
108
     * @param string $selector Selector used to filter lights. Defaults to `all`.
109
     * @param string $color Color the lights should be set to. Defaults to `white`.
110
     * @param float $duration (Optional) Fade to the given `state` over a duration of seconds. Defaults to `1.0`.
111
     * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`.
112
     * @return \Psr\Http\Message\StreamInterface
113
     */
114 View Code Duplication
    public function setColor($selector = 'all', $color = 'white', $duration = 1.0, $power_on = true)
115
    {
116
        $request = new Request('PUT', 'lights/' . $selector . '/color');
117
        $response = $this->sendRequest($request,[
118
            'query' => [
119
                'color' => $color,
120
                'duration' => $duration,
121
                'power_on' => $power_on,
122
            ]
123
        ]);
124
125
        return $response->getBody();
126
    }
127
128
    /**
129
     * Performs a breathe effect by slowly fading between the given colors.
130
     * If `from_color` is omitted then the current color is used in its place.
131
     *
132
     * @param string $selector Selector used to filter lights. Defaults to `all`.
133
     * @param string $color Color the lights should be set to. Defaults to `purple`.
134
     * @param string|null $from_color (Optional) From color of the waveform. Defaults to `null`.
135
     * @param float $period (Optional) Period of the waveform in seconds. Defaults to `1.0`.
136
     * @param float $cycles (Optional) Number of times to repeat, cycle counts. Defaults to `1.0`.
137
     * @param bool $persist (Optional) Whether to keep state at the end of the effect. Defaults to `false`.
138
     * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`.
139
     * @param float $peak (Optional) Defines where in a period the target color is at its maximum. Defaults to `0.5`. Minimum `0.0`, maximum `1.0`.
140
     * @return \Psr\Http\Message\StreamInterface
141
     */
142 View Code Duplication
    public function breatheLights(
143
        $selector = 'all',
144
        $color = 'purple',
145
        $from_color = null,
146
        $period = 1.0,
147
        $cycles = 1.0,
148
        $persist = false,
149
        $power_on = true,
150
        $peak = 0.5
151
    ) {
152
        $request = new Request('POST', 'lights/' . $selector . '/effects/breathe');
153
        $response = $this->sendRequest($request,[
154
            'query' => [
155
                'color' => $color,
156
                'from_color' => $from_color,
157
                'period' => $period,
158
                'cycles' => $cycles,
159
                'persist' => $persist,
160
                'power_on' => $power_on,
161
                'peak' => $peak,
162
            ]
163
        ]);
164
165
        return $response->getBody();
166
    }
167
168
    /**
169
     * Performs a pulse effect by quickly flashing between the given colors.
170
     * If `from_color` is omitted then the current color is used in its place.
171
     *
172
     * @param string $selector Selector used to filter lights. Defaults to `all`.
173
     * @param string $color Color the lights should be set to. Defaults to `purple`.
174
     * @param string|null $from_color (Optional) From color of the waveform. Defaults to `null`.
175
     * @param float $period (Optional) Period of the waveform in seconds. Defaults to `1.0`.
176
     * @param float $cycles (Optional) Number of times to repeat, cycle counts. Defaults to `1.0`.
177
     * @param bool $persist (Optional) Whether to keep state at the end of the effect. Defaults to `false`.
178
     * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`.
179
     * @param float $duty_cycle (Optional) Ratio of the period where color is active. Defaults to `0.5`. Minimum `0.0`, maximum `1.0`.
180
     * @return \Psr\Http\Message\StreamInterface
181
     */
182 View Code Duplication
    public function pulseLights(
183
        $selector = 'all',
184
        $color = 'purple',
185
        $from_color = null,
186
        $period = 1.0,
187
        $cycles = 1.0,
188
        $persist = false,
189
        $power_on = true,
190
        $duty_cycle = 0.5
191
    ) {
192
        $request = new Request('POST', 'lights/' . $selector . '/effects/pulse');
193
        $response = $this->sendRequest($request,[
194
            'query' => [
195
                'selector' => $selector,
196
                'color' => $color,
197
                'from_color' => $from_color,
198
                'period' => $period,
199
                'cycles' => $cycles,
200
                'persist' => $persist,
201
                'power_on' => $power_on,
202
                'duty_cycle' => $duty_cycle,
203
            ]
204
        ]);
205
206
        return $response->getBody();
207
    }
208
}