Passed
Push — master ( cc6d2c...b88fd4 )
by frey
03:46
created

CloudInfinite::imageProcess()   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 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 2
rs 9.9332
c 0
b 0
f 0
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
     * @return $this
23
     */
24
    public function handle()
25
    {
26
        return $this;
27
    }
28
29
    /**
30
     * @param string $objectKey
31
     * @param array $picOperations
32
     *
33
     * @return array
34
     */
35
    public function imageProcess($objectKey, array $picOperations)
36
    {
37
        $adapter = $this->filesystem->getAdapter();
38
39
        $url = 'https://' . $adapter->getPicturePath($objectKey) . '?image_process';
40
41
        $response = $adapter->getHttpClient()->post($url, [
42
            'http_errors' => false,
43
            'headers' => [
44
                'Authorization' => $adapter->getAuthorization('POST', $url),
45
                'Pic-Operations' => \GuzzleHttp\json_encode(
46
                    $picOperations, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
47
                ),
48
            ],
49
        ]);
50
51
        return $this->parse(
52
            $response->getBody()->getContents()
53
        );
54
    }
55
56
    /**
57
     * @param string $objectKey
58
     * @param array $contentRecognition
59
     *
60
     * @return array
61
     */
62
    public function contentRecognition($objectKey, array $contentRecognition)
63
    {
64
        $adapter = $this->filesystem->getAdapter();
65
66
        $url = 'https://' . $adapter->getPicturePath($objectKey) . '?CR';
67
68
        $response = $adapter->getHttpClient()->get($url, [
69
            'http_errors' => false,
70
            'headers' => [
71
                'Authorization' => $adapter->getAuthorization('GET', $url),
72
                'Content-Recognition' => \GuzzleHttp\json_encode(
73
                    $contentRecognition, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
74
                ),
75
            ],
76
        ]);
77
78
        return $this->parse(
79
            $response->getBody()->getContents()
80
        );
81
    }
82
83
    /**
84
     * @param string $xml
85
     *
86
     * @return array
87
     */
88
    protected function parse($xml)
89
    {
90
        $backup = libxml_disable_entity_loader(true);
91
92
        $result = $this->normalize(
93
            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

93
            /** @scrutinizer ignore-type */ simplexml_load_string(
Loading history...
94
                $this->sanitize($xml),
95
                'SimpleXMLElement',
96
                LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS
97
            )
98
        );
99
100
        libxml_disable_entity_loader($backup);
101
102
        return $result;
103
    }
104
105
    /**
106
     * Object to array.
107
     *
108
     *
109
     * @param SimpleXMLElement $obj
110
     *
111
     * @return array
112
     */
113
    protected function normalize($obj)
114
    {
115
        $result = null;
116
117
        if (is_object($obj)) {
118
            $obj = (array)$obj;
119
        }
120
121
        if (is_array($obj)) {
122
            foreach ($obj as $key => $value) {
123
                $res = $this->normalize($value);
124
                if (('@attributes' === $key) && ($key)) {
125
                    $result = $res; // @codeCoverageIgnore
126
                } else {
127
                    $result[$key] = $res;
128
                }
129
            }
130
        } else {
131
            $result = $obj;
132
        }
133
134
        return $result;
135
    }
136
137
    /**
138
     * Delete invalid characters in XML.
139
     *
140
     * @see https://www.w3.org/TR/2008/REC-xml-20081126/#charsets - XML charset range
141
     * @see http://php.net/manual/en/regexp.reference.escape.php - escape in UTF-8 mode
142
     *
143
     * @param string $xml
144
     *
145
     * @return string
146
     */
147
    protected function sanitize($xml)
148
    {
149
        return preg_replace(
150
            '/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u',
151
            '',
152
            $xml
153
        );
154
    }
155
}