RemoteSpec::instance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-8
6
 * Time: 下午5:17.
7
 */
8
9
namespace MiotApi\Contract;
10
11
use MiotApi\Util\Jsoner\Jsoner;
12
use MiotApi\Util\Request;
13
14
class RemoteSpec extends Jsoner
15
{
16
    private static $host = 'miot-spec.org';
17
18
    private static $prot = 80;
19
20
    private static $namespaces = 'miot-spec-v2';
21
22
    private static $timeout = 10;
23
24
    const SPEC = 'spec';
25
26
    const INSTANCES = 'instances';
27
28
    const PROPERTIES = 'properties';
29
30
    const ACTIONS = 'actions';
31
32
    const EVENTS = 'events';
33
34
    const SERVICES = 'services';
35
36
    const DEVICES = 'devices';
37
38
    const INSTANCE = 'instance';
39
40
    const PROPERTY = 'property';
41
42
    const ACTION = 'action';
43
44
    const EVENT = 'event';
45
46
    const SERVICE = 'service';
47
48
    const DEVICE = 'device';
49
50
    /**
51
     * 读取所有设备实例列表.
52
     *
53
     * @return bool|Jsoner|null
54
     */
55
    public static function instances()
56
    {
57
        $file = self::INSTANCES;
58
59
        return self::__instances($file);
60
    }
61
62
    /**
63
     * 读取所有的PropertyType.
64
     *
65
     * @return bool|Jsoner|null
66
     */
67
    public static function properties()
68
    {
69
        $file = self::SPEC.DIRECTORY_SEPARATOR.self::PROPERTIES;
70
71
        return self::__instances($file);
72
    }
73
74
    /**
75
     * 读取所有的ActionType.
76
     *
77
     * @return bool|Jsoner|null
78
     */
79
    public static function actions()
80
    {
81
        $file = self::SPEC.DIRECTORY_SEPARATOR.self::ACTIONS;
82
83
        return self::__instances($file);
84
    }
85
86
    /**
87
     * 读取所有的EventType.
88
     *
89
     * @return bool|Jsoner|null
90
     */
91
    public static function events()
92
    {
93
        $file = self::SPEC.DIRECTORY_SEPARATOR.self::EVENTS;
94
95
        return self::__instances($file);
96
    }
97
98
    /**
99
     * 读取所有的ServiceType.
100
     *
101
     * @return bool|Jsoner|null
102
     */
103
    public static function services()
104
    {
105
        $file = self::SPEC.DIRECTORY_SEPARATOR.self::SERVICES;
106
107
        return self::__instances($file);
108
    }
109
110
    /**
111
     * 读取所有的DeviceType.
112
     *
113
     * @return bool|Jsoner|null
114
     */
115
    public static function devices()
116
    {
117
        $file = self::SPEC.DIRECTORY_SEPARATOR.self::DEVICES;
118
119
        return self::__instances($file);
120
    }
121
122
    /**
123
     * 读取某个实例的详细定义.
124
     *
125
     * @return bool|Jsoner|null
126
     */
127
    public static function instance($type)
128
    {
129
        $file = self::INSTANCE.DIRECTORY_SEPARATOR.$type;
130
        $uri = self::INSTANCE;
131
        $params = [
132
            'type' => $type,
133
        ];
134
135
        return self::__instance($file, $uri, $params);
136
    }
137
138
    /**
139
     * 读取一个PropertyType的具体定义.
140
     *
141
     * @throws \MiotApi\Exception\SpecificationErrorException
142
     *
143
     * @return bool|Jsoner|null
144
     */
145
    public static function property($type)
146
    {
147
        $type = self::getBaseType($type);
148
        $file = self::PROPERTY.DIRECTORY_SEPARATOR.$type;
149
        $uri = self::SPEC.DIRECTORY_SEPARATOR.self::PROPERTY;
150
        $params = [
151
            'type' => $type,
152
        ];
153
154
        return self::__instance($file, $uri, $params);
155
    }
156
157
    /**
158
     * 读取一个ActionType的具体定义.
159
     *
160
     * @throws \MiotApi\Exception\SpecificationErrorException
161
     *
162
     * @return bool|Jsoner|null
163
     */
164
    public static function action($type)
165
    {
166
        $type = self::getBaseType($type);
167
        $file = self::ACTION.DIRECTORY_SEPARATOR.$type;
168
        $uri = self::SPEC.DIRECTORY_SEPARATOR.self::ACTION;
169
        $params = [
170
            'type' => $type,
171
        ];
172
173
        return self::__instance($file, $uri, $params);
174
    }
175
176
    /**
177
     * 读取一个EventType的具体定义.
178
     *
179
     * @throws \MiotApi\Exception\SpecificationErrorException
180
     *
181
     * @return bool|Jsoner|null
182
     */
183
    public static function event($type)
184
    {
185
        $type = self::getBaseType($type);
186
        $file = self::EVENT.DIRECTORY_SEPARATOR.$type;
187
        $uri = self::SPEC.DIRECTORY_SEPARATOR.self::EVENT;
188
        $params = [
189
            'type' => $type,
190
        ];
191
192
        return self::__instance($file, $uri, $params);
193
    }
194
195
    /**
196
     * 读取一个ServiceType的具体定义.
197
     *
198
     * @throws \MiotApi\Exception\SpecificationErrorException
199
     *
200
     * @return bool|Jsoner|null
201
     */
202
    public static function service($type)
203
    {
204
        $type = self::getBaseType($type);
205
        $file = self::SERVICE.DIRECTORY_SEPARATOR.$type;
206
        $uri = self::SPEC.DIRECTORY_SEPARATOR.self::SERVICE;
207
        $params = [
208
            'type' => $type,
209
        ];
210
211
        return self::__instance($file, $uri, $params);
212
    }
213
214
    /**
215
     * 读取一个DeviceType的具体定义.
216
     *
217
     * @throws \MiotApi\Exception\SpecificationErrorException
218
     *
219
     * @return bool|Jsoner|null
220
     */
221
    public static function device($type)
222
    {
223
        $type = self::getBaseType($type);
224
        $file = self::DEVICE.DIRECTORY_SEPARATOR.$type;
225
        $uri = self::SPEC.DIRECTORY_SEPARATOR.self::DEVICE;
226
        $params = [
227
            'type' => $type,
228
        ];
229
230
        return self::__instance($file, $uri, $params);
231
    }
232
233
    /**
234
     * 读取所有实例列表.
235
     *
236
     * @return bool|Jsoner|null
237
     */
238
    private static function __instances($file)
239
    {
240
        $instances = Jsoner::load($file);
241
        if (!$instances) {
242
            $instances = self::fetch($file);
243
244
            if (!$instances) {
245
                return false;
246
            }
247
248
            Jsoner::fill($instances, $file);
249
        }
250
251
        return $instances;
252
    }
253
254
    /**
255
     * 读取某个实例的详细定义.
256
     *
257
     * @param $file
258
     * @param $uri
259
     * @param $params
260
     *
261
     * @return bool|Jsoner|null
262
     */
263
    private static function __instance($file, $uri, $params)
264
    {
265
        $instance = Jsoner::load($file);
266
        if (!$instance) {
267
            $instance = self::fetch($uri, $params);
268
269
            if (!$instance) {
270
                return false;
271
            }
272
273
            $instance = Jsoner::fill($instance, $file);
274
        }
275
276
        return $instance;
277
    }
278
279
    /**
280
     * @param $type
281
     *
282
     * @throws \MiotApi\Exception\SpecificationErrorException
283
     *
284
     * @return mixed
285
     */
286
    private static function getBaseType($type)
287
    {
288
        $urn = new Urn($type);
289
290
        return $urn->getBaseUrn();
291
    }
292
293
    /**
294
     * @param $uri
295
     * @param array $params
296
     *
297
     * @return bool
298
     */
299
    public static function fetch($uri, $params = [])
300
    {
301
        $http = new Request(
302
            self::$host,
303
            '/'.self::$namespaces.'/'.$uri,
304
            self::$prot,
305
            true,
306
            self::$timeout
307
        );
308
309
        $result = $http
310
            ->setQueryParams($params)
311
            ->execute()
312
            ->getResponseText();
313
314
        if ($result) {
315
            return $result;
316
        }
317
318
        return false;
319
    }
320
}
321