File::upload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the light/easemob.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\Easemob\Rest;
13
14
use Psr\Http\Message\RequestInterface;
15
16
/**
17
 * Class File.
18
 *
19
 * ~~~
20
 * $file = $easemob->file;
21
 * $result = $file->upload('/home/1.png');
22
 * $file_url = $file->url($result['uuid']);
23
 * ~~~
24
 */
25
class File extends Rest
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function registerHttpMiddleware()
31
    {
32
        parent::registerHttpMiddleware();
33
34
        $this->http->addMiddleware($this->RestrictAccessMiddleware());
35
    }
36
37
    /**
38
     * @return \Closure
39
     */
40
    public function RestrictAccessMiddleware()
41
    {
42
        return function (callable $handler) {
43
            return function (RequestInterface $request, array $options) use ($handler) {
44
45
                if (!$this->accessToken->getToken()) {
46
                    return $handler($request, $options);
47
                }
48
49
                $request = $request->withHeader('restrict-access ', 'true');
50
51
                return $handler($request, $options);
52
            };
53
        };
54
    }
55
56
    /**
57
     * 上传文件.
58
     *
59
     * ~~~
60
     * [
61
     *     'uuid' => 'd8329590-c018-11e5-a936-0f89b914ff91',
62
     *     'type' => 'chatfile',
63
     *     'share-secret' => '2DKVmsAYEeWn7wsNJSQy-QxD0mQSSXd6jqC7PpD7_YQuIDr6'
64
     * ]
65
     * ~~~
66
     *
67
     * @param string $file
68
     *
69
     * @return array
70
     */
71
    public function upload($file)
72
    {
73
        $response = $this->http->upload('chatfiles', [
74
            'file' => $file,
75
        ]);
76
77
        return array_shift($this->parseResponse($response)['entities']);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->http->upload('cha...array('file' => $file)) on line 73 can be null; however, light\Easemob\Rest\Rest::parseResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
78
    }
79
80
    /**
81
     * Return the resource web access url.
82
     *
83
     * @param string $uuid
84
     *
85
     * @return string
86
     */
87
    public function url($uuid)
88
    {
89
        return (string) $this->http->buildUri("chatfiles/{$uuid}");
90
    }
91
}
92