CloudInfinite::normalize()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 13
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 42
rs 9.2222
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5\Plugins;
4
5
use League\Flysystem\Plugin\AbstractPlugin;
6
use SimpleXMLElement;
7
8
class CloudInfinite extends AbstractPlugin
9
{
10
    /**
11
     * Get the method name.
12
     *
13
     * @return string
14
     */
15
    public function getMethod()
16
    {
17
        return 'cloudInfinite';
18
    }
19
20
    /**
21
     * @return $this
22
     */
23
    public function handle()
24
    {
25
        return $this;
26
    }
27
28
    /**
29
     * @param string $objectKey
30
     * @param array  $picOperations
31
     *
32
     * @return array
33
     */
34
    public function imageProcess($objectKey, array $picOperations)
35
    {
36
        $adapter = $this->filesystem->getAdapter();
37
38
        $url = 'https://'.$adapter->getPicturePath($objectKey).'?image_process';
39
40
        $response = $adapter->getHttpClient()->post($url, [
41
            'http_errors' => false,
42
            'headers'     => [
43
                'Authorization'  => $adapter->getAuthorization('POST', $url),
44
                'Pic-Operations' => \GuzzleHttp\json_encode(
45
                    $picOperations, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
46
                ),
47
            ],
48
        ]);
49
50
        return $this->parse(
51
            $response->getBody()->getContents()
52
        );
53
    }
54
55
    /**
56
     * @param string $objectKey
57
     * @param array  $contentRecognition
58
     *
59
     * @return array
60
     */
61
    public function contentRecognition($objectKey, array $contentRecognition)
62
    {
63
        $adapter = $this->filesystem->getAdapter();
64
65
        $url = 'https://'.$adapter->getPicturePath($objectKey).'?CR';
66
67
        $response = $adapter->getHttpClient()->get($url, [
68
            'http_errors' => false,
69
            'headers'     => [
70
                'Authorization'       => $adapter->getAuthorization('GET', $url),
71
                'Content-Recognition' => \GuzzleHttp\json_encode(
72
                    $contentRecognition, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
73
                ),
74
            ],
75
        ]);
76
77
        return $this->parse(
78
            $response->getBody()->getContents()
79
        );
80
    }
81
82
    /**
83
     * @param string $xml
84
     *
85
     * @return array
86
     */
87
    protected function parse($xml)
88
    {
89
        $backup = libxml_disable_entity_loader(true);
90
91
        $result = $this->normalize(
92
            simplexml_load_string(
93
                $this->sanitize($xml),
94
                'SimpleXMLElement',
95
                LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS
96
            )
97
        );
98
99
        libxml_disable_entity_loader($backup);
100
101
        return $result;
102
    }
103
104
    /**
105
     * Object to array.
106
     *
107
     *
108
     * @param SimpleXMLElement $obj
109
     *
110
     * @return array
111
     */
112
    protected function normalize($obj)
113
    {
114
        $result = null;
115
116
        if (is_object($obj)) {
117
            $obj = (array) $obj;
118
        }
119
120
        if (is_array($obj)) {
121
            foreach ($obj as $key => $value) {
122
                $res = $this->normalize($value);
123
                if (('@attributes' === $key) && ($key)) {
124
                    $result = $res; // @codeCoverageIgnore
125
                } else {
126
                    $result[$key] = $res;
127
                }
128
            }
129
        } else {
130
            $result = $obj;
131
        }
132
133
        return $result;
134
    }
135
136
    /**
137
     * Delete invalid characters in XML.
138
     *
139
     * @see https://www.w3.org/TR/2008/REC-xml-20081126/#charsets - XML charset range
140
     * @see http://php.net/manual/en/regexp.reference.escape.php - escape in UTF-8 mode
141
     *
142
     * @param string $xml
143
     *
144
     * @return string
145
     */
146
    protected function sanitize($xml)
147
    {
148
        return preg_replace(
149
            '/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u',
150
            '',
151
            $xml
152
        );
153
    }
154
}
155