Passed
Push — master ( 1a1cd5...cc6d2c )
by frey
05:06
created

CloudInfinite::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 2
rs 9.9332
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5\Plugins;
4
5
use Closure;
6
use League\Flysystem\Plugin\AbstractPlugin;
7
use SimpleXMLElement;
8
9
class CloudInfinite extends AbstractPlugin
10
{
11
    /**
12
     * Get the method name.
13
     *
14
     * @return string
15
     */
16
    public function getMethod()
17
    {
18
        return 'cloudInfinite';
19
    }
20
21
    /**
22
     * @param string $objectKey
23
     * @param array $picOperations
24
     *
25
     * @return mixed
26
     */
27
    public function handle($objectKey, array $picOperations)
28
    {
29
        $adapter = $this->filesystem->getAdapter();
30
31
        $url = 'https://' . $adapter->getPicturePath($objectKey) . '?image_process';
32
33
        $response = $adapter->getHttpClient()->post($url, [
34
            'http_errors' => false,
35
            'headers' => [
36
                'Authorization' => $adapter->getAuthorization('POST', $url),
37
                'Pic-Operations' => \GuzzleHttp\json_encode(
38
                    $picOperations, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
39
                ),
40
            ],
41
        ]);
42
43
        return $this->parse(
44
            $response->getBody()->getContents()
45
        );
46
    }
47
48
    /**
49
     * @param string $xml
50
     *
51
     * @return array
52
     */
53
    protected function parse($xml)
54
    {
55
        $backup = libxml_disable_entity_loader(true);
56
57
        $result = $this->normalize(
58
            simplexml_load_string(
0 ignored issues
show
Bug introduced by
It seems like simplexml_load_string($t...lugins\LIBXML_NOBLANKS) can also be of type false; however, parameter $obj of Freyo\Flysystem\QcloudCO...udInfinite::normalize() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            /** @scrutinizer ignore-type */ simplexml_load_string(
Loading history...
59
                $this->sanitize($xml),
60
                'SimpleXMLElement',
61
                LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS
62
            )
63
        );
64
65
        libxml_disable_entity_loader($backup);
66
67
        return $result;
68
    }
69
70
    /**
71
     * Object to array.
72
     *
73
     *
74
     * @param SimpleXMLElement $obj
75
     *
76
     * @return array
77
     */
78
    protected function normalize($obj)
79
    {
80
        $result = null;
81
82
        if (is_object($obj)) {
83
            $obj = (array)$obj;
84
        }
85
86
        if (is_array($obj)) {
87
            foreach ($obj as $key => $value) {
88
                $res = $this->normalize($value);
89
                if (('@attributes' === $key) && ($key)) {
90
                    $result = $res; // @codeCoverageIgnore
91
                } else {
92
                    $result[$key] = $res;
93
                }
94
            }
95
        } else {
96
            $result = $obj;
97
        }
98
99
        return $result;
100
    }
101
102
    /**
103
     * Delete invalid characters in XML.
104
     *
105
     * @see https://www.w3.org/TR/2008/REC-xml-20081126/#charsets - XML charset range
106
     * @see http://php.net/manual/en/regexp.reference.escape.php - escape in UTF-8 mode
107
     *
108
     * @param string $xml
109
     *
110
     * @return string
111
     */
112
    protected function sanitize($xml)
113
    {
114
        return preg_replace(
115
            '/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u',
116
            '',
117
            $xml
118
        );
119
    }
120
}