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.
Completed
Push — master ( ae3ad2...c1f900 )
by
unknown
05:02
created

MyQ::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Route4Me;
3
4
use Route4Me\Common;
5
/**
6
 * class MyQ
7
 *
8
 * Offers authentication to MyQ API, and access to garage door open/close/status functions
9
 *
10
 */
11
class MyQException extends \Exception {}
12
class MyQ {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
13
    
14
    /** @var string|null $username contains the username used to authenticate with the MyQ API */
15
    protected $username = null;
16
    /** @var string|null $password contains the password used to authenticate with the MyQ API */
17
    protected $password = null;
18
    /** @var string|null $appId is the application ID used to register with the MyQ API */
19
    protected $appId = 'NWknvuBd7LoFHfXmKNMBcgajXtZEgKUh4V7WNzMidrpUUluDpVYVZx+xT4PCM5Kx';
20
    //protected $appId = 'Vj8pQggXLhLy0WHahglCD4N1nAkkXQtGYpq2HrHD7H1nvmbT55KqtN6RSF4ILB%2fi';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
    /** @var string|null $securityToken is the auth token returned after a successful login */
22
    protected $securityToken = null;
23
    /** @var string|null $userAgent is the User-Agent header value sent with each API request */
24
    protected $userAgent = 'Chamberlain/3.4.1';
25
    /** @var string|null $culture is the API culture code for the API */
26
    protected $culture = 'en';
27
    /** @var string|null $contentType is the content type used for all cURL requests */
28
    protected $contentType = 'application/json';
29
    /** @var array $headers contain HTTP headers for cURL requests */
30
    protected $_headers = array();
31
    protected $_deviceId = null;
32
    protected $_locationName = null;
33
    protected $_doorName = null;
34
    protected $_loginUrl = 'https://myqexternal.myqdevice.com/api/v4/User/Validate';
35
    protected $_getDeviceDetailUrl = 'https://myqexternal.myqdevice.com/api/v4/userdevicedetails/get?&filterOn=true';
36
    protected $_putDeviceStateUrl = '/api/v4/DeviceAttribute/PutDeviceAttribute';
37
    /** @var resource|null $_conn is the web connection to the MyQ API */
38
    protected $_conn = null;
39
    /**
40
     * Initializes class. Optionally allows user to override variables
41
     *
42
     * @param array $params A associative array for overwriting class variables
43
     *
44
     * @return MyQ
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
45
     */
46
    public function __construct ($params = array()) {
47
        // Overwrite class variables
48
        foreach ($params as $k => $v) {
49
            $this->$k = $v;
50
        }
51
        // Initialize cURL request headers
52
        if (sizeof($this->_headers) == 0) {
53
            $this->_headers = array (
54
                'MyQApplicationId' => $this->appId,
55
                'Culture' => $this->culture,
56
                'Content-Type' => $this->contentType,
57
                'User-Agent' => $this->userAgent,
58
            );
59
        }
60
        // Initialize cURL connection
61
        $this->_init();
62
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
63
    }
64
    /**
65
     * Perform a login request
66
     *
67
     * @param string|null $username Username to use when logging in
68
     * @param string|null $password Password to use for logging in
69
     *
70
     * @return MyQ
71
     */
72
    public function login ($username = null, $password = null) {
73
        // Set username/password if not null
74
        if (!is_null($username)) {
75
            $this->username = $username;
76
        }
77
        if (!is_null($password)) {
78
            $this->password = $password;
79
        }
80
        // confirm that we have a valid username/password
81
        $error = array();
82
        if (is_null($this->username)) {
83
            $error[] = 'username';
84
        }
85
        if (is_null($this->password)) {
86
            $error[] = 'password';
87
        }
88
        if (sizeof($error) > 0) {
89
            throw new MyQException('Missing required auth credential: ' . implode(',', $error));
90
        }
91
        $this->_login();
92
    }
93
    public function getState () {
94
        $this->_getDetails();
95
        $timeInState = time() - $this->_doorStateTime;
0 ignored issues
show
Bug introduced by
The property _doorStateTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96
        echo implode(',', array (
97
            $this->_locationName,
98
            $this->_doorName,
99
            $this->_doorState,
0 ignored issues
show
Bug introduced by
The property _doorState does not seem to exist. Did you mean _doorStateTime?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
100
            (int)$timeInState,
101
        ));
102
    }
103
	
104
	public function getDetails() {
105
		return $this->_getDetails();
106
	}
107
	
108
    private function _init () {
109
        if (!isset($this->_conn)) {
110
            $this->_conn = curl_init();
111
            curl_setopt_array($this->_conn, array (
112
                CURLOPT_RETURNTRANSFER => true,
113
                CURLOPT_ENCODING => "",
114
                CURLOPT_MAXREDIRS => 10,
115
                CURLOPT_TIMEOUT => 30,
116
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
117
                CURLOPT_FAILONERROR => true,
118
                CURLOPT_FOLLOWLOCATION => true,
119
                CURLOPT_FRESH_CONNECT => true,
120
                CURLOPT_FORBID_REUSE => true,
121
                CURLOPT_USERAGENT => $this->userAgent,
122
            ));
123
        }
124
        $this->_setHeaders();
125
    }
126
    private function _setHeaders () {
127
        $headers = array();
128
        foreach ($this->_headers as $k => $v) {
129
            $headers[] = "$k: $v";
130
        }
131
        curl_setopt($this->_conn, CURLOPT_HTTPHEADER, $headers);
132
    }
133
    private function _login () {
134
        $this->_init();
135
        curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'POST');
136
        curl_setopt($this->_conn, CURLOPT_URL, $this->_loginUrl);
137
        $post = json_encode(array('username' => $this->username, 'password' => $this->password));
138
        curl_setopt($this->_conn, CURLOPT_POSTFIELDS, $post);
139
        $output = curl_exec($this->_conn);
140
        $data = json_decode($output);
141
        if ($data == false || !isset($data->SecurityToken)) {
142
            throw new MyQException("Error processing login request: $output");
143
        }
144
        $this->_headers['SecurityToken'] = $data->SecurityToken;
145
        return $this;
146
    }
147
    private function _getDetails () {
148
        $this->_init();
149
        curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'GET');
150
        curl_setopt($this->_conn, CURLOPT_URL, $this->_getDeviceDetailUrl);
151
        $output = curl_exec($this->_conn);
152
        $data = json_decode($output);
153
        if ($data == false || !isset($data->Devices)) {
154
            throw new MyQException("Error fetching device details: $output");
155
        }
156
        // Find our door device ID
157
        foreach ($data->Devices as $device) {
158
            if (stripos($device->MyQDeviceTypeName, "Gateway") !== false) {
159
                // Find location name
160
                foreach ($device->Attributes as $attr) {
161
                    if ($attr->AttributeDisplayName == 'desc') {
162
                        $this->_locationName = $attr->Value;
163
                    }
164
                }
165
            }
166
            
167
            $this->_deviceId = $device->MyQDeviceId;
168
            foreach ($device->Attributes as $attr) {
169
                switch ($attr->AttributeDisplayName) {
170
                    case 'desc':
171
                        $this->_doorName = $attr->Value;
172
                        break;
173
                    case 'doorstate':
174
                        $this->_doorState = $attr->Value;
0 ignored issues
show
Bug introduced by
The property _doorState does not seem to exist. Did you mean _doorStateTime?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
175
                        // UpdatedTime is a timestamp in ms, so we truncate
176
                        $this->_doorStateTime = (int)$attr->UpdatedTime / 1000;
177
                        break;
178
                    default:
179
                        continue;
180
                }
181
            }
182
        }
183
    }
184
}